12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 5.5 Heaps <strong>and</strong> Priority Queues 185routine is finished. If the value of V is greater than that of its parent, then the twoelements swap positions. From here, the process of comparing V <strong>to</strong> its (current)parent continues until V reaches its correct position.Each call <strong>to</strong> insert takes Θ(log n) time in the worst case, because the valuebeing inserted can move at most the distance from the bot<strong>to</strong>m of the tree <strong>to</strong> the <strong>to</strong>pof the tree. Thus, the time <strong>to</strong> insert n values in<strong>to</strong> the heap, if we insert them one ata time, will be Θ(n log n) in the worst case.If all n values are available at the beginning of the building process, we canbuild the heap faster than just inserting the values in<strong>to</strong> the heap one by one. ConsiderFigure 5.20(a), which shows one series of exchanges that will result in a heap.Note that this figure shows the input in its logical form as a complete binary tree,but you should realize that these values are physically s<strong>to</strong>red in an array. All exchangesare between a node <strong>and</strong> one of its children. The heap is formed as a resul<strong>to</strong>f this exchange process. The array for the right-h<strong>and</strong> tree of Figure 5.20(a) wouldappear as follows:7 4 6 1 2 3 5Figure 5.20(b) shows an alternate series of exchanges that also forms a heap,but much more efficiently. From this example, it is clear that the heap for any givenset of numbers is not unique, <strong>and</strong> we see that some rearrangements of the inputvalues require fewer exchanges than others <strong>to</strong> build the heap. So, how do we pickthe best rearrangement?One good algorithm stems from induction. Suppose that the left <strong>and</strong> right subtreesof the root are already heaps, <strong>and</strong> R is the name of the element at the root.This situation is illustrated by Figure 5.21. In this case there are two possibilities.(1) R has a value greater than or equal <strong>to</strong> its two children. In this case, constructionis complete. (2) R has a value less than one or both of its children. In this case,R should be exchanged with the child that has greater value. The result will be aheap, except that R might still be less than one or both of its (new) children. In thiscase, we simply continue the process of “pushing down” R until it reaches a levelwhere it is greater than its children, or is a leaf node. This process is implementedby the private method siftdown of the heap class. The siftdown operation isillustrated by Figure 5.22.This approach assumes that the subtrees are already heaps, suggesting that acomplete algorithm can be obtained by visiting the nodes in some order such thatthe children of a node are visited before the node itself. One simple way <strong>to</strong> do thisis simply <strong>to</strong> work from the high index of the array <strong>to</strong> the low index. Actually, thebuild process need not visit the leaf nodes (they can never move down because theyare already at the bot<strong>to</strong>m), so the building algorithm can start in the middle of the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!