1 / 33

Priority Queues

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]. R. root. l. r. ll. lr. rl. rr. 0. 1. 2. 3. 4. 5. 6. Vector Representation of Complete Binary Tree.

nat
Télécharger la présentation

Priority Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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] R root l r ll lr rl rr

  3. 0 1 2 3 4 5 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] R

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

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

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

  7. 0 1 2 3 4 5 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] R l r ll lr

  8. 0 1 2 3 4 5 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] R l r ll lr rl

  9. 0 1 2 3 4 5 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] 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. int main() { 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); Q.pop(); assert(Q.top() == 5); Q.pop(); assert(Q.top() == 4); Q.pop(); assert(Q.top() == 2); Q.pop(); assert(Q.top() == 1); Q.pop(); assert(Q.empty()); } Priority Queue Usage

  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, binarytree • The tree is completely filled on all levels except possibly the lowest 4 root 3 2 1 0

  17. Heap example • 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

  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 7 6 5 4 3

  20. The Push Heap Algorithm • Add new data at next leaf 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 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 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 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 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 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 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 8 6 7 4 3 5

  29. The Pop Heap Algorithm • Copy last leaf to root 5 6 7 4 3 5

  30. The Pop Heap Algorithm • Remove last leaf 5 6 7 4 3

  31. The Pop Heap Algorithm • Repeat • find the larger child • if tree not partially ordered • swap • else • stop 5 6 7 4 3

  32. The Pop Heap Algorithm • Repeat • find the larger child • if tree not partially ordered • swap • else • stop 5 6 7 4 3

  33. The Pop Heap Algorithm • Repeat • find the larger child • if tree not partially ordered • swap • else • stop 7 6 5 4 3

More Related