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

84
6 8 9 11 13 11 Heap: A Special Kind of Tree 15 26 16 25
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

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

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

6

8

9

11

13

11

Heap: A Special Kind of Tree

1526

16 25

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

6

8

9

11

13

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

1526

16 25

Heap: A Special Kind of Tree

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

6

8

9

11

13

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

1526

16 25

Heap Structure PropertyA 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

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

6

8

9

11

13

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

1526

16 25

Heap Structure PropertyA heap is also a completetree. “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

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

6

8

9

11

18

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

1526

16 25

Heap Structure PropertyA heap is also a completetree. “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

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

6

8

9

11

18

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

1526

16 25

Heap Structure PropertyA heap is also a completetree. “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 orderviolation

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

1526

16 25

Heap Structure PropertyA heap is also a completetree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right.

Heap: A Special Kind of Tree

Ok

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

1526

25

Heap Structure PropertyA heap is also a completetree. “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

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

1526

25

Heap Structure PropertyA heap is also a completetree. “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 rownot filledleft-to-right

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

1526

25

Heap Structure PropertyA heap is also a completetree. “Complete” means completely filled, except perhaps for the bottom row, which is filled from left to right.

Heap: A Special Kind of Tree

16

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

15

25

Heap Structure PropertyA heap is also a completetree. “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?

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

15

25

Heap Structure PropertyA heap is also a completetree. “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 notfilled.

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

15

25

Heap Structure PropertyA heap is also a completetree. “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

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

15

25

Binary Search Tree PropertyAt every node X, values inleft subtree are smaller thanthe value in X, and values in right subtree are largerthan the value in X.

Heap: A Special Kind of Tree

16

26

versus

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

15

25

Binary Search Tree PropertyAt every node X, values inleft subtree are smaller thanthe value in X, and values in right subtree are largerthan the value in X.

Heap: A Special Kind of Tree

16

26

versus

This is a heap.

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

6

8

9

11

14

11

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

15

25

Binary Search Tree PropertyAt every node X, values inleft subtree are smaller thanthe value in X, and values in right subtree are largerthan the value in X.

Heap: A Special Kind of Tree

16

26

versus

This is a heap. It is not abinary searchtree.

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

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

Binary Search Tree PropertyAt every node X, values inleft subtree are smaller thanthe value in X, and values in right subtree are largerthan the value in X.

Heap: A Special Kind of Tree

versus

This is nota heap.

It is abinary searchtree.

6

2

4

3

1

8

9

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

2516

26

Leftist Structure PropertyFor every node X, the nullpath length of the left childis at least as large as that of the right child.

Heap Order PropertyAt every node X, values inboth subtrees (if any)are greater than or equal tothe value in X.

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

2516

26

The null path length,npl(X), of any node X isthe length of the shortestpath from X to a node withzero or one child.

Leftist Structure PropertyFor every node X, the nullpath length of the left childis at least as large as that of the right child.

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

2516

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure PropertyFor every node X, the nullpath length of the left childis at least as large as that of the right child.

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

2516

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

2516

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

0 0

00 0

1 0

1 1

2This is a leftist heap.

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

2516

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

0 0

00 0

1 0

1 1

2This is a leftist heap.

Note that itis also abinary heap.

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

2516

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

0 0

00 0

1 0

1 1

2This is a leftist heap.Suppose we

remove thisnode Note that it

is also abinary heap.

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

25

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

0 0

00

1 0

1 1

2

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

25

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

0 0

00

1 0

1 1

2

Is the tree stillleftist?

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

25

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

0 0

00

1 0

1 1

2

Yes.

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

25

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

0 0

00

1 0

1 1

2

Is it stilla binaryheap?

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

25

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

0 0

00

1 0

1 1

2

No,because. . .

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

25

26

npl(X) = min(npl(left(X)), npl(right(X))) + 1npl() = 1

Leftist Structure Property

npl(left(X)) npl(right(X))

0 0

00

1 0

1 1

2

No,becauseit is notcomplete.

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

25

26

0 0

00

1 0

1 1

2

16

0

Complete trees have good balance,but . . .

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

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.

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

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.

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

As a trivial case,consider two entirelyleft-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

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

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

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

To merge them,first make the onewith the largerroot node valuethe rightchildof theother.

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

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

In the case shown,the result is alreadya leftist heap,and the mergeis 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

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

Leftist Heaps: Easily Mergeable Trees

Although leftist heaps can be very unbalanced, they are easy to merge.

60If we had started with

7

9

11

0

0

0

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

Leftist Heaps: Easily Mergeable Trees

Although leftist heaps can be very unbalanced, they are easy to merge.

60

7

9

11

0

0

0

. . . then after the firststep the tree is not leftist, since the left child of the root hasnull path length–1, which is less thanthat of theright subtree.

–1

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

In this case, the mergealgorithm swapsthe left and rightsubtrees of theroot.

Leftist Heaps: Easily Mergeable Trees

Although leftist heaps can be very unbalanced, they are easy to merge.

60

7

9

11

0

0

0

–1

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

In either case, the mergeis done inconstant time!

Leftist Heaps: Easily Mergeable Trees

Although leftist heaps can be very unbalanced, they are easy to merge.

60

7

9

11

0

0

0

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

Remember, though, thesewere special cases(entirely left-leaningtrees).

