This preview shows pages 1–7. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Priority Queues Each data item is assigned a numeric priority . Data items with larger priority will be accessed first. Example: Prioritybased scheduling. 1 The PriorityQueue ADT Precondition: true . Postcondition: Constructs an empty PriorityQueue . PriorityQueue () Precondition: p is a Number . Postcondition: Adds x to the set with priority p . PriorityQueue.Put ( x, p ) 2 Precondition: The represented set is not empty. Postcondition: Returns the maximum priority of any item in the set. PriorityQueue.MaxPriority () Precondition: The represented set is not empty. Postcondition: An item with maximum priority is removed from the set and returned. PriorityQueue.RemoveMax () Precondition: true . Postcondition: Returns the number of items in the set. PriorityQueue.Size () 3 The Keyed Data Structure Representation Variables: key (readable) data (readable) Interpretation: key and data are associated with each other. Structural Invariant: true . 4 Implementations Using Arrays Unsorted Array Put is similar to ExpandableArrayStack.Push amortized running time in O (1) . RemoveMax requires finding the largest key and removing it ( n ) running time. Sorted Array RemoveMax removes the last key (1) running time. Put requires finding the insertion point and making room ( n ) time in the worst case. 5 Heaps Defn: A heap is a tree whose nodes contain Keyed items such that if the tree is nonempty, the maximum key is stored at the root; and each child is a heap. We will use only binary trees. 6 53 32 48 53 27 89 24 13 17 32 7 The RemoveMax Operation If the heap is nonempty, the maximum key is at the root. When the root is removed, two (possibly empty) heaps remain. We will define an internal function Merge to combine two heaps into one. Merge can also be used to implement Put . 8 The Merge Function We wish to construct from two given heaps h 1 and h 2 a new heap containing all of the elements in either h 1 or h 2 . If either h 1 or h 2 is empty, we can simply return the other. Otherwise, the root with larger key is the new root. Its children, plus the other heap, contain the elements that must be included in the new children. We can merge two of these heaps. Which two? 9 A Simple Strategy Let L be the heap with the larger root, and let S be the other heap. Always merge the right child of L with S . Make the result the new right child, and make the left child of L the new left child. 10 BinaryTreeNode Representation Variables: root : the Keyed item stored in the node; leftChild : the BinaryTreeNode representing the root of the left child; rightChild : the BinaryTreeNode representing the root of the right child....
View
Full
Document
This note was uploaded on 09/06/2009 for the course CIS 11274 taught by Professor Howell during the Spring '09 term at Kansas State University.
 Spring '09
 Howell

Click to edit the document details