Priority Queues Sections 6.1 to 6.5
Vector Representation of Complete Binary Tree v[k].parent == v[(k-1)/2] v[k].lchild == v[2*k+1] v[k].rchild == v[2*k+2] root R l r ll lr rl rr
Vector Representation of Complete Binary Tree v[k].parent == v[(k-1)/2] v[k].lchild == v[2*k+1] v[k].rchild == v[2*k+2] 6 5 4 3 2 1 R
Vector Representation of Complete Binary Tree v[k].parent == v[(k-1)/2] v[k].lchild == v[2*k+1] v[k].rchild == v[2*k+2] 6 5 4 3 2 1 R l
Vector Representation of Complete Binary Tree v[k].parent == v[(k-1)/2] v[k].lchild == v[2*k+1] v[k].rchild == v[2*k+2] 6 5 4 3 2 1 R l r
Vector Representation of Complete Binary Tree v[k].parent == v[(k-1)/2] v[k].lchild == v[2*k+1] v[k].rchild == v[2*k+2] 6 5 4 3 2 1 R l r ll
Vector Representation of Complete Binary Tree v[k].parent == v[(k-1)/2] v[k].lchild == v[2*k+1] v[k].rchild == v[2*k+2] 6 5 4 3 2 1 R l r ll lr
Vector Representation of Complete Binary Tree v[k].parent == v[(k-1)/2] v[k].lchild == v[2*k+1] v[k].rchild == v[2*k+2] 6 5 4 3 2 1 R l r ll lr rl
Vector Representation of Complete Binary Tree v[k].parent == v[(k-1)/2] v[k].lchild == v[2*k+1] v[k].rchild == v[2*k+2] 6 5 4 3 2 1 R l r ll lr rl rr
Priority Queue deleteMax Note: STL provides deleteMax. But book provides deleteMin. We’ll follow the deleteMax strategy.
Priority Queue Usage int main() { assert(Q.top() == 8); Q.pop(); priority_queue<int> Q; Q.push(1); Q.push(4); Q.push(2); Q.push(8); Q.push(5); Q.push(7); assert(Q.size() == 6); assert(Q.top() == 8); Q.pop(); assert(Q.top() == 7); assert(Q.top() == 5); assert(Q.top() == 4); assert(Q.top() == 2); assert(Q.top() == 1); assert(Q.empty()); }
STL Priority Queues Element type with priority <typename T> t Compare & cmp Associative queue operations Void push(t) void pop() T& top() void clear() bool empty()
Priority Queue Implementation Implement as adaptor class around Linked lists Binary Search Trees B-Trees Heaps This is what we’ll study O( logN ) worst case for both insertion and delete operations
Partially Ordered Trees Definition: A partially ordered tree is a tree T such that: There is an order relation >= defined for the vertices of T For any vertex p and any child c of p, p >= c
Partially Ordered Trees Consequences: The largest element in a partially ordered tree is the root No conclusion can be drawn about the order of children
Heaps Definition: A heap is a partially ordered, complete, binary tree The tree is completely filled on all levels except possibly the lowest root 4 3 2 1
Heap example A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 Parent of v[k] = v[(k-1)/2] Left child of v[k] = v[2*k+1] Right child of v[k] = v[2*k + 2] A B C D E F G H I J A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13
The Push-Heap Algorithm Add new data at next leaf Repair upward Repeat Locate parent if tree not partially ordered swap else stop Until partially ordered
The Push Heap Algorithm Add new data at next leaf 1 2 3 4 5 6 7 R l r ll lr rl rr 7 6 5 4 3
The Push Heap Algorithm Add new data at next leaf 1 2 3 4 5 6 7 R l r ll lr rl rr 7 6 5 4 3 8
The Push Heap Algorithm Repeat Locate parent of v[k] = v[(k – 1)/2] if tree not partially ordered swap else stop 1 2 3 4 5 6 R l r ll lr rl rr 7 6 5 4 3 8
The Push Heap Algorithm Repeat Locate parent of v[k] = v[(k – 1)/2] if tree not partially ordered swap else stop 1 2 3 4 5 6 R l r ll lr rl rr 7 6 5 4 3 8
The Push Heap Algorithm Repeat Locate parent of v[k] = v[(k – 1)/2] if tree not partially ordered swap else stop 1 2 3 4 5 6 R l r ll lr rl rr 7 6 8 4 3 5
The Push Heap Algorithm Repeat Locate parent of v[k] = v[(k – 1)/2] if tree not partially ordered swap else stop 1 2 3 4 5 6 R l r ll lr rl rr 7 6 8 4 3 5
The Push Heap Algorithm Repeat Locate parent of v[k] = v[(k – 1)/2] if tree not partially ordered swap else stop 1 2 3 4 5 6 R l r ll lr rl rr 7 6 8 4 3 5
The Push Heap Algorithm Repeat Locate parent of v[k] = v[(k – 1)/2] if tree not partially ordered swap else stop 1 2 3 4 5 6 R l r ll lr rl rr 8 6 7 4 3 5
The Pop-Heap Algorithm Copy last leaf to root Remove last leaf Repeat find the larger child if not partially ordered tree swap else stop Until tree is partially ordered
The Pop Heap Algorithm Copy last leaf to root 1 2 3 4 5 6 R l r ll lr 1 2 3 4 5 6 R l r ll lr rl rr 8 6 7 4 3 5
The Pop Heap Algorithm Copy last leaf to root 1 2 3 4 5 6 R l r ll lr 1 2 3 4 5 6 R l r ll lr rl rr 5 6 7 4 3 5
The Pop Heap Algorithm Remove last leaf 1 2 3 4 5 6 R l r ll lr rl rr 1 2 3 4 5 6 R l r ll lr rl rr 5 6 7 4 3
The Pop Heap Algorithm Repeat find the larger child if tree not partially ordered swap else stop 1 2 3 4 5 6 R l r ll lr rl rr 5 6 7 4 3
The Pop Heap Algorithm Repeat find the larger child if tree not partially ordered swap else stop 1 2 3 4 5 6 R l r ll lr rl rr 5 6 7 4 3
The Pop Heap Algorithm Repeat find the larger child if tree not partially ordered swap else stop 1 2 3 4 5 6 R l r ll lr rl rr 7 6 5 4 3