Presentation is loading. Please wait.

Presentation is loading. Please wait.

Priority Queues Sections 6.1 to 6.5.

Similar presentations


Presentation on theme: "Priority Queues Sections 6.1 to 6.5."— Presentation transcript:

1 Priority Queues Sections 6.1 to 6.5

2 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

3 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

4 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

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] 6 5 4 3 2 1 R l r

6 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

7 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

8 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

9 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

10 Priority Queue deleteMax Note: STL provides deleteMax.
But book provides deleteMin. We’ll follow the deleteMax strategy.

11 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()); }

12 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()

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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


Download ppt "Priority Queues Sections 6.1 to 6.5."

Similar presentations


Ads by Google