B i n a r y h e a p s ( c h a p t e r s 2 0 . - - PowerPoint PPT Presentation
B i n a r y h e a p s ( c h a p t e r s 2 0 . - - PowerPoint PPT Presentation
B i n a r y h e a p s ( c h a p t e r s 2 0 . 3 2 0 . 5 ) L e f t i s t h e a p s B i n a r y h e a p s a r e a r r a y s ! A b i n a r y h e a p i s r e a l l y i
B i n a r y h e a p s a r e a r r a y s !
A b i n a r y h e a p i s r e a l l y i m p l e m e n t e d u s i n g a n a r r a y !
8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 7 8 9 10 11 12 3 4 5 6 8 18 29 20 28 39 66 37 26 76 32 74 89
P
- s
s i b l e b e c a u s e
- f
c
- m
p l e t e n e s s p r
- p
e r t y
C h i l d p
- s
i t i
- n
s
8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 7 8 9 10 11 12 3 4 5 6 8 18 29 20 28 39 66 37 26 76 32 74 89
Parent
- L. Child
- R. Child
Tie l e f t c h i l d
- f
n
- d
e i i s a t i n d e x 2 i + 1 i n t h e a r r a y . . . . . . t h e r i g h t c h i l d i s a t i n d e x 2 i + 2
C h i l d p
- s
i t i
- n
s
8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 7 8 9 10 11 12 3 4 5 6 8 18 29 20 28 39 66 37 26 76 32 74 89
Parent
- L. Child
- R. Child
Tie l e f t c h i l d
- f
n
- d
e i i s a t i n d e x 2 i + 1 i n t h e a r r a y . . . . . . t h e r i g h t c h i l d i s a t i n d e x 2 i + 2
C h i l d p
- s
i t i
- n
s
8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 7 8 9 10 11 12 3 4 5 6 8 18 29 20 28 39 66 37 26 76 32 74 89
Tie l e f t c h i l d
- f
n
- d
e i i s a t i n d e x 2 i + 1 i n t h e a r r a y . . . . . . t h e r i g h t c h i l d i s a t i n d e x 2 i + 2
Parent
- L. Child
- R. Child
P a r e n t p
- s
i t i
- n
8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 7 8 9 10 11 12 3 4 5 6 8 18 29 20 28 39 66 37 26 76 32 74 89
Tie p a r e n t
- f
n
- d
e i i s a t i n d e x ( i
- 1
) / 2
Parent Child
R e m i n d e r : i n s e r t i n g i n t
- a
b i n a r y h e a p
T
- i
n s e r t a n e l e m e n t i n t
- a
b i n a r y h e a p :
- A
d d t h e n e w e l e m e n t a t t h e e n d
- f
t h e h e a p
- S
i f t t h e e l e m e n t u p : w h i l e t h e e l e m e n t i s l e s s t h a n i t s p a r e n t , s w a p i t w i t h i t s p a r e n t
W e c a n d
- e
x a c t l y t h e s a m e t h i n g f
- r
a b i n a r y h e a p r e p r e s e n t e d a s a n a r r a y !
I n s e r t i n g i n t
- a
b i n a r y h e a p
S t e p 1 : a d d t h e n e w e l e m e n t t
- t
h e e n d
- f
t h e a r r a y , s e t c h i l d t
- i
t s i n d e x
6 18 29 37 26 76 32 74 89 20 28 39 66 1 2 7 8 9 10 11 12 3 4 5 6 6 18 29 20 28 39 66 37 26 76 32 74 89 13 8
Child
8
I n s e r t i n g i n t
- a
b i n a r y h e a p
S t e p 2 : c
- m
p u t e p a r e n t = ( c h i l d
- 1
) / 2
6 18 29 37 26 76 32 74 89 20 28 39 66 1 2 7 8 9 10 11 12 3 4 5 6 6 18 29 20 28 39 66 37 26 76 32 74 89 13 8
Child Parent
8
I n s e r t i n g i n t
- a
b i n a r y h e a p
S t e p 3 : i f a r r a y [ p a r e n t ] > a r r a y [ c h i l d ] , s w a p t h e m
6 18 29 37 26 76 32 74 89 20 28 39 8 1 2 7 8 9 10 11 12 3 4 5 6 6 18 29 20 28 39 8 37 26 76 32 74 89 13 66
Child Parent
66
I n s e r t i n g i n t
- a
b i n a r y h e a p
S t e p 4 : s e t c h i l d = p a r e n t , p a r e n t = ( c h i l d 1 ) / 2 – , a n d r e p e a t
6 18 29 37 26 76 32 74 89 20 28 39 8 1 2 7 8 9 10 11 12 3 4 5 6 6 18 29 20 28 39 8 37 26 76 32 74 89 13 66 66
Child Parent
I n s e r t i n g i n t
- a
b i n a r y h e a p
S t e p 4 : s e t c h i l d = p a r e n t , p a r e n t = ( c h i l d 1 ) / 2 – , a n d r e p e a t
6 18 8 37 26 76 32 74 89 20 28 39 29 1 2 7 8 9 10 11 12 3 4 5 6 6 18 8 20 28 39 29 37 26 76 32 74 89 13 66 66
Child Parent
B i n a r y h e a p s a s a r r a y s
B i n a r y h e a p s a r e “ m
- r
a l l y ” t r e e s
- Tii
s i s h
- w
w e v i e w t h e m w h e n w e d e s i g n t h e h e a p a l g
- r
i t h m s
B u t w e i m p l e m e n t t h e t r e e a s a n a r r a y
- Tie
a c t u a l i m p l e m e n t a t i
- n
t r a n s l a t e s t h e s e t r e e c
- n
c e p t s t
- u
s e a r r a y s
I n t h e r e s t
- f
t h e l e c t u r e , w e w i l l
- n
l y s h
- w
t h e h e a p s a s t r e e s
- B
u t y
- u
s h
- u
l d h a v e t h e “ a r r a y v i e w ” i n y
- u
r h e a d w h e n l
- k
i n g a t t h e s e t r e e s
B u i l d i n g a h e a p
O n e m
- r
e
- p
e r a t i
- n
, b u i l d h e a p
- T
a k e s a n a r b i t r a r y a r r a y a n d m a k e s i t i n t
- a
h e a p
- I
n
- p
l a c e : m
- v
e s t h e e l e m e n t s a r
- u
n d t
- m
a k e t h e h e a p p r
- p
e r t y h
- l
d
I d e a :
- H
e a p p r
- p
e r t y : e a c h e l e m e n t m u s t b e l e s s t h a n i t s c h i l d r e n
- I
f a n
- d
e b r e a k s t h i s p r
- p
e r t y , w e c a n fj x i t b y s i f t i n g d
- w
n
- S
- s
i m p l y l
- p
i n g t h r
- u
g h t h e a r r a y a n d s i f t i n g d
- w
n e a c h e l e m e n t i n t u r n
- u
g h t t
- fj
x t h e i n v a r i a n t
- B
u t w h e n w e s i f t a n e l e m e n t d
- w
n , i t s c h i l d r e n m u s t a l r e a d y h a v e t h e h e a p p r
- p
e r t y (
- t
h e r w i s e t h e s i f t i n g d
- e
s n ' t w
- r
k )
- T
- e
n s u r e t h i s , l
- p
t h r
- u
g h t h e a r r a y i n r e v e r s e
B u i l d i n g a h e a p
G
- t
h r
- u
g h e l e m e n t s i n r e v e r s e
- r
d e r , s i f t i n g e a c h d
- w
n
25 13 28 37 32 29 6 20 26 18 28 31 18
B u i l d i n g a h e a p
L e a v e s n e v e r n e e d s i f t i n g d
- w
n !
25 13 28 37 32 29 6 20 26 18 28 31 18
B u i l d i n g a h e a p
2 9 i s g r e a t e r t h a n 1 8 s
- n
e e d s s w a p p i n g
25 13 28 37 32 18 6 20 26 18 28 31 29
B u i l d i n g a h e a p
3 2 i s g r e a t e r t h a n 1 8 s
- n
e e d s s w a p p i n g
25 13 28 37 18 18 6 20 26 32 28 31 29
B u i l d i n g a h e a p
3 7 i s g r e a t e r t h a n 2 s
- n
e e d s s w a p p i n g
25 13 28 20 18 18 6 37 26 32 28 31 29
B u i l d i n g a h e a p
2 8 i s g r e a t e r t h a n 6 s
- n
e e d s s w a p p i n g
25 13 6 20 18 18 28 37 26 32 28 31 29
B u i l d h e a p c
- m
p l e x i t y
Y
- u
w
- u
l d e x p e c t O ( n l
- g
n ) c
- m
p l e x i t y :
- n
“ s i f t d
- w
n ”
- p
e r a t i
- n
s
- e
a c h s i f t d
- w
n h a s O ( l
- g
n ) c
- m
p l e x i t y
A c t u a l l y , i t ' s O ( n ) ! S e e b
- k
2 . 3 .
- (
R
- u
g h r e a s
- n
: s i f t i n g d
- w
n i s m
- s
t e x p e n s i v e f
- r
e l e m e n t s n e a r t h e r
- t
- f
t h e t r e e , b u t t h e v a s t m a j
- r
i t y
- f
e l e m e n t s a r e n e a r t h e l e a v e s )
H e a p s
- r
t
T
- s
- r
t a l i s t u s i n g a h e a p :
- s
t a r t w i t h a n e m p t y h e a p
- a
d d a l l t h e l i s t e l e m e n t s i n t u r n
- r
e p e a t e d l y fj n d a n d r e m
- v
e t h e s m a l l e s t e l e m e n t f r
- m
t h e h e a p , a n d a d d i t t
- t
h e r e s u l t l i s t
( t h i s i s a k i n d
- f
s e l e c t i
- n
s
- r
t ) H
- w
e v e r , t h i s a l g
- r
i t h m i s n
- t
i n
- p
l a c e . H e a p s
- r
t u s e s t h e s a m e i d e a , b u t w i t h
- u
t a l l
- c
a t i n g a n y e x t r a m e m
- r
y .
H e a p s
- r
t , i n
- p
l a c e
W e a r e g
- i
n g t
- r
e p e a t e d l y r e m
- v
e t h e l a r g e s t v a l u e f r
- m
t h e a r r a y a n d p u t i t i n t h e r i g h t p l a c e
- u
s i n g a s
- c
a l l e d m a x h e a p , a h e a p w h e r e y
- u
c a n fj n d a n d d e l e t e t h e m a x i m u m e l e m e n t i n s t e a d
- f
t h e m i n i m u m
W e ' l l d i v i d e t h e a r r a y i n t
- t
w
- p
a r t s
- Tie
fj r s t p a r t w i l l b e a h e a p
- Tie
r e s t w i l l c
- n
t a i n t h e v a l u e s w e ' v e r e m
- v
e d
( Tii s d i v i s i
- n
i s t h e s a m e i d e a w e u s e d f
- r
i n
- p
l a c e s e l e c t i
- n
a n d i n s e r t i
- n
s
- r
t )
H e a p s
- r
t , i n
- p
l a c e
F i r s t t u r n t h e a r r a y i n t
- a
h e a p Tie n j u s t r e p e a t e d l y d e l e t e t h e m i n i m u m e l e m e n t ! R e m e m b e r t h e d e l e t i
- n
a l g
- r
i t h m :
- S
w a p t h e m a x i m u m ( fj r s t ) e l e m e n t w i t h t h e l a s t e l e m e n t
- f
t h e h e a p
- R
e d u c e t h e s i z e
- f
t h e h e a p b y 1 ( d e l e t e s t h e fj r s t e l e m e n t , b r e a k s t h e i n v a r i a n t )
- S
i f t t h e fj r s t e l e m e n t d
- w
n ( r e p a i r s t h e i n v a r i a n t )
Tie s w a p a c t u a l l y p u t s t h e m a x i m u m e l e m e n t i n t h e r i g h t p l a c e i n t h e a r r a y
T r a c e
- f
h e a p s
- r
t
89 76 74 37 32 39 66 20 26 18 28 29 6
F i r s t b u i l d a h e a p ( n
- t
s h
- w
n )
T r a c e
- f
h e a p s
- r
t
89 76 74 37 32 39 66 20 26 18 28 29 6
S t e p 1 : s w a p m a x i m u m a n d l a s t e l e m e n t ; d e c r e a s e s i z e
- f
h e a p b y 1
T r a c e
- f
h e a p s
- r
t
6 76 74 37 32 39 66 20 26 18 28 29 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
B l u e – s
- r
t e d p a r t
- f
a r r a y , n
- t
p a r t
- f
h e a p
T r a c e
- f
h e a p s
- r
t
76 6 74 37 32 39 66 20 26 18 28 29 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
T r a c e
- f
h e a p s
- r
t
76 37 74 6 32 39 66 20 26 18 28 29 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
T r a c e
- f
h e a p s
- r
t
76 37 74 26 32 39 66 20 6 18 28 29 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
T r a c e
- f
h e a p s
- r
t
76 37 74 26 32 39 66 20 6 18 28 29 89
W e n
- w
h a v e t h e b i g g e s t e l e m e n t a t t h e e n d
- f
t h e a r r a y , a n d a h e a p t h a t ' s
- n
e e l e m e n t s m a l l e r !
T r a c e
- f
h e a p s
- r
t
76 37 74 26 32 39 66 20 6 18 28 29 89
S t e p 1 : s w a p m a x i m u m a n d l a s t e l e m e n t ; d e c r e a s e s i z e
- f
h e a p b y 1
T r a c e
- f
h e a p s
- r
t
29 37 74 26 32 39 66 20 6 18 28 76 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
T r a c e
- f
h e a p s
- r
t
74 37 29 26 32 39 66 20 6 18 28 76 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
T r a c e
- f
h e a p s
- r
t
74 37 66 26 32 39 29 20 6 18 28 76 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
T r a c e
- f
h e a p s
- r
t
74 37 66 26 32 39 29 20 6 18 28 76 89
T r a c e
- f
h e a p s
- r
t
74 37 66 26 32 39 29 20 6 18 28 76 89
S t e p 1 : s w a p m a x i m u m a n d l a s t e l e m e n t ; d e c r e a s e s i z e
- f
h e a p b y 1
T r a c e
- f
h e a p s
- r
t
28 37 66 26 32 39 29 20 6 18 74 76 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
T r a c e
- f
h e a p s
- r
t
66 37 28 26 32 39 29 20 6 18 74 76 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
T r a c e
- f
h e a p s
- r
t
66 37 39 26 32 28 29 20 6 18 74 76 89
S t e p 2 : s i f t fj r s t e l e m e n t d
- w
n
T r a c e
- f
h e a p s
- r
t
66 37 39 26 32 28 29 20 6 18 74 76 89
C
- m
p l e x i t y
- f
h e a p s
- r
t
B u i l d i n g t h e h e a p t a k e s O ( n ) t i m e W e d e l e t e t h e m a x i m u m e l e m e n t n t i m e s , e a c h d e l e t i
- n
t a k i n g O ( l
- g
n ) t i m e H e n c e t h e t
- t
a l c
- m
p l e x i t y i s O ( n l
- g
n )
W a r n i n g
O u r f
- r
m u l a s f
- r
fj n d i n g c h i l d r e n a n d p a r e n t s i n t h e a r r a y a s s u m e
- b
a s e d a r r a y s Tie b
- k
, f
- r
s
- m
e r e a s
- n
, u s e s 1
- b
a s e d a r r a y s ( a n d l a t e r s w i t c h e s t
- b
a s e d a r r a y s ) ! I n a h e a p i m p l e m e n t e d u s i n g a 1
- b
a s e d a r r a y :
- t
h e l e f t c h i l d
- f
i n d e x i i s i n d e x 2 i
- t
h e r i g h t c h i l d i s i n d e x 2 i + 1
- t
h e p a r e n t i s i n d e x i / 2
B e c a r e f u l w h e n d
- i
n g t h e l a b !
S u m m a r y
- f
b i n a r y h e a p s
B i n a r y h e a p s : O ( l
- g
n ) i n s e r t , O ( 1 ) fj n d m i n i m u m , O ( l
- g
n ) d e l e t e m i n i m u m
- A
c
- m
p l e t e b i n a r y t r e e w i t h t h e h e a p p r
- p
e r t y , r e p r e s e n t e d a s a n a r r a y
H e a p s
- r
t : b u i l d a m a x h e a p , r e p e a t e d l y r e m
- v
e l a s t e l e m e n t a n d p l a c e a t e n d
- f
a r r a y
- C
a n b e d
- n
e i n
- p
l a c e , O ( n l
- g
n )
I n f a c t , h e a p s w e r e
- r
i g i n a l l y i n v e n t e d f
- r
h e a p s
- r
t !
L e f t i s t h e a p s
M e r g i n g t w
- h
e a p s
A n
- t
h e r
- p
e r a t i
- n
w e m i g h t w a n t t
- d
- i
s m e r g e t w
- h
e a p s
- B
u i l d a n e w h e a p w i t h t h e c
- n
t e n t s
- f
b
- t
h h e a p s
- e
. g . , m e r g i n g a h e a p c
- n
t a i n i n g 1 , 2 , 8 , 9 , 1 a n d a h e a p c
- n
t a i n i n g 3 , 4 , 5 , 6 , 7 g i v e s a h e a p c
- n
t a i n i n g 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1
F
- r
- u
r e a r l i e r n a ï v e p r i
- r
i t y q u e u e s :
- A
n u n s
- r
t e d a r r a y : c
- n
c a t e n a t e t h e a r r a y s
- A
s
- r
t e d a r r a y : m e r g e t h e a r r a y s ( a s i n m e r g e s
- r
t )
F
- r
b i n a r y h e a p s :
- T
a k e s O ( n ) t i m e b e c a u s e y
- u
n e e d t
- a
t l e a s t c
- p
y t h e c
- n
t e n t s
- f
- n
e h e a p t
- t
h e
- t
h e r
C a n ' t c
- m
b i n e t w
- a
r r a y s i n l e s s t h a n O ( n ) t i m e !
M e r g i n g t r e e
- b
a s e d h e a p s
G
- b
a c k t
- u
r i d e a
- f
a b i n a r y t r e e w i t h t h e h e a p p r
- p
e r t y : I f w e c a n m e r g e t w
- f
t h e s e t r e e s , w e c a n i m p l e m e n t i n s e r t i
- n
a n d d e l e t e m i n i m u m ! ( W e ' l l s e e h
- w
t
- i
m p l e m e n t m e r g e l a t e r )
8 18 29 37 32 74 89 20 28 39 66
I n s e r t i
- n
T
- i
n s e r t a s i n g l e e l e m e n t :
- b
u i l d a h e a p c
- n
t a i n i n g j u s t t h a t
- n
e e l e m e n t
- m
e r g e i t i n t
- t
h e e x i s t i n g h e a p !
E . g . , i n s e r t i n g 1 2
8 18 29 37 32 74 89 20 28 39 66 + 12
A t r e e w i t h j u s t
- n
e n
- d
e
D e l e t e m i n i m u m
T
- d
e l e t e t h e m i n i m u m e l e m e n t :
- t
a k e t h e l e f t a n d r i g h t b r a n c h e s
- f
t h e t r e e
- t
h e s e c
- n
t a i n e v e r y e l e m e n t e x c e p t t h e s m a l l e s t
- m
e r g e t h e m !
E . g . , d e l e t i n g 8 f r
- m
t h e p r e v i
- u
s h e a p
18 29 37 32 74 89 20 28 39 66
+
H e a p s b a s e d
- n
m e r g i n g
I f w e c a n t a k e t r e e s w i t h t h e h e a p p r
- p
e r t y , a n d i m p l e m e n t m e r g i n g w i t h O ( l
- g
n ) c
- m
p l e x i t y , w e g e t a p r i
- r
i t y q u e u e w i t h :
- O
( 1 ) fj n d m i n i m u m
- O
( l
- g
n ) i n s e r t i
- n
( b y m e r g i n g )
- O
( l
- g
n ) d e l e t e m i n i m u m ( b y m e r g i n g )
- p
l u s t h i s u s e f u l m e r g e
- p
e r a t i
- n
i t s e l f
Tie r e a r e l
- t
s
- f
h e a p s b a s e d
- n
t h i s i d e a :
- s
k e w h e a p s , F i b
- n
a c c i h e a p s , b i n
- m
i a l h e a p s
W e w i l l s t u d y
- n
e : l e f t i s t h e a p s
N a i v e m e r g i n g
1 . L
- k
a t t h e r
- t
s
- f
t h e t w
- t
r e e s W e a r e g
- i
n g t
- p
i c k t h e s m a l l e r
- n
e a s t h e r
- t
- f
t h e n e w t r e e
18 29 37 32 74 89 20 28 39 66
+
N a i v e m e r g i n g
2 . R e c u r s i v e l y m e r g e t h e r i g h t b r a n c h a n d t h e s e c
- n
d t r e e
18 29 37 32 74 89 20 28 39 66
+
N a i v e m e r g i n g
2 . R e c u r s i v e l y m e r g e t h e r i g h t b r a n c h a n d t h e s e c
- n
d t r e e
18 29 37 32 74 89 20 28 39 66
+
N a i v e m e r g i n g
2 . R e c u r s i v e l y m e r g e t h e r i g h t b r a n c h a n d t h e s e c
- n
d t r e e
18 29 37 32 74 89 20 39 66
+
28
N a i v e m e r g i n g
2 . R e c u r s i v e l y m e r g e t h e r i g h t b r a n c h a n d t h e s e c
- n
d t r e e
18 29 37 32 74 89 20 39 66
+
28
N a i v e m e r g i n g
2 . R e c u r s i v e l y m e r g e t h e r i g h t b r a n c h a n d t h e s e c
- n
d t r e e
18 29 37 32 74 89 20 39 66 28
N a i v e m e r g i n g
2 . R e c u r s i v e l y m e r g e t h e r i g h t b r a n c h a n d t h e s e c
- n
d t r e e
18 29 37 32 74 89 20 39 66 28
P e r f
- r
m a n c e
- f
n a ï v e m e r g i n g
Tie m e r g e a l g
- r
i t h m d e s c e n d s d
- w
n t h e r i g h t b r a n c h
- f
b
- t
h t r e e s S
- t
h e r u n t i m e d e p e n d s
- n
h
- w
m a n y t i m e s y
- u
c a n f
- l
l
- w
t h e r i g h t b r a n c h b e f
- r
e y
- u
g e t t
- t
h e e n d
- f
t h e t r e e
- L
e t ' s c a l l t h i s t h e r i g h t n u l l p a t h l e n g t h
C
- m
p l e x i t y : O ( m + n )
- w
h e r e m a n d n a r e t h e r i g h t n u l l p a t h l e n g t h s
- f
t h e t w
- t
r e e s
L
- g
a r i t h m i c c
- m
p l e x i t y f
- r
b a l a n c e d t r e e s , b u t c a n b e c
- m
e l i n e a r i s t h e t r e e s a r e h e a v i l y “ r i g h t
- b
i a s e d ”
W
- r
s t c a s e f
- r
n a ï v e m e r g i n g
A h e a v i l y r i g h t
- b
i a s e d t r e e :
L e f t i s t h e a p s –
- b
s e r v a t i
- n
N a i v e m e r g i n g i s :
- b
a d ( l i n e a r c
- m
p l e x i t y ) f
- r
r i g h t
- b
i a s e d t r e e s
- g
- d
( l
- g
a r i t h m i c
- r
b e t t e r ) f
- r
l e f t
- b
i a s e d t r e e s
I d e a
- f
l e f t i s t h e a p s :
- A
d d a n i n v a r i a n t t h a t s t
- p
s t h e t r e e b e c
- m
i n g r i g h t
- b
i a s e d
- I
n
- t
h e r w
- r
d s , b y r e p e a t e d l y f
- l
l
- w
i n g t h e r i g h t b r a n c h , y
- u
q u i c k l y r e a c h t h e e n d
- f
t h e t r e e
N u l l p a t h l e n g t h
W e d e fj n e t h e n u l l p a t h l e n g t h ( n p l )
- f
a n
- d
e t
- b
e t h e s h
- r
t e s t p a t h t h a t l e a d s t
- t
h e e n d
- f
t h e t r e e ( a n u l l i n J a v a ) Tie n u l l p a t h l e n g t h
- f
n u l l i t s e l f i s S i m i l a r c
- n
c e p t t
- h
e i g h t , b u t w i t h h e i g h t w e m e a s u r e t h e l
- n
g e s t p a t h i n t h e t r e e
18 29 37 32 74 89 20 39 66 28
n p l 3 ( l e f t l e f t → l e f t ) ) → n p l 2 ( l e f t r i g h t ) →
66
n p l 1 ( r i g h t )
L e f t i s t h e a p s
L e f t i s t i n v a r i a n t : t h e n p l
- f
t h e l e f t c h i l d ≥ t h e n p l
- f
t h e r i g h t c h i l d Tii s m e a n s : t h e q u i c k e s t w a y t
- r
e a c h a n u l l i s t
- f
- l
l
- w
t h e r i g h t b r a n c h
18 29 37 32 74 89 20 39 66 28 66
L e f t i s t m e r g i n g
W e s t a r t w i t h t h e n a ï v e m e r g i n g a l g
- r
i t h m f r
- m
e a r l i e r :
- Tie
l e f t i s t i n v a r i a n t m e a n s t h a t n a ï v e m e r g i n g s t
- p
s a f t e r O ( l
- g
n ) s t e p s
B u t t h e m e r g e m i g h t b r e a k t h e l e f t i s t i n v a r i a n t !
- Wh
e n w e d e s c e n d i n t
- t
h e r i g h t c h i l d , i t s n p l m i g h t i n c r e a s e , a n d b e c
- m
e g r e a t e r t h a n t h e l e f t c h i l d
F i x i t b y :
- G
- i
n g u p w a r d s i n t h e t r e e f r
- m
w h e r e t h e m e r g e fj n i s h e d , a n d w h e r e v e r w e e n c
- u
n t e r a n
- d
e w h e r e l e f t c h i l d ' s n p l < r i g h t c h i l d ' s n p l , s w a p t h e t w
- c
h i l d r e n !
L e f t i s t m e r g i n g
1 . S t a r t w i t h n a ï v e m e r g i n g f r
- m
e a r l i e r
18 29 37 32 74 89 20 28 39 66
+
31 37
L e f t i s t m e r g i n g
2 . Tie r e c u r s i
- n
“ b
- t
t
- m
e d
- u
t ” a t 6 6 h e r e
18 29 37 31 74 89 20 39 66 28 32 37
L e f t i s t m e r g i n g
3 . G
- u
p t
- t
h e p a r e n t , c
- m
p a r e l e f t a n d r i g h t c h i l d ' s n p l
18 29 37 31 74 89 20 39 66 28 32
l e f t n p l : r i g h t n p l : 1 I n v a r i a n t b r
- k
e n !
37
L e f t i s t m e r g i n g
4 . I f t h e l e f t i s t i n v a r i a n t i s b r
- k
e n , s w a p t h e l e f t a n d r i g h t c h i l d r e n
18 29 37 31 74 89 20 39 66 28 32
6 6 b e c
- m
e s t h e l e f t c h i l d i n s t e a d
37
L e f t i s t m e r g i n g
5 . G
- u
p a g a i n a n d r e p e a t !
18 29 37 31 74 89 20 39 66 28 32
l e f t n p l : 2 r i g h t n p l : 1 O K !
37
L e f t i s t m e r g i n g
5 . G
- u
p a g a i n a n d r e p e a t !
18 29 37 31 74 89 20 39 66 28 32
l e f t n p l : 1 r i g h t n p l : 2 I n v a r i a n t b r
- k
e n ! S w a p l e f t a n d r i g h t .
37
L e f t i s t m e r g i n g
5 . G
- u
p a g a i n a n d r e p e a t !
18 29 37 31 74 89 20 39 66 28 32
l e f t n p l : 1 r i g h t n p l : 2 I n v a r i a n t b r
- k
e n ! S w a p l e f t a n d r i g h t .
37
L e f t i s t m e r g i n g
6 . Wh e n w e ' v e r e a c h e d t h e r
- t
, w e ' v e fj n i s h e d ! N
- t
i c e h
- w
t h e fj n a l h e a p “ l e a n s t
- t
h e l e f t ” .
18 29 37 31 74 89 20 39 66 28 32 37
I m p l e m e n t a t i
- n
I m p l e m e n t a t i
- n
:
- N
e e d t
- b
e a b l e t
- c
- m
p u t e n p l e ffjc i e n t l y
- A
d d a fj e l d f
- r
t h e n p l t
- e
a c h n
- d
e , a n d u p d a t e i t w h e n e v e r w e m
- d
i f y t h e n
- d
e
- U
p d a t e b y c
- m
p u t i n g : n p l = 1 + r i g h t c h i l d ' s n p l
C
- m
p l e x i t y
- f
l e f t i s t m e r g i n g
I c l a i m : t h e n p l
- f
a t r e e
- f
s i z e n i s O ( l
- g
n )
- C
h e c k i t f
- r
y
- u
r s e l f : )
- F
- r
b a l a n c e d t r e e s , t h e n p l i s O ( l
- g
n ) , m u c h l i k e h e i g h t
- B
y u n b a l a n c i n g a t r e e , w e m a k e s
- m
e p a t h s l
- n
g e r , a n d s
- m
e s h
- r
t e r . Tii s i n c r e a s e s t h e h e i g h t , b u t d e c r e a s e s t h e n p l !
H e n c e , i n a l e f t i s t h e a p , b y f
- l
l
- w
i n g t h e r i g h t b r a n c h O ( l
- g
n ) t i m e s , y
- u
r e a c h a n u l l S
- m
e r g e t a k e s O ( l
- g
n ) t i m e !
- l
- g
n s t e p s d
- w
n t h e t r e e t
- d
- t
h e n a ï v e t r e e
- t
h e n l
- g
n s t e p s u p w a r d s w h i l e r e p a i r i n g t h e l e f t i s t i n v a r i a n t
L e f t i s t h e a p s
I m p l e m e n t a t i
- n
- f
p r i
- r
i t y q u e u e s :
- b
i n a r y t r e e s w i t h h e a p p r
- p
e r t y
- l
e f t i s t i n v a r i a n t f
- r
O ( l
- g
n ) m e r g i n g
- o
t h e r
- p
e r a t i
- n
s a r e b a s e d
- n
m e r g e
A g
- d
fj t f
- r
f u n c t i
- n
a l l a n g u a g e s :
- b
a s e d
- n