1 / 84

Heap: A Special Kind of Tree

Learn about the properties and structure of a heap, a complete tree with order property at every node.

trevinol
Download Presentation

Heap: A Special Kind of Tree

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. Heap: A Special Kind of Tree 6 8 11 9 15 13 26 25 16 11

  2. Heap: A Special Kind of Tree Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 8 11 9 15 13 26 25 16 11

  3. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 8 11 9 15 13 26 25 16 11

  4. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 This is a binary heap. 8 11 9 15 13 26 25 16 11

  5. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 This is not a binary heap. Why not? 8 11 9 15 18 26 25 16 11

  6. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 This is not a binary heap. 8 11 Heap order violation 9 15 18 26 25 16 11

  7. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 8 11 Ok 9 15 14 26 25 16 11

  8. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 This is not a binary heap. Why not? 8 11 9 15 14 26 25 11

  9. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 This is not a binary heap. 8 11 9 15 14 26 Bottom row not filled left-to-right 25 11

  10. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 8 11 9 15 14 26 25 16 11

  11. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 This is not a binary heap. Why not? 8 11 9 15 14 25 16 11

  12. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 This is not a binary heap. 8 11 Interior row not filled. 9 15 14 25 16 11

  13. Heap: A Special Kind of Tree Heap Structure Property A heap is also a complete tree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 This is a binary heap. 8 11 9 15 14 26 25 16 11

  14. Heap: A Special Kind of Tree Binary Search Tree Property At every node X, values in left subtree are smaller than the value in X, and values in right subtree are larger than the value in X. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. versus 6 8 11 9 15 14 26 25 16 11

  15. Heap: A Special Kind of Tree Binary Search Tree Property At every node X, values in left subtree are smaller than the value in X, and values in right subtree are larger than the value in X. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. versus 6 This is a heap. 8 11 9 15 14 26 25 16 11

  16. Heap: A Special Kind of Tree Binary Search Tree Property At every node X, values in left subtree are smaller than the value in X, and values in right subtree are larger than the value in X. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. versus 6 It is not a binary search tree. This is a heap. 8 11 9 15 14 26 25 16 11

  17. Heap: A Special Kind of Tree Binary Search Tree Property At every node X, values in left subtree are smaller than the value in X, and values in right subtree are larger than the value in X. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. versus 6 It is a binary search tree. This is not a heap. 2 8 4 9 1 3

  18. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property For every node X, the null path length of the left child is at least as large as that of the right child. Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 6 8 11 9 15 14 26 25 16 11

  19. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property For every node X, the null path length of the left child is at least as large as that of the right child. The null path length, npl(X), of any node X is the length of the shortest path from X to a node with zero or one child. 6 8 11 9 15 14 26 25 16 11

  20. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property For every node X, the null path length of the left child is at least as large as that of the right child. npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 6 8 11 9 15 14 26 25 16 11

  21. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 6 8 11 9 15 14 26 25 16 11

  22. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 2 This is a leftist heap. 6 1 1 8 11 1 0 0 0 9 15 14 26 0 0 0 25 16 11

  23. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 2 This is a leftist heap. 6 1 1 8 11 Note that it is also a binary heap. 1 0 0 0 9 15 14 26 0 0 0 25 16 11

  24. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 2 This is a leftist heap. 6 Suppose we remove this node 1 1 8 11 Note that it is also a binary heap. 1 0 0 0 9 15 14 26 0 0 0 25 16 11

  25. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 2 6 1 1 8 11 1 0 0 0 9 15 14 26 0 0 25 11

  26. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 2 6 1 1 8 11 1 0 0 0 9 15 14 26 Is the tree still leftist? 0 0 25 11

  27. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 2 6 1 1 8 11 1 0 0 0 9 15 14 26 0 0 Yes. 25 11

  28. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 2 6 1 1 8 11 1 0 0 0 9 15 14 26 Is it still a binary heap? 0 0 25 11

  29. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 2 6 1 1 8 11 1 0 0 0 No, because . . . 9 15 14 26 0 0 25 11

  30. Leftist Heaps: Easily Mergeable Trees Leftist Structure Property npl(left(X))  npl(right(X)) npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(L) = -1 2 6 1 1 8 11 1 0 0 0 No, because it is not complete. 9 15 14 26 0 0 25 11

  31. Leftist Heaps: Easily Mergeable Trees Complete trees have good balance, but . . . 2 6 1 1 8 11 1 0 0 0 9 15 14 26 0 0 0 25 16 11

  32. Leftist Heaps: Easily Mergeable Trees Complete trees have good balance, but leftist trees can be very unbalanced. 0 6 0 8 0 14 0 16

  33. Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. 0 6 0 8 0 14 0 16

  34. Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. 0 As a trivial case, consider two entirely left-leaning leftist heaps: 0 6 7 0 0 8 9 0 0 14 11 0 16

  35. 7 0 9 0 11 Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. 0 To merge them, . . . 0 6 0 8 0 14 0 16

  36. Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. To merge them, first make the one with the larger root node value the right child of the other. 1 6 0 0 7 8 0 0 14 9 0 0 11 16

  37. Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. In the case shown, the result is already a leftist heap, and the merge is finished. 1 6 0 0 7 8 0 0 14 9 0 0 11 16

  38. 7 0 9 0 11 Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. 0 If we had started with 0 6

  39. Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. 0 . . . then after the first step the tree is not leftist, since the left child of the root has null path length –1, which is less than that of the right subtree. 6 0 –1 7  0 9 0 11

  40. Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. 0 In this case, the merge algorithm swaps the left and right subtrees of the root. 6 0 –1 7  0 9 0 11

  41. Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. 0 In either case, the merge is done in constant time! 6 0 7 0 9 0 11

  42. 7 Leftist Heaps: Easily Mergeable Trees Remember, though, these were special cases (entirely left-leaning trees). 6 8 9 14 11 16

  43. Leftist Heaps: Easily Mergeable Trees In general, the merge can’t be done in constant time, but . . .

  44. Leftist Heaps: Easily Mergeable Trees r r 1 2 In general, the merge can’t be done in constant time, but it can be done in O(log N) time, where N is the number of nodes in the larger of the two heaps. H 1 H 2

  45. r = rpl(H ) + 1 r i i r 1 2 H 1 H 2 Leftist Heaps: Easily Mergeable Trees T (H , H ) = O(max(r , r )) merge 1 2 1 2 where

  46. r H Leftist Heaps: Easily Mergeable Trees The right path length, rpl(X), of any node X is the length of the path following right links to a leaf.

  47. Leftist Heaps: Easily Mergeable Trees rpl(X) = rpl(right(X))) + 1 rpl(L) = -1 rpl(H) = 2 H 6 8 11 9 15 14 26 25 16 11

  48. Theorem. (Theorem 6.2 in textbook, restated.) If H is a leftist tree, then size(H)  2 – 1, where r = rpl(H) + 1. r r H Leftist Heaps: Easily Mergeable Trees

  49. Theorem. (Theorem 6.2 in textbook, restated.) If H is a leftist tree, then size(H)  2 – 1, where r = rpl(H) + 1. r r H Leftist Heaps: Easily Mergeable Trees N = size(H) Corollary. The number of nodes on the right path of a leftist tree with N nodes is at most log(N + 1).

  50. Theorem. (Theorem 6.2 in textbook, restated.) If H is a leftist tree, then size(H)  2 – 1, where r = rpl(H) + 1. r r H Leftist Heaps: Easily Mergeable Trees Proof of the Corollary: By the theorem, N 2 – 1, thus . . . N = size(H) Corollary. The number of nodes on the right path of a leftist tree with N nodes is at most log(N + 1). r

More Related