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.

182 Chap. 5 Binary TreesMin-heaps <strong>and</strong> max-heaps both have their uses. For example, the Heapsor<strong>to</strong>f Section 7.6 uses the max-heap, while the Replacement Selection algorithm ofSection 8.5.2 uses a min-heap. The examples in the rest of this section will use amax-heap.Be careful not <strong>to</strong> confuse the logical representation of a heap with its physicalimplementation by means of the array-based complete binary tree. The two are notsynonymous because the logical view of the heap is actually a tree structure, whilethe typical physical implementation uses an array.Figure 5.19 shows an implementation for heaps. The class is a generic with onetype parameter. E defines the type for the data elements s<strong>to</strong>red in the heap. E mustextend the Comparable interface, <strong>and</strong> we use the compareTo for comparingrecords in the heap.This class definition makes two concessions <strong>to</strong> the fact that an array-based implementationis used. First, heap nodes are indicated by their logical position withinthe heap rather than by a pointer <strong>to</strong> the node. In practice, the logical heap positioncorresponds <strong>to</strong> the identically numbered physical position in the array. Second, theconstruc<strong>to</strong>r takes as input a pointer <strong>to</strong> the array <strong>to</strong> be used. This approach providesthe greatest flexibility for using the heap because all data values can be loaded in<strong>to</strong>the array directly by the client. The advantage of this comes during the heap constructionphase, as explained below. The construc<strong>to</strong>r also takes an integer parameterindicating the initial size of the heap (based on the number of elements initiallyloaded in<strong>to</strong> the array) <strong>and</strong> a second integer parameter indicating the maximum sizeallowed for the heap (the size of the array).Method heapsize returns the current size of the heap. H.isLeaf(pos)will return true if position pos is a leaf in heap H, <strong>and</strong> false otherwise. Membersleftchild, rightchild, <strong>and</strong> parent return the position (actually, thearray index) for the left child, right child, <strong>and</strong> parent of the position passed, respectively.One way <strong>to</strong> build a heap is <strong>to</strong> insert the elements one at a time. Method insertwill insert a new element V in<strong>to</strong> the heap. You might expect the heap insertion process<strong>to</strong> be similar <strong>to</strong> the insert function for a BST, starting at the root <strong>and</strong> workingdown through the heap. However, this approach is not likely <strong>to</strong> work because theheap must maintain the shape of a complete binary tree. Equivalently, if the heaptakes up the first n positions of its array prior <strong>to</strong> the call <strong>to</strong> insert, it must takeup the first n + 1 positions after. To accomplish this, insert first places V at positionn of the array. Of course, V is unlikely <strong>to</strong> be in the correct position. To moveV <strong>to</strong> the right place, it is compared <strong>to</strong> its parent’s value. If the value of V is lessthan or equal <strong>to</strong> the value of its parent, then it is in the correct place <strong>and</strong> the insert

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

Saved successfully!

Ooh no, something went wrong!