Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues.

Similar presentations


Presentation on theme: "CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues."— Presentation transcript:

1 CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues

2 Today – Priority Queues Priority queues Array representation of binary heaps Binary heap operations – insert and deleteMin Build heap operation Why buildHeap takes linear time? Brief review – binary search tree, AVL tree, splay tree, heap

3 Binary Heaps

4 Array Implementation

5 Example – Insert 14

6 Example – deleteMin

7 deleteMin

8 percolateDown

9 Time Behavior What is the worst-case time for deleteMin? What is the worst-case time for insert? What is the average-case time for insert?

10 Examples in Class We may illustrate the idea with a few examples: http://window.terratron.com/~sosman/computers/java/heap/

11 Build Heap How much time is required?

12 buildHeap

13 Example percolateDown(7):

14 Example percolateDown(6) and then percolateDown(5):

15 Example percolateDown(4) and then percolateDown(3):

16 Example percolateDown(2) and then percolateDown(1):

17 buildHeap We start with an unordered tree. The seven remaining trees show the result of each of the seven percolateDowns. Each dash line corresponds to two comparisons: one to find the smaller child and one to compare the smaller child with the node. Note that there are only 10 dashed lines in the entire algorithm (there could have been an 11 th – where?) corresponding to 20 comparisons. How do we bound the running time of buildHeap? How do we bound the number of dashed lines? Why do we compute the sum of the heights of all the nodes? What is an upper bound for the sum?

18 Theorem 6.1 For the perfect binary tree of height h containing 2 h+1 – 1 nodes, the sum of the heights of the nodes is 2 h+1 – 1 – (h+1). It is easy to see that this tree consists of one node at height h, two nodes at height h – 1, 2 2 nodes at height h – 2, and in general 2 i nodes at height h – i. The sum of the heights of all the nodes is therefore S = h + 2(h – 1) + 2 2 (h – 2) + 2 3 (h – 3) + … + 2 h–1 (1) How do we find S? Multiply equation by 2: 2S = 2h + 2 2 (h – 1) + 2 3 (h – 2) + 2 4 (h – 3) + … + 2 h (1) Subtract and get S = –h + 2 + 2 2 + 2 3 + … + 2 h–1 + 2 h What does S equal?

19 Exact Value of S Sum of the heights equals N – b(N), where b(N) is the number of 1’s in the binary representation of N. We may prove this result by induction. An illustration: N = 10, or 1010 in binary. Sum of height = 10 – 2 = 8. Add one node. N = 11, or 1011 in binary. Sum of height = 11 – 3 = 8. Add one node. N = 12, or 1100 in binary. Sum of height = 12 – 2 = 10. Add one node. N = 13, or 1101 in binary. Sum of height = 13 – 3 = 10.

20 Binary Search Tree Property that turns a binary tree into a binary search tree Which is the appropriate node traversal scheme? Operation contains Operation findMin Operation findMax Operation insert Operation remove

21 AVL Tree Property that turns a binary search tree into an AVL tree Balance condition Single rotation Double rotation Operations contains, findMin, findMax, insert, remove. Time?

22 Splay Tree Is it a binary search tree? What does a splay tree guarantee? Basic idea of splay tree? Zig-zig and zig-zag:

23 Heap Is it a binary search tree? What is a complete binary tree? Operations insert, deleteMin, and buildHeap. Operations percolateUp and percolateDown.


Download ppt "CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues."

Similar presentations


Ads by Google