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

Post on 21-Dec-2015

214 views 0 download

Tags:

Transcript of 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

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

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

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

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

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

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

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

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

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

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?

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.

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

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

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.

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.

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

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.

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.

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.

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))

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.

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.

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.

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

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?

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.

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?

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. . .

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.

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 . . .

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.

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.

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

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

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

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

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

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

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

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

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

Leftist Heaps: Easily Mergeable Trees

6

8

14

7

9

11

16

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

Leftist Heaps: Easily Mergeable Trees

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

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

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.

Leftist Heaps: Easily Mergeable Trees

6

8

9

11

14

11

15

2516

26

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

Hrpl(H) = 2

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

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)

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

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

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

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

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

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.

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

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.

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

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

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

Leftist Heaps: Easily Mergeable Trees

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

H1H

r

H'

r1

r r1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Leftist Heaps: Easily Mergeable Trees

Merge algorithm: . . . producing:

3

10

14

23

218

17

26

6

12

24

33

7

37

18

18

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

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

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

Leftist Heaps: Easily Mergeable Trees

H

What about insert and delete_min?

insert( , ) = ?, v

Leftist Heaps: Easily Mergeable Trees

H

insert( , v ) =

H

merge( , ) v

A one element heap

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

Leftist Heaps: Easily Mergeable Trees

delete_min( ) = ?

H1

H2

Leftist Heaps: Easily Mergeable Trees

delete_min( ) = merge(

H1

H2

H1

H2

, )

T (H) = ? delete_min