{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 5-twoup - Priority Queues Each data item is assigned a...

This preview shows pages 1–7. Sign up to view the full content.

Priority Queues Each data item is assigned a numeric priority . Data items with larger priority will be accessed first. Example: Priority-based 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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. Read/write access is provided for all 3 variables.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 28

5-twoup - Priority Queues Each data item is assigned a...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online