Priority Queues
E N D
Presentation Transcript
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
Complexity Of Operations Sorted List Insert O(N) Delete O(1) Unsorted List Insert O(1) Delete O(N)
Heaps • Two key properties • Heap shape • Complete binary tree • Value at node • Min Heap: Smaller than or equal to values in subtrees • Max Heap: Larger than or equal to values in subtrees • Example heap • X Y • X Z X Y Z
Heap & Non-heap Examples 5 5 8 5 45 6 45 5 2 2 6 22 6 22 6 22 25 8 45 25 8 45 25 Heaps Non-heaps
2 4 3 6 7 9 3 8 6 Min Heap With 9 Nodes Complete binary tree with 9 nodes that is also a min tree.
9 8 7 6 7 2 6 5 1 Max Heap With 9 Nodes Complete binary tree with 9 nodes that is also a max tree.
Heap Height Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).
9 8 7 6 7 2 6 5 1 A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 1 0 1 2 3 4 5 6 7 8 9 10
9 8 7 6 7 2 6 5 1 7 Inserting An Element Into A Max Heap Complete binary tree with 10 nodes.
Inserting An Element Into A Max Heap 9 8 7 6 7 2 6 New element is 5. 5 1 7 5
Inserting An Element Into A Max Heap 9 8 7 6 2 6 7 New element is 20. 5 1 7 7
Inserting An Element Into A Max Heap 9 8 7 6 2 6 New element is 20. 5 1 7 7 7
Inserting An Element Into A Max Heap 9 7 6 8 2 6 New element is 20. 5 1 7 7 7
Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 New element is 20. 5 1 7 7 7
Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 Complete binary tree with 11 nodes. 5 1 7 7 7
Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 New element is 15. 5 1 7 7 7
Inserting An Element Into A Max Heap 20 9 7 6 2 6 New element is 15. 8 5 1 7 7 8 7
Inserting An Element Into A Max Heap 20 7 15 6 9 2 6 New element is 15. 8 5 1 7 7 8 7
Heap Operations – Insert( X ) • Algorithm • Add X to end of tree • While (X > parent) • Swap X with parent // X bubbles up tree • Complexity • # of swaps proportional to height of tree • O( log(n) )
Insertion Insert(int key) { n++; //heap size+1 i=n; while (key > heap[i/2] && i>1) { heap[i]=heap[i/2]; i=i/2; } heap[i]=key; }
20 7 15 6 9 2 6 8 5 1 7 7 8 7 Removing The Max Element Max element is in the root.
Removing The Max Element 7 15 6 9 2 6 After max element is removed. 8 5 1 7 7 8 7
Removing The Max Element 7 15 6 9 2 6 Heap with 10 nodes. 8 5 1 7 7 8 7 Reinsert 8 into the heap.
Removing The Max Element 7 15 6 9 2 6 Reinsert 8 into the heap. 5 1 7 7 7
Removing The Max Element 15 7 6 9 2 6 Reinsert 8 into the heap. 5 1 7 7 7
Removing The Max Element 15 9 7 6 2 6 8 Reinsert 8 into the heap. 5 1 7 7 7
Removing The Max Element 15 9 7 6 2 6 8 Max element is 15. 5 1 7 7 7
Removing The Max Element 9 7 6 2 6 8 After max element is removed. 5 1 7 7 7
Removing The Max Element 9 7 6 2 6 8 Heap with 9 nodes. 5 1 7 7 7
Removing The Max Element 9 7 6 2 6 8 Reinsert 7. 5 1
Removing The Max Element 9 7 6 2 6 8 Reinsert 7. 5 1
Removing The Max Element 9 8 7 6 7 2 6 Reinsert 7. 5 1
Deletion Delete() { key=heap[1]; data=heap[n--]; i=1; while ((j=2*i)<=n) { if (2*i+1<=n) { if (heap[2*i+1] > heap[2*i]) j=2*i+1; } if (heap[j] > data); { heap[i]=heap[j]; i=j; } else break; } heap[i]=data; return key; }
9 8 7 6 7 2 6 5 1 Complexity Of Remove Max Element Complexity is O(log n).
Binary Search Tree 20 10 40 6 15 30 25 2 8 Only keys are shown.
Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree of x are smaller than that in x. For every node x, all keys in the right subtree of x are greater than that in x.
Binary search trees Two binary search trees representing the same set: • Average depth of a node is O(log N); maximum depth of a node is O(N)
20 10 40 6 15 30 25 2 8 The Operation Insert() 35 Insert a pair whose key is 35.
20 10 40 6 15 30 25 35 2 8 The Operation Insert() 7 Insert a pair whose key is 7.
The Operation Delete() • Four cases: • No element with delete key. • Element is in a leaf. • Element is in a degree 1 node. • Element is in a degree 2 node.
20 10 40 6 15 30 18 25 35 2 8 7 Delete From A Leaf Delete a leaf element. key = 7
20 10 40 6 15 30 18 25 35 2 8 7 Delete From A Degree 1 Node Delete from a degree 1 node. key = 40
20 10 40 6 15 30 18 25 35 2 8 7 Delete From A Degree 1 Node (contd.) Delete from a degree 1 node. key = 15
20 10 40 6 15 30 18 25 35 2 8 7 Delete From A Degree 2 Node Delete from a degree 2 node. key = 10
Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).
Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).
Delete From A Degree 2 Node 20 8 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).
Delete From A Degree 2 Node 20 8 40 6 15 30 18 25 35 2 8 7 Largest key must be in a leaf or degree 1 node.
20 10 40 6 15 30 18 25 35 2 8 7 Another Delete From A Degree 2 Node Delete from a degree 2 node. key = 20
Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.