Presentation is loading. Please wait.

Presentation is loading. Please wait.

6 8 9 11 13 11 Heap: A Special Kind of Tree 15 26 16 25.

Similar presentations


Presentation on theme: "6 8 9 11 13 11 Heap: A Special Kind of Tree 15 26 16 25."— Presentation transcript:

1 6 8 9 11 13 11 Heap: A Special Kind of Tree 15 26 16 25

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

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

4 6 8 9 11 13 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 26 16 25 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. This is a binary heap. Heap: A Special Kind of Tree

5 6 8 9 11 18 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 26 16 25 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. This is not a binary heap. Why not? Heap: A Special Kind of Tree

6 6 8 9 11 18 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 26 16 25 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. This is not a binary heap. Heap: A Special Kind of Tree Heap order violation

7 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 26 16 25 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: A Special Kind of Tree Ok

8 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 26 25 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. This is not a binary heap. Why not? Heap: A Special Kind of Tree

9 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 26 25 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. This is not a binary heap. Heap: A Special Kind of Tree Bottom row not filled left-to-right

10 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 26 25 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: A Special Kind of Tree 16

11 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 25 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: A Special Kind of Tree 16 This is not a binary heap. Why not?

12 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 25 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: A Special Kind of Tree 16 This is not a binary heap. Interior row not filled.

13 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 25 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: A Special Kind of Tree 16 This is a binary heap. 26

14 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 25 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: A Special Kind of Tree 16 26 versus

15 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 25 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: A Special Kind of Tree 16 26 versus This is a heap.

16 6 8 9 11 14 11 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 15 25 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: A Special Kind of Tree 16 26 versus This is a heap. It is not a binary search tree.

17 Heap Order Property At every node X, values in both subtrees (if any) are greater than or equal to the value in X. 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: A Special Kind of Tree versus This is not a heap. It is a binary search tree. 6 2 4 3 1 8 9

18 Leftist Heaps: Easily Mergeable Trees 6 8 9 11 14 11 15 25 16 26 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.

19 Leftist Heaps: Easily Mergeable Trees 6 8 9 11 14 11 15 25 16 26 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. 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.

20 Leftist Heaps: Easily Mergeable Trees 6 8 9 11 14 11 15 25 16 26 npl(X) = min(npl(left(X)), npl(right(X))) + 1 npl(  ) =  1 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

39 Leftist Heaps: Easily Mergeable Trees Although leftist heaps can be very unbalanced, they are easy to merge. 6 0 7 9 11 0 0 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.  –1

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

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

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

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

44 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 r 1 r 2

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

46 Leftist Heaps: Easily Mergeable Trees H r 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 6 8 9 11 14 11 15 25 16 26 rpl(X) = rpl(right(X))) + 1 rpl(  ) =  1 H rpl(H) = 2

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

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

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

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

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

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

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

55 Leftist Heaps: Easily Mergeable Trees Theorem. (Theorem 6.2 in textbook, restated.) If H is a leftist tree, then size(H)  2 – 1, where r = rpl(H) + 1. r Proof of the theorem: By induction on r.

56 Leftist Heaps: Easily Mergeable Trees Theorem. (Theorem 6.2 in textbook, restated.) If H is a leftist tree, then size(H)  2 – 1, where r = rpl(H) + 1. r Proof of the theorem: By induction on r. Basis case. r = 1. In this case the tree has at least one node; i.e., size(H)  1 = 2 – 1. 1

57 Leftist Heaps: Easily Mergeable Trees Theorem. (Theorem 6.2 in textbook, restated.) If H is a leftist tree, then size(H)  2 – 1, where r = rpl(H) + 1. r Proof of the theorem: By induction on r. Inductive step. Consider a leftist tree H' with r + 1 nodes on its right path.

58 Leftist Heaps: Easily Mergeable Trees Theorem. (Theorem 6.2 in textbook, restated.) If H is a leftist tree, then size(H)  2 – 1, where r = rpl(H) + 1. r Proof of the theorem: By induction on r. Inductive step. Consider a leftist tree H' with r + 1 nodes on its right path. (We will show that size(H')  2 – 1.) r+1

59 Leftist Heaps: Easily Mergeable Trees Inductive step. Consider a leftist tree H' with r + 1 nodes on its right path. Then the root has a right subtree with r nodes on the right path, and a left subtree with at least r nodes on its right path — why? H 1 H r H' r 1 r  r 1

60 Leftist Heaps: Easily Mergeable Trees Inductive step. Consider a leftist tree H' with r + 1 nodes on its right path. Then the root has a right subtree with r nodes on the right path, and a left subtree with at least r nodes on its right path (otherwise H' would not be leftist). H 1 H r H' r 1 r  r 1

