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 181next job selected is the one with the highest priority. Priority is indicated by aparticular value associated with the job (<strong>and</strong> might change while the job remains inthe wait list).When a collection of objects is organized by importance or priority, we callthis a priority queue. A normal queue data structure will not implement a priorityqueue efficiently because search for the element with highest priority will takeΘ(n) time. A list, whether sorted or not, will also require Θ(n) time for either insertionor removal. A BST that organizes records by priority could be used, with the<strong>to</strong>tal of n inserts <strong>and</strong> n remove operations requiring Θ(n log n) time in the averagecase. However, there is always the possibility that the BST will become unbalanced,leading <strong>to</strong> bad performance. Instead, we would like <strong>to</strong> find a data structurethat is guaranteed <strong>to</strong> have good performance for this special application.This section presents the heap 4 data structure. A heap is defined by two properties.First, it is a complete binary tree, so heaps are nearly always implementedusing the array representation for complete binary trees presented in Section 5.3.3.Second, the values s<strong>to</strong>red in a heap are partially ordered. This means that there isa relationship between the value s<strong>to</strong>red at any node <strong>and</strong> the values of its children.There are two variants of the heap, depending on the definition of this relationship.A max-heap has the property that every node s<strong>to</strong>res a value that is greater thanor equal <strong>to</strong> the value of either of its children. Because the root has a value greaterthan or equal <strong>to</strong> its children, which in turn have values greater than or equal <strong>to</strong> theirchildren, the root s<strong>to</strong>res the maximum of all values in the tree.A min-heap has the property that every node s<strong>to</strong>res a value that is less thanor equal <strong>to</strong> that of its children. Because the root has a value less than or equal <strong>to</strong>its children, which in turn have values less than or equal <strong>to</strong> their children, the roots<strong>to</strong>res the minimum of all values in the tree.Note that there is no necessary relationship between the value of a node <strong>and</strong> tha<strong>to</strong>f its sibling in either the min-heap or the max-heap. For example, it is possible thatthe values for all nodes in the left subtree of the root are greater than the values forevery node of the right subtree. We can contrast BSTs <strong>and</strong> heaps by the strength oftheir ordering relationships. A BST defines a <strong>to</strong>tal order on its nodes in that, giventhe positions for any two nodes in the tree, the one <strong>to</strong> the “left” (equivalently, theone appearing earlier in an inorder traversal) has a smaller key value than the one<strong>to</strong> the “right.” In contrast, a heap implements a partial order. Given their positions,we can determine the relative order for the key values of two nodes in the heap onlyif one is a descendant of the other.4 The term “heap” is also sometimes used <strong>to</strong> refer <strong>to</strong> a memory pool. See Section 12.3.

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

Saved successfully!

Ooh no, something went wrong!