A V L t r e e s ( W e i s s 4 . 4 ) B a l a n - - PowerPoint PPT Presentation
A V L t r e e s ( W e i s s 4 . 4 ) B a l a n - - PowerPoint PPT Presentation
A V L t r e e s ( W e i s s 4 . 4 ) B a l a n c e d B S T s : t h e p r o b l e m Tie B S T o p e r a t i o n s t a k e O ( h e i g h t o f t r e e ) , s o f o r u n b
B a l a n c e d B S T s : t h e p r
- b
l e m
Tie B S T
- p
e r a t i
- n
s t a k e O ( h e i g h t
- f
t r e e ) , s
- f
- r
u n b a l a n c e d t r e e s c a n t a k e O ( n ) t i m e
B a l a n c e d B S T s : t h e s
- l
u t i
- n
T a k e B S T s a n d a d d a n e x t r a i n v a r i a n t t h a t m a k e s s u r e t h a t t h e t r e e i s b a l a n c e d
- H
e i g h t
- f
t r e e m u s t b e O ( l
- g
n )
- Tie
n a l l
- p
e r a t i
- n
s w i l l t a k e O ( l
- g
n ) t i m e
O n e p
- s
s i b l e i d e a f
- r
a n i n v a r i a n t :
- H
e i g h t
- f
l e f t c h i l d = h e i g h t
- f
r i g h t c h i l d ( f
- r
a l l n
- d
e s i n t h e t r e e )
- T
r e e w
- u
l d b e s
- r
t
- f
“ p e r f e c t l y b a l a n c e d ”
Wh a t ' s w r
- n
g w i t h t h i s i d e a ?
A t
- r
e s t r i c t i v e i n v a r i a n t
P e r f e c t b a l a n c e i s t
- r
e s t r i c t i v e ! N u m b e r
- f
n
- d
e s c a n
- n
l y b e 1 , 3 , 7 , 1 5 , 3 1 , . . .
- w
l
- w
l l e m u r l e m u r g
- r
i l l a g
- r
i l l a p e n g u i n p e n g u i n w
- l
f w
- l
f h a m s t e r h a m s t e r p a n d a p a n d a
A V L t r e e s – a l e s s r e s t r i c t i v e i n v a r i a n t
Tie A V L t r e e i s t h e fj r s t b a l a n c e d B S T d i s c
- v
e r e d ( f r
- m
1 9 6 2 ) – i t ' s n a m e d a f t e r A d e l s
- n
- V
e l s k y a n d L a n d i s I t ' s a B S T w i t h t h e f
- l
l
- w
i n g i n v a r i a n t :
- Tie
d i fg e r e n c e i n h e i g h t s b e t w e e n t h e l e f t a n d r i g h t c h i l d r e n
- f
a n y n
- d
e i s a t m
- s
t 1
- (
c
- m
p a r e d t
- f
- r
a p e r f e c t l y b a l a n c e d t r e e )
Tii s m a k e s t h e t r e e ' s h e i g h t O ( l
- g
n ) , s
- i
t ' s b a l a n c e d
E x a m p l e
- f
a n A V L t r e e ( f r
- m
W i k i p e d i a )
12 23 54 76 9 14 19 67 50 17 72
L e f t c h i l d h e i g h t 2 R i g h t c h i l d h e i g h t 1 L e f t c h i l d h e i g h t 2 R i g h t c h i l d h e i g h t 2 L e f t c h i l d h e i g h t 1 R i g h t c h i l d h e i g h t
W h y a r e t h e s e n
- t
A V L t r e e s ?
W h y a r e t h e s e n
- t
A V L t r e e s ?
L e f t c h i l d h e i g h t R i g h t c h i l d h e i g h t 8
W h y a r e t h e s e n
- t
A V L t r e e s ?
L e f t c h i l d h e i g h t 1 R i g h t c h i l d h e i g h t 3
R
- t
a t i
- n
R
- t
a t i
- n
r e a r r a n g e s a B S T b y m
- v
i n g a d i fg e r e n t n
- d
e t
- t
h e r
- t
, w i t h
- u
t c h a n g i n g t h e B S T ' s c
- n
t e n t s ( p i c f r
- m
W i k i p e d i a )
R
- t
a t i
- n
W e c a n s t r a t e g i c a l l y u s e r
- t
a t i
- n
s t
- r
e b a l a n c e a n u n b a l a n c e d t r e e . Tii s i s w h a t m
- s
t b a l a n c e d B S T v a r i a n t s d
- !
H e i g h t
- f
4 H e i g h t
- f
3
A V L i n s e r t i
- n
S t a r t b y d
- i
n g a B S T i n s e r t i
- n
- Tii
s m i g h t b r e a k t h e A V L ( b a l a n c e ) i n v a r i a n t
Tie n g
- u
p w a r d s f r
- m
t h e n e w l y
- i
n s e r t e d n
- d
e , l
- k
i n g f
- r
n
- d
e s t h a t b r e a k t h e i n v a r i a n t ( u n b a l a n c e d n
- d
e s ) I f y
- u
fj n d
- n
e , r
- t
a t e i t t
- fj
x t h e b a l a n c e Tie r e a r e f
- u
r c a s e s d e p e n d i n g
- n
h
- w
t h e n
- d
e b e c a m e u n b a l a n c e d
C a s e 1 : a l e f t
- l
e f t t r e e
5 c 2 5 b a
E a c h p i n k t r i a n g l e r e p r e s e n t s a n A V L t r e e w i t h h e i g h t k Tie p u r p l e r e p r e s e n t s a n i n s e r t i
- n
t h a t h a s i n c r e a s e d t h e h e i g h t
- f
t r e e a t
- k
+ 1 N
- t
i c e t h a t t h e t r e e w a s b a l a n c e d b e f
- r
e t h e p u r p l e b i t w a s a d d e d A s s u m p t i
- n
: t h i s t r e e i s t h e d e e p e s t n
- d
e t h a t v i
- l
a t e s t h e i n v a r i a n t
C a s e 1 : a l e f t
- l
e f t t r e e
5 c 2 5 b a
H e i g h t k H e i g h t k + 2 L e f t h e i g h t m i n u s r i g h t h e i g h t = 2 : i n v a r i a n t b r
- k
e n !
C a s e 1 : a l e f t
- l
e f t t r e e
5 c 2 5 b a
Tii s i s c a l l e d a l e f t
- l
e f t t r e e b e c a u s e b
- t
h t h e r
- t
a n d t h e l e f t c h i l d a r e d e e p e r
- n
t h e l e f t T
- fj
x i t w e d
- a
r i g h t r
- t
a t i
- n
B a l a n c i n g a l e f t
- l
e f t t r e e , a f t e r w a r d s
5 c 2 5 b a
H e i g h t k + 1 H e i g h t k + 1 I n v a r i a n t r e s t
- r
e d !
C a s e 2 : a r i g h t
- r
i g h t t r e e
2 5 a 5 b c
M i r r
- r
i m a g e
- f
l e f t
- l
e f t t r e e C a n b e fj x e d w i t h l e f t r
- t
a t i
- n
C a s e 3 : a l e f t
- r
i g h t t r e e
5 c 2 5 b a
H e i g h t k H e i g h t k + 2 L e f t h e i g h t m i n u s r i g h t h e i g h t = 2 : i n v a r i a n t b r
- k
e n !
C a s e 3 : a l e f t
- r
i g h t t r e e
5 c 2 5 b a
W e c a n ' t fj x t h i s w i t h
- n
e r
- t
a t i
- n
L e t ' s l
- k
a t b ' s s u b t r e e s b
L
a n d b
R
C a s e 3 : a l e f t
- r
i g h t t r e e
5 c 2 5 a
R
- t
a t e 2 5
- s
u b t r e e t
- t
h e l e f t
4
b
R
b
L
H e i g h t k
- 1
C a s e 3 : a l e f t
- r
i g h t t r e e
5 c 2 5 a
W e n
- w
h a v e a l e f t
- l
e f t t r e e ! S
- w
e c a n fj x i t b y r
- t
a t i n g t h e w h
- l
e t r e e t
- t
h e r i g h t
4
b
R
b
L
H e i g h t k + 2 H e i g h t k H e i g h t k + 2 H e i g h t k + 1
C a s e 3 : a l e f t
- r
i g h t t r e e
5 c 2 5 a 4
b
R
b
L
B a l a n c e d ! N
- t
i c e i t w
- r
k s w h i c h e v e r
- f
b
L
a n d b
R
h a s t h e e x t r a h e i g h t
C a s e 4 : a r i g h t
- l
e f t t r e e
2 5 a 5 b c
M i r r
- r
i m a g e
- f
l e f t
- r
i g h t t r e e
H
- w
t
- i
d e n t i f y t h e c a s e s
L e f t
- l
e f t ( e x t r a h e i g h t i n l e f t
- l
e f t g r a n d c h i l d ) :
- h
e i g h t
- f
l e f t
- l
e f t g r a n d c h i l d = k + 1 h e i g h t
- f
l e f t c h i l d = k + 2 h e i g h t
- f
r i g h t c h i l d = k
- R
- t
a t e t h e w h
- l
e t r e e t
- t
h e r i g h t
L e f t
- r
i g h t ( e x t r a h e i g h t i n l e f t
- r
i g h t g r a n d c h i l d ) :
- h
e i g h t
- f
l e f t
- r
i g h t g r a n d c h i l d = k + 1 h e i g h t
- f
l e f t c h i l d = k + 2 h e i g h t
- f
r i g h t c h i l d = k
- F
i r s t r
- t
a t e t h e l e f t c h i l d t
- t
h e l e f t
- Tie
n r
- t
a t e t h e w h
- l
e t r e e t
- t
h e r i g h t
R i g h t
- l
e f t a n d r i g h t
- r
i g h t : s y m m e t r i c
A l g
- r
i t h m u s e s h e i g h t s
- f
s u b t r e e s t
- d
e t e r m i n e c a s e
T h e f
- u
r c a s e s
( p i c t u r e f r
- m
W i k i p e d i a ) Tie n u m b e r s i n t h e d i a g r a m s h
- w
t h e b a l a n c e
- f
t h e t r e e : l e f t h e i g h t m i n u s r i g h t h e i g h t T
- i
m p l e m e n t t h i s e ffj c i e n t l y , r e c
- r
d t h e b a l a n c e i n t h e n
- d
e s a n d l
- k
a t i t t
- w
- r
k
- u
t w h i c h c a s e y
- u
' r e i n
5 D 3 A 4 C B Left Right Case Right Left Case 3 A 4 5 C D B Right Right Case 5 D 4 3 B A C Left Left Case 4 5 C D Balanced 3 A B 4 5 C D Balanced 3 A B 3 A 5 D 4 B C
- 2
2 1
- 1
2
- 2
1/0
- 1/0
- 1/0
1/0
E x a m p l e : t h e q u i c k b r
- w
n f
- x
j u m p s
- v
e r a l a z y d
- g
I n s e r t “ b r
- w
n ” i n t
- “
t h e q u i c k ”
t h e q u i c k b r
- w
n L e f t
- l
e f t t r e e ! R
- t
a t e r i g h t
E x a m p l e : t h e q u i c k b r
- w
n f
- x
j u m p s
- v
e r a l a z y d
- g
I n s e r t “ b r
- w
n ” i n t
- “
t h e q u i c k ”
t h e q u i c k b r
- w
n
E x a m p l e : t h e q u i c k b r
- w
n f
- x
j u m p s
- v
e r a l a z y d
- g
I n s e r t “ j u m p s ” i n t
- “
t h e q u i c k b r
- w
n f
- x
”
t h e q u i c k b r
- w
n f
- x
j u m p s R i g h t
- r
i g h t t r e e ! ( Wh a t n
- d
e ? ) R
- t
a t e l e f t
E x a m p l e : t h e q u i c k b r
- w
n f
- x
j u m p s
- v
e r a l a z y d
- g
I n s e r t “ j u m p s ” i n t
- “
t h e q u i c k b r
- w
n f
- x
”
t h e q u i c k f
- x
j u m p s b r
- w
n
E x a m p l e : t h e q u i c k b r
- w
n f
- x
j u m p s
- v
e r a l a z y d
- g
I n s e r t “
- v
e r ” i n t
- “
t h e q u i c k b r
- w
n f
- x
j u m p s ”
t h e q u i c k f
- x
j u m p s b r
- w
n
- v
e r L e f t
- r
i g h t t r e e ! ( q u i c k → f
- x
→ j u m p s ) R
- t
a t e f
- x
l e f t . . .
E x a m p l e : t h e q u i c k b r
- w
n f
- x
j u m p s
- v
e r a l a z y d
- g
I n s e r t “
- v
e r ” i n t
- “
t h e q u i c k b r
- w
n f
- x
j u m p s ”
t h e q u i c k j u m p s
- v
e r f
- x
. . . t h e n r
- t
a t e q u i c k r i g h t b r
- w
n
E x a m p l e : t h e q u i c k b r
- w
n f
- x
j u m p s
- v
e r a l a z y d
- g
I n s e r t “
- v
e r ” i n t
- “
t h e q u i c k b r
- w
n f
- x
j u m p s ”
q u i c k j u m p s f
- x
t h e b r
- w
n
- v
e r
D e l e t i
- n
i n a n A V L t r e e
F i r s t d
- t
h e n
- r
m a l B S T d e l e t i
- n
Tie n g
- u
p t h e t r e e , fj n d i n g n
- d
e s t h a t b r e a k t h e i n v a r i a n t a n d fj x i n g t h e m u s i n g r
- t
a t i
- n
s
- S
t a r t f r
- m
t h e n
- d
e t h a t w a s r e m
- v
e d f r
- m
t h e t r e e ( r e c a l l t h a t i n t h e c a s e t h a t t h e v a l u e t
- b
e d e l e t e d w a s a n
- d
e w i t h t w
- c
h i l d r e n , t h i s w a s t h e b i g g e s t v a l u e i n t h e l e f t s u b t r e e )
Tie c a s e s y
- u
n e e d t
- c
- n
s i d e r a r e e x a c t l y t h e s a m e a s f
- r
i n s e r t i
- n
!
- Wh
e n i m p l e m e n t i n g A V L t r e e s , y
- u
n e e d
- n
l y i m p l e m e n t t h e b a l a n c i n g c
- d
e
- n
c e
- I
n g e n e r a l , t h e b a l a n c i n g a l g
- r
i t h m w
- r
k s f
- r
a n y n
- d
e w h e r e t h e l e f t a n d r i g h t c h i l d r e n s a t i s f y t h e A V L i n v a r i a n t , b u t t h e i r h e i g h t s d i fg e r b y 2 . N
- t
h i n g s p e c i fj c t
- i
n s e r t i
- n
. . .
Tie r e i s
- n
e s u b t l e p
- i
n t , s e e n e x t s l i d e . . .
A n e x t r a c a s e i n d e l e t i
- n
5 c 2 5 b
H e i g h t k H e i g h t k + 2 Tii s c a s e d
- e
s n ’ t a p p e a r i n i n s e r t i
- n
( t h i n k a b
- u
t w h y ) . B u t i t w
- r
k s fj n e w h e n t r e a t e d a s a l e f t
- l
e f t t r e e !
a
a a n d b b
- t
h h a v e h e i g h t k + 1
C
- n
s i d e r t h e s m a l l e s t A V L t r e e w i t h h e i g h t h ( h ≥ 2 ) . I t m u s t h a v e t w
- c
h i l d r e n :
- O
n e
- f
h e i g h t h
- 1
, s
- t
h a t t h e t r e e t
- h
a v e h e i g h t h
- O
n e
- f
h e i g h t h
- 2
, s
- t
h a t t h e t r e e i s a s s m a l l a s p
- s
s i b l e
Tiu s , i f F ( h ) i s t h e s i z e
- f
t h e s m a l l e s t A V L t r e e
- f
h e i g h t h , w e h a v e :
- F
( ) = , F ( 1 ) = 1 , F ( h ) = F ( h
- 1
) + F ( h
- 2
) i f h ≥ 2
Tiu s F ( h ) i s t h e h t h F i b
- n
a c c i n u m b e r !
- F
( h ) ~ φ
h
, w h e r e i s t h e g
- l
d e n r a t i
- φ
- I
f a n A V L t r e e h a s s i z e n a n d h e i g h t h , t h e n n ≥ φ
h
- T
a k i n g l
- g
s
- f
b
- t
h s i d e s , h ≤ l
- g
φ
n = l
- g
2
n / l
- g
2
~ 1 . 4 4 l
- g
φ
2
n
S
- :
a n A V L t r e e
- f
n n
- d
e s h a s h e i g h t a t m
- s
t 1 . 4 4 l
- g
2
n
H
- w
b a l a n c e d a r e A V L t r e e s ?
A V L t r e e s
U s e r
- t
a t i
- n
t
- k
e e p t h e t r e e b a l a n c e d
- W
- r
s t c a s e h e i g h t 1 . 4 4 l
- g
2
n , n
- r
m a l l y c l
- s
e t
- l
- g
2
n – s
- l
- k
u p s a r e q u i c k
I n s e r t i
- n
/ d e l e t i
- n
– B S T i n s e r t i
- n
/ d e l e t i
- n
, t h e n r
- t
a t e t
- r
e p a i r t h e i n v a r i a n t V i s u a l i s a t i
- n
:
- h
t t p : / / v i s u a l g
- .
n e t /
- h
t t p s : / / w w w . c s . u s f c a . e d u / ~ g a l l e s / v i s u a l i z a t i
- n