Leftist Heaps: Easily Mergeable Trees

6

8

14

7

9

11

16

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

In general, the mergecan’t be done in constant time,but . . .

Leftist Heaps: Easily Mergeable Trees

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

Leftist Heaps: Easily Mergeable Trees

In general, the mergecan’t be done in constant time,but it can be donein O(log N) time,where N isthe number ofnodes in thelarger of the twoheaps.

H1

H2

r1 r

2

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

T (H , H ) =

O(max(r , r ))

Leftist Heaps: Easily Mergeable Trees

H1

H2

r1 r

2

r = rpl(H ) + 1ii

merge

1 2

1 2

where

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

Leftist Heaps: Easily Mergeable Trees

H

rThe right path length, rpl(X), of any node X is the length of the pathfollowing right links to a leaf.

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

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

2516

26

rpl(X) = rpl(right(X))) + 1rpl() = 1

Hrpl(H) = 2

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

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

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

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 ofnodes on the rightpath of a leftist tree with N nodesis at most log(N + 1).

N = size(H)

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

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 ofnodes on the rightpath of a leftist tree with N nodesis at most log(N + 1).

N = size(H)Proof of the Corollary:By the theorem, N 2 – 1, thus . . .

r

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

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 ofnodes on the rightpath of a leftist tree with N nodesis 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

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

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 ofnodes on the rightpath of a leftist tree with N nodesis 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

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

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 ofnodes on the rightpath of a leftist tree with N nodesis 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

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

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 ofnodes on the rightpath of a leftist tree with N nodesis 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

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

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.

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

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

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

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' withr + 1 nodes on its right path.

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

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' withr + 1 nodes on its right path. (We will showthat size(H') 2 – 1.)r+1

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

Leftist Heaps: Easily Mergeable Trees

Inductive step. Consider a leftist tree H' withr + 1 nodes on its right path. Then the roothas a right subtree with r nodes on the rightpath, and a left subtree with at least r nodes on its right path — why?

H1H

r

H'

r1

r r1

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

Leftist Heaps: Easily Mergeable Trees

Inductive step. Consider a leftist tree H' withr + 1 nodes on its right path. Then the roothas a right subtree with r nodes on the rightpath, and a left subtree with at least r nodes on its right path (otherwise H' would not be leftist).

H1H

r

H'

r1

r r1

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

Leftist Heaps: Easily Mergeable Trees

Applying the inductive hypothesis, and using thenotation in the diagram, . . .

H1H

r

H'

r1

r r1

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

Leftist Heaps: Easily Mergeable Trees

Applying the inductive hypothesis, and using thenotation in the diagram, size(H ) 2 – 1, size(H) 2 – 1,

H1H

r

H'

r1

r r1

1 r

r

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

Leftist Heaps: Easily Mergeable Trees

Applying the inductive hypothesis, and using thenotation in the diagram, size(H ) 2 – 1, size(H) 2 – 1,and therefore size(H' ) 2 – 1.

H1H

r

H'

r1

r r1

1 r

r

r + 1

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

Leftist Heaps: Easily Mergeable Trees

Applying the inductive hypothesis, and using thenotation in the diagram, size(H ) 2 – 1, size(H) 2 – 1,and therefore size(H' ) 2 – 1. QED

H1H

r

H'

r1

r r1

1 r

r

r + 1

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

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

H1

H2

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

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

H1

H2

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

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

H1

H2

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

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

H1

H2

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

Leftist Heaps: Easily Mergeable Trees

Merge algorithm: . . . and recursively merge the heap with the largerroot . . .

3

10

14

23

21

8

17

26

6

12

24

33

18

7

37 18

H1

H2

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

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

H1

H2

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

Leftist Heaps: Easily Mergeable Trees

Merge algorithm: . . . producing:

3

10

14

23

21 8

17

26

6

12

24

33

7

37

18

H1

18

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

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

18

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

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

18

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

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

18

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

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

18

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

Leftist Heaps: Easily Mergeable Trees

Merge algorithm: . . . producing:

3

10

14

23

218

17

26

6

12

24

33

7

37

18

18

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

Leftist Heaps: Easily Mergeable Trees

Merge algorithm: . . . The result is then a leftist heap, and the algorithm terminates. 3

10

14

23

218

17

26

6

12

24

33

7

37

18

18

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

T (H , H ) =

O(max(r , r ))

Leftist Heaps: Easily Mergeable Trees

H1

H2

r1 r

2

r = rpl(H ) + 1ii

merge

1 2

1 2

where

Again,

This can be proved by induction on r = max(r , r )1 2

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

T (H , H ) =

O(log (N + 1))

Leftist Heaps: Easily Mergeable Trees

H1

H2

r1 r

2

merge

1 2

1 2

where

By the theorem and its corollary,

N = max(N , N )

N

N

1

2

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

Leftist Heaps: Easily Mergeable Trees

H

What about insert and delete_min?

insert( , ) = ?, v

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

Leftist Heaps: Easily Mergeable Trees

H

insert( , v ) =

H

merge( , ) v

A one element heap

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

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

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

Leftist Heaps: Easily Mergeable Trees

delete_min( ) = ?

H1

H2

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

Leftist Heaps: Easily Mergeable Trees

delete_min( ) = merge(

H1

H2

H1

H2

, )

T (H) = ? delete_min