CSE 326 Data Structures Priority Queues Binary Heaps
CSE 326: Data Structures Priority Queues & Binary Heaps Ben Lerner Summer 2007 1
Administrivia • • • Due tonight: Project #1 Released today: Project #2 a Due Wednesday: HW #1 Released Wednesday: HW #2 Office hours today and tomorrow 2
A new problem • Application: Find the smallest (i. e. highest priority) item quickly – Operating system needs to schedule jobs by importance – Hospitals needs to treat patients by severity of problem 3
Priority Queue ADT • Security line at the airport ? ? ? • Printer queues ? ? ? • operations: insert, delete. Min insert 6 2 15 23 12 18 45 3 7 delete. Min 4
Priority Queue ADT 1. PQueue data : collection of data with priority 2. PQueue operations – insert – delete. Min (also: create, destroy, is_empty) 3. PQueue property: for two elements in the queue, x and y, if x has a lower priority 5 value than y, x will be deleted before y
Applications of the Priority Q • Select print jobs in order of decreasing length • Forward packets on network routers in order of urgency • Select most frequent symbols for compression • Sort numbers, picking minimum first • Anything greedy 6
Implementations of Priority Queue ADT insert delete. Min Unsorted list (Array) O(1) O(N) Unsorted list (Linked-List) O(1) O(N) Sorted list (Array) O(N) O(1) Sorted list (Linked-List) O(N) O(1) Binary Search Tree (BST) Binary heap Don’t worry O(log N) 7
Tree Review Tree T A root(T): leaves(T): B C children(B): parent(H): D E F siblings(E): G H ancestors(F): I descendents(G): subtree(C): J K L M N 8
More Tree Terminology A depth(T): height(G): degree(B): Tree T B D E C F branching factor(T): G H J K I L M N 9
Some More Tree Terminology T is binary if … A Tree T B C T is n-ary if … D T is complete if … H E I How deep is a complete tree with n nodes? F G J 10
Brief interlude: Some Definitions: A Perfect binary tree – A binary tree with all leaf nodes at the same depth. All internal nodes have 2 children. height h 2 h+1 – 1 nodes 2 h – 1 non-leaves 2 h leaves 11 5 21 2 1 16 9 3 7 10 13 25 19 22 30 11
Binary Heap Properties 1. Structure Property 2. Ordering Property 12
Heap Structure Property • A binary heap is a complete binary tree. Complete binary tree – binary tree that is completely filled, with the possible exception of the bottom level, which is filled left to right. Examples: 13
Representing Complete Binary Trees in an Array 1 2 4 8 H From node i: 3 B 5 D 9 A 10 I 6 E J 11 K 12 F C 7 G L left child: right child: parent: implicit (array) implementation: 0 A B C D E F G H I J K L 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Why better than tree with pointers? 15
Heap Order Property Heap order property: For every non-root node X, the value in the parent of X is less than (or equal to) the value in X. 10 10 20 30 20 80 15 40 50 80 60 85 99 700 not a heap 16
Heap order property • A heap provides limited ordering information • Each path is sorted, but the subtrees are not sorted relative to each other – A binary heap is NOT a binary search tree -1 2 6 4 7 0 5 1 1 0 These are all valid binary heaps (minimum) 2 8 6 4 7 17
Find. Min and Delete. Min • Find. Min: Easy! – Return root value A[1] – Run time = ? • Delete. Min: – Delete (and return) value at 2 4 7 3 5 8 9 11 9 6 10 root node • Insert(val): – Insert value into heap 18
Delete. Min • Delete (and return) value at root node 4 7 3 5 8 11 9 6 10 19 9
Maintain the Structure Property • We now have a “Hole” at the root – Need to fill the hole with another value • When we get done, the tree will have one less node and must still be complete 4 7 3 5 8 9 11 9 6 10 20
Maintain the Heap Property • The last value has lost its node 10 – we need to find a new place for it • We can do a simple insertion sort operation to find the correct place for it in the tree 4 7 3 5 8 9 11 9 6 21
Delete. Min: Percolate Down 10 ? 4 7 3 5 11 9 6 3 8 ? 10 3 4 4 9 7 11 9 6 5 8 9 7 8 5 10 9 11 9 6 • Keep comparing with children A[2 i] and A[2 i + 1] • Copy smaller child up and go down one level • Done if both children are item or reached a leaf node • What is the run time? 22
Delete. Min: Run Time Analysis • Run time is O(depth of heap) • A heap is a complete binary tree • Depth of a complete binary tree of N nodes? – height = log 2(N) - 1 • Run time of Delete. Min is O(log N) 23
Insert • Add a value to the tree • Structure and heap order properties must still be correct when we are done 2 3 4 7 8 5 10 9 11 9 6 24
Maintain the Structure Property • The only valid place for a new node in a complete tree is at the end of the array • We need to decide on the correct value for the new node, and adjust the heap accordingly 2 3 4 7 8 5 10 9 11 9 6 25
Maintain the Heap Property • The new value goes where? • We can do a simple insertion sort operation to find the correct place for it in the tree 3 4 7 8 5 10 9 11 9 6 2 26
Insert: Percolate Up 3 4 7 8 5 11 9 6 2 ? 3 4 10 ? 9 2 8 7 11 9 6 3 5 10 ? 8 9 2 7 11 9 6 4 10 5 • Start at last node and keep comparing with parent A[i/2] • If parent larger, copy parent down and go up one level • Done if parent item or reached top node A[1] • Run time? 27 9
Insert: Done 2 3 7 8 4 11 9 6 10 9 5 • Run time? 28
Insert: 16, 32, 4, 69, 105, 43, 2 0 1 2 3 4 5 6 7 8 29
• More Priority Queue Operations decrease. Key – given a pointer to an object in the queue, reduce its priority value Solution: change priority and ______________ • increase. Key – given a pointer to an object in the queue, increase its priority value Why do we need a pointer? Why not simply data value? Solution: change priority and _______________ 30
More Heap Operations decrease. Key(obj. Ptr, amount): raise the priority of a object, percolate up increase. Key(obj. Ptr, amount): lower the priority of a object, percolate down remove(obj. Ptr): remove a object, move to top, them delete. 1) decrease. Key(obj. Ptr, ) 2) delete. Min() Worst case Running time for all of these: Find. Max? Expand. Heap – when heap fills, copy into new space. 31
Build Heap Build. Heap { for i = N/2 to 1 by – 1 Perc. Down(i, A[i]) } N=11 11 2 5 4 5 9 8 4 2 7 6 9 10 6 10 8 3 11 3 5 7 9 9 2 7 6 10 3 8 9 4 32
Build Heap 11 11 5 2 9 7 6 10 3 8 4 5 9 2 9 7 6 8 3 10 9 4 33
Build Heap 11 2 2 5 9 7 6 8 3 10 4 3 9 5 8 4 10 9 9 7 6 11 34
Analysis of Build Heap • Assume N = 2 K – 1 – – Level 1: k -1 steps for 1 item Level 2: k - 2 steps of 2 items Level 3: k - 3 steps for 4 items Level i : k - i steps for 2 i-1 items 35
One More Operation • Merge two heaps. Ideas? 36
- Slides: 36