1 / 23

CSC 2300 Data Structures & Algorithms

This chapter covers priority queues, array representation of binary heaps, and binary heap operations like insert and deleteMin. It also explains the buildHeap operation and analyzes the time complexity of various operations. Examples and illustrations are provided for better understanding.

hagaman
Download Presentation

CSC 2300 Data Structures & Algorithms

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. CSC 2300Data 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 11th – 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 2h+1 – 1 nodes, the sum of the heights of the nodes is 2h+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, 22 nodes at height h – 2, and in general 2i nodes at height h – i. • The sum of the heights of all the nodes is therefore S = h + 2(h – 1) + 22(h – 2) + 23(h – 3) + … + 2h–1 (1) • How do we find S? • Multiply equation by 2: 2S = 2h + 22(h – 1) + 23(h – 2) + 24(h – 3) + … + 2h (1) • Subtract and get S = –h + 2 + 22 + 23 + … + 2h–1 + 2h • 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.

More Related