61 Leftist Heaps: Easily Mergeable Trees Applying the inductive hypothesis, and using the notation in the diagram,... H 1 H r H' r 1 r  r 1

62 Leftist Heaps: Easily Mergeable Trees Applying the inductive hypothesis, and using the notation in the diagram, size(H )  2 – 1, H 1 H r H' r 1 r  r 1 1 r r

63 Leftist Heaps: Easily Mergeable Trees Applying the inductive hypothesis, and using the notation in the diagram, size(H )  2 – 1, and therefore size(H' )  2 – 1. H 1 H r H' r 1 r  r 1 1 r r r + 1

64 Leftist Heaps: Easily Mergeable Trees Applying the inductive hypothesis, and using the notation in the diagram, size(H )  2 – 1, and therefore size(H' )  2 – 1. QED H 1 H r H' r 1 r  r 1 1 r r r + 1

65 Leftist Heaps: Easily Mergeable Trees Merge algorithm, applied to: 3 10 14 23 21 8 17 26 6 12 24 33 18 7 37 18 H 1 H 2

66 Leftist Heaps: Easily Mergeable Trees Merge algorithm: If either of the two heaps is empty, we can return the other. 3 10 14 23 21 8 17 26 6 12 24 33 18 7 37 18 H 1 H 2

67 Leftist Heaps: Easily Mergeable Trees Merge algorithm: In this case, neither is empty. 3 10 14 23 21 8 17 26 6 12 24 33 18 7 37 18 H 1 H 2

68 Leftist Heaps: Easily Mergeable Trees Merge algorithm: In this case, neither is empty, so we compare their roots... 3 10 14 23 21 8 17 26 6 12 24 33 18 7 37 18 H 1 H 2

69 Leftist Heaps: Easily Mergeable Trees Merge algorithm:... and recursively merge the heap with the larger root... 3 10 14 23 21 8 17 26 6 12 24 33 18 7 37 18 H 1 H 2

70 Leftist Heaps: Easily Mergeable Trees Merge algorithm:... with the right subheap of the other heap. 3 10 14 23 21 8 17 26 6 12 24 33 18 7 37 18 H 1 H 2

71 Leftist Heaps: Easily Mergeable Trees Merge algorithm:... producing: 3 10 14 23 21 8 17 26 6 12 24 33 7 37 18 H 1

72 Leftist Heaps: Easily Mergeable Trees Merge algorithm:... The resulting tree satisfies the heap order property... 3 10 14 23 21 8 17 26 6 12 24 33 7 37 18

73 Leftist Heaps: Easily Mergeable Trees Merge algorithm:... but is it leftist?... 3 10 14 23 21 8 17 26 6 12 24 33 7 37 18

74 Leftist Heaps: Easily Mergeable Trees Merge algorithm:... but is it leftist? No, the left subtree has a null path length of 1 while the right subtree has a null path length of 2. 3 10 14 23 21 8 17 26 6 12 24 33 7 37 18

75 Leftist Heaps: Easily Mergeable Trees Merge algorithm:... so the algorithm swaps the left subtree with the right... 3 10 14 23 21 8 17 26 6 12 24 33 7 37 18

76 Leftist Heaps: Easily Mergeable Trees Merge algorithm:... producing: 3 10 14 23 21 8 17 26 6 12 24 33 7 37 18

77 Leftist Heaps: Easily Mergeable Trees Merge algorithm:... The result is then a leftist heap, and the algorithm terminates. 3 10 14 23 21 8 17 26 6 12 24 33 7 37 18

78 T (H, H ) = O(max(r, r )) Leftist Heaps: Easily Mergeable Trees H 1 H 2 r 1 r 2 r = rpl(H ) + 1 ii merge 12 12 where Again, This can be proved by induction on r = max(r, r ) 12

79 T (H, H ) = O(log (N + 1)) Leftist Heaps: Easily Mergeable Trees H 1 H 2 r 1 r 2 merge 12 12 where By the theorem and its corollary, N = max(N, N ) N N 1 2

80 Leftist Heaps: Easily Mergeable Trees H What about insert and delete_min? insert(, ) = ?, v

81 Leftist Heaps: Easily Mergeable Trees H insert(, v ) = H merge(, ) v A one element heap

82 Leftist Heaps: Easily Mergeable Trees H insert(, v ) = H merge(, ) v A one element heap T (H, v) = O(log(N + 1)), where N = size(H). insert

83 Leftist Heaps: Easily Mergeable Trees delete_min() = ? H 1 H 2

84 Leftist Heaps: Easily Mergeable Trees delete_min() = merge( H 1 H 2 H 1 H 2, ) T (H) = ? delete_min


Download ppt "6 8 9 11 13 11 Heap: A Special Kind of Tree 15 26 16 25."

Similar presentations


Ads by Google