lecture 8 - binary trees
TRANSCRIPT
-
7/29/2019 Lecture 8 - Binary Trees
1/19
-
7/29/2019 Lecture 8 - Binary Trees
2/19
9 . 1 - 4 S h o w t h a t t h e n l g n l o w e r b o u n d f o r s o r t i n g
s t i l l h o l d s w i t h t e r n a r y c o m p a r i s o n s .
T h e m a x i m u m n u m b e r o f l e a v e s i n a t r e e o f h e i g h t h
i s 3
h
,
l g
3
n ! = n l g n
S o i t g o e s f o r a n y c o n s t a n t b a s e .
-
7/29/2019 Lecture 8 - Binary Trees
3/19
B i n a r y S e a r c h T r e e s
I t h i n k t h a t I s h a l l n e v e r s e e
a p o e m a s l o v e l y a s a t r e e P o e m ' s
a r e w r o t e b y f o o l s l i k e m e b u t o n l y
G - d c a n m a k e a t r e e
J o y c e K i l m e r
B i n a r y s e a r c h t r e e s p r o v i d e a d a t a s t r u c t u r e w h i c h e f -
c i e n t l y s u p p o r t s a l l s i x d i c t i o n a r y o p e r a t i o n s .
A b i n a r y t r e e i s a r o o t e d t r e e w h e r e e a c h n o d e c o n t a i n s
a t m o s t t w o c h i l d r e n .
E a c h c h i l d c a n b e i d e n t i e d a s e i t h e r a l e f t o r r i g h t
c h i l d .
parent
rightleft
A b i n a r y t r e e c a n b e i m p l e m e n t e d w h e r e e a c h n o d e
h a s l e f t a n d r i g h t p o i n t e r e l d s , a n o p t i o n a l p a r e n t
p o i n t e r , a n d a d a t a e l d .
-
7/29/2019 Lecture 8 - Binary Trees
4/19
B i n a r y S e a r c h T r e e s
A b i n a r y s e a r c h t r e e l a b e l s e a c h n o d e i n a b i n a r y t r e e
w i t h a s i n g l e k e y s u c h t h a t f o r a n y n o d e x , a n d n o d e s
i n t h e l e f t s u b t r e e o f x h a v e k e y s x a n d a l l n o d e s i n
t h e r i g h t s u b t r e e o f x h a v e k e y ' s x
2
3
7
6 8
5
8
5
32
7
6
L e f t : A b i n a r y s e a r c h t r e e . R i g h t : A h e a p b u t n o t a
b i n a r y s e a r c h t r e e .
T h e s e a r c h t r e e l a b e l i n g e n a b l e s u s t o n d w h e r e a n y
k e y i s . S t a r t a t t h e r o o t - i f t h a t i s n o t t h e o n e w e w a n t ,
s e a r c h e i t h e r l e f t o r r i g h t d e p e n d i n g u p o n w h e t h e r w h a t
w e w a n t i s o r t h e n r o o t .
-
7/29/2019 Lecture 8 - Binary Trees
5/19
S e a r c h i n g i n a B i n a r y T r e e
D i c t i o n a r y s e a r c h o p e r a t i o n s a r e e a s y i n b i n a r y t r e e s . . .
T R E E - S E A R C H x , k
i f x = N I L o r k = k e y x
t h e n r e t u r n x
i f k k e y x
t h e n r e t u r n T R E E - S E A R C H l e f t x , k
e l s e r e t u r n T R E E - S E A R C H r i g h t x , k
T h e a l g o r i t h m w o r k s b e c a u s e b o t h t h e l e f t a n d r i g h t
s u b t r e e s o f a b i n a r y s e a r c h t r e e a r e b i n a r y s e a r c h t r e e s
r e c u r s i v e s t r u c t u r e , r e c u r s i v e a l g o r i t h m .
T h i s t a k e s t i m e p r o p o r t i o n a l t o t h e h e i g h t o f t h e t r e e ,
O h
-
7/29/2019 Lecture 8 - Binary Trees
6/19
M a x i m u m a n d M i n i m u m
W h e r e a r e t h e m a x i m u m a n d m i n i m u m e l e m e n t s i n a
b i n a r y t r e e ?
T R E E - M A X I M U M X
w h i l e r i g h t x 6= N I L
d o x = r i g h t x
r e t u r n x
T R E E - M I N I M U M x
w h i l e l e f t x 6= N I L
d o x = l e f t x
r e t u r n x
B o t h t a k e t i m e p r o p o r t i o n a l t o t h e h e i g h t o f t h e t r e e ,
O h
-
7/29/2019 Lecture 8 - Binary Trees
7/19
W h e r e i s t h e p r e d e c e s s o r ?
W h e r e i s t h e p r e d e c e s s o r o f a n o d e i n a t r e e , a s s u m i n g
a l l k e y s a r e d i s t i n c t ?
X
PREDECESSOR(X) SUCCESSOR(X)
I f X h a s t w o c h i l d r e n , i t s p r e d e c e s s o r i s t h e m a x i m u m
v a l u e i n i t s l e f t s u b t r e e a n d i t s s u c c e s s o r t h e m i n i m u m
v a l u e i n i t s r i g h t s u b t r e e .
-
7/29/2019 Lecture 8 - Binary Trees
8/19
X
predecessor(x)
I f i t d o e s n o t h a v e a l e f t c h i l d , a n o d e ' s p r e d e c e s s o r i s
i t s r s t l e f t a n c e s t o r .
T h e p r o o f o f c o r r e c t n e s s c o m e s f r o m l o o k i n g a t t h e
i n - o r d e r t r a v e r s a l o f t h e t r e e .
-
7/29/2019 Lecture 8 - Binary Trees
9/19
H
AF
GB
D
C E
T r e e - S u c c e s s o r x
i f r i g h t x 6= N I L
t h e n r e t u r n T r e e - M i n i m u m r i g h t x
y p x
w h i l e y 6= N I L a n d x = r i g h t y
d o x y
y p y
r e t u r n y
T r e e p r e d e c e s s o r s u c c e s s o r b o t h r u n i n t i m e p r o p o r -
t i o n a l t o t h e h e i g h t o f t h e t r e e .
I n o r d e r - T r e e - w a l k x
i f x N I L
t h e n I n o r d e r - T r e e - W a l k l e f t x
p r i n t k e y x
I n o r d e r - T r e e - w a l k r i g h t x
A - B - C - D - E - F - G - H
-
7/29/2019 Lecture 8 - Binary Trees
10/19
T r e e I n s e r t i o n
D o a b i n a r y s e a r c h t o n d w h e r e i t s h o u l d b e , t h e n
r e p l a c e t h e t e r m i n a t i o n N I L p o i n t e r w i t h t h e n e w i t e m .
L E A F I N S E R T I O N F I G U R E
T r e e - i n s e r t T ; z
y = N I L
x = r o o t T
w h i l e x 6= N I L
d o y = x
i f k e y z k e y x
t h e n x = l e f t x
e l s e x = r i g h t x
p z y
i f y = N I L
t h e n r o o t T z
e l s e i f k e y z k e y y
t h e n l e f t y z
e l s e r i g h t y z
y i s m a i n t a i n e d a s t h e p a r e n t o f x , s i n c e x e v e n t u a l l y
b e c o m e s N I L .
T h e n a l t e s t e s t a b l i s h e s w h e t h e r t h e N I L w a s a l e f t
o r r i g h t t u r n f r o m y
I n s e r t i o n t a k e s t i m e p r o p o r t i o n a l t o t h e h e i g h t o f t h e
t r e e , O h
-
7/29/2019 Lecture 8 - Binary Trees
11/19
T r e e D e l e t i o n
D e l e t i o n i s s o m e w h a t m o r e t r i c k y t h a n i n s e r t i o n , b e -
c a u s e t h e n o d e t o d i e m a y n o t b e a l e a f , a n d t h u s e e c t
o t h e r n o d e s .
C a s e a , w h e r e t h e n o d e i s a l e a f , i s s i m p l e - j u s t N I L
o u t t h e p a r e n t s c h i l d p o i n t e r .
C a s e b , w h e r e a n o d e h a s o n e c h l d , t h e d o o m e d n o d e
c a n j u s t b e c u t o u t .
C a s e c , r e l a b e l t h e n o d e a s i t s s u c c e s s o r w h i c h h a s
a t m o s t o n e c h i l d w h e n z h a s t w o c h i l d r e n ! a n d d e l e t e
t h e s u c c e s s o r !
T h i s i m p l e m e n t a t i o n o f d e l e t i o n a s s u m e s p a r e n t p o i n t -
e r s t o m a k e t h e c o d e n i c e r , b u t i f y o u h a d t o s a v e s p a c e
t h e y c o u l d b e d i s p e n s e d w i t h b y k e e p i n g t h e p o i n t e r s
o n t h e s e a r c h p a t h s t o r e d i n a s t a c k .
T r e e - D e l e t e T ; z
i f l e f t z = N I L o r r i g h t z = N I L
t h e n y z
e l s e y T r e e - S u c c e s s o r z
i f l e f t y 6= N I L
t h e n x l e f t y
e l s e x r i g h t y
i f x 6= N I L
t h e n p x p y
i f p y = N I L
t h e n r o o t T x
e l s e i f y = l e f t p y
-
7/29/2019 Lecture 8 - Binary Trees
12/19
t h e n l e f t p y x
e l s e r i g h t p y x
i f y z
t h e n k e y z k e y y
* I f y h a s o t h e r e l d s , c o p y t h e m , t o o .
r e t u r n y
L i n e s 1 - 3 d e t e r m i n e w h i c h n o d e y i s p h y s i c a l l y r e m o v e d .
L i n e s 4 - 6 i d e n t i f y x a s t h e n o n - n i l d e c e n d a n t , i f a n y .
L i n e s 7 - 8 g i v e x a n e w p a r e n t .
L i n e s 9 - 1 0 m o d i f y t h e r o o t n o d e , i f n e c e s s a r y
L i n e s 1 1 - 1 3 r e a t t a c h t h e s u b t r e e , i f n e c e s s a r y .
L i n e s 1 4 - 1 6 i f t h e r e m o v e d n o d e i s d e l e t e d , c o p y .
C o n c l u s i o n : d e l e t i o n t a k e s t i m e p r o p o r t i o n a l t o t h e
h e i g h t o f t h e t r e e .
-
7/29/2019 Lecture 8 - Binary Trees
13/19
B a l a n c e d S e a r c h T r e e s
A l l s i x o f o u r d i c t i o n a r y o p e r a t i o n s , w h e n i m p l e m e n t e d
w i t h b i n a r y s e a r c h t r e e s , t a k e O h , w h e r e h i s t h e
h e i g h t o f t h e t r e e .
T h e b e s t h e i g h t w e c o u l d h o p e t o g e t i s l g n , i f t h e
t r e e w a s p e r f e c t l y b a l a n c e d , s i n c e
P
b l g n c
i = 0
2
i
n
B u t i f w e g e t u n l u c k y w i t h o u r o r d e r o f i n s e r t i o n o r
d e l e t i o n , w e c o u l d g e t l i n e a r h e i g h t !
i n s e r t a
i n s e r t b
i n s e r t c
i n s e r t d
A
B
C
D
I n f a c t , r a n d o m s e a r c h t r e e s o n a v e r a g e h a v e l g N
h e i g h t , b u t w e a r e w o r r i e d a b o u t w o r s t c a s e h e i g h t .
W e c a n ' t e a s i l y u s e r a n d o m i z a t i o n - W h y ?
-
7/29/2019 Lecture 8 - Binary Trees
14/19
P e r f e c t l y B a l a n c e d T r e e s
P e r f e c t l y b a l a n c e d t r e e s r e q u i r e a l o t o f w o r k t o m a i n -
t a i n :
9
5 13
11 15
1412108
7
64
3
2
1
16
I f w e i n s e r t t h e k e y 1 , w e m u s t m o v e e v e r y s i n g l e n o d e
i n t h e t r e e t o r e b a l a n c e i t , t a k i n g n t i m e .
T h e r e f o r e , w h e n w e t a l k a b o u t " b a l a n c e d " t r e e s , w e
m e a n t r e e s w h o s e h e i g h t i s O l g n , s o a l l d i c t i o n a r y
o p e r a t i o n s i n s e r t , d e l e t e , s e a r c h , m i n m a x , s u c c e s -
s o r p r e d e c e s s o r t a k e O l g n t i m e .
R e d - B l a c k t r e e s a r e b i n a r y s e a r c h t r e e s w h e r e e a c h
n o d e i s a s s i g n e d a c o l o r , w h e r e t h e c o l o r i n g s c h e m e
h e l p s u s m a i n t a i n t h e h e i g h t a s l g n
-
7/29/2019 Lecture 8 - Binary Trees
15/19
R e d - B l a c k T r e e D e n i t i o n
R e d - b l a c k t r e e s h a v e t h e f o l l o w i n g p r o p e r t i e s :
1 . E v e r y n o d e i s c o l o r e d e i t h e r r e d o r b l a c k .
2 . E v e r y l e a f N I L p o i n t e r i s b l a c k .
3 . I f a n o d e i s r e d t h e n b o t h i t s c h i l d r e n a r e b l a c k .
4 . E v e r y s i n g l e p a t h f r o m a n o d e t o a d e c e n d a n t l e a f
c o n t a i n s t h e s a m e n u m b e r o f b l a c k n o d e s .
-
7/29/2019 Lecture 8 - Binary Trees
16/19
W h a t d o e s t h i s m e a n ?
I f t h e r o o t o f a r e d - b l a c k t r e e i s b l a c k c a n w e j u s t c o l o r
i t r e d ?
N o ! F o r o n e o f i t s c h i l d r e n m i g h t b e r e d .
I f a n a r b i t r a r y n o d e i s r e d c a n w e c o l o r i t b l a c k ?
N o ! B e c a u s e n o w a l l n o d e s m a y n o t h a v e t h e s a m e
b l a c k h e i g h t .
W h a t t r e e m a x i m i z e s t h e n u m b e r o f n o d e s i n a t r e e o f
b l a c k h e i g h t h ?
-
7/29/2019 Lecture 8 - Binary Trees
17/19
W h a t d o e s a r e d - b l a c k t r e e w i t h t w o r e a l n o d e s l o o k
l i k e ?
(1) (2) (3) (4)
N o t 1 - c o n s e c u t i v e r e d s N o t 2 , 4 - N o n - U n i f o r m
b l a c k h e i g h t
-
7/29/2019 Lecture 8 - Binary Trees
18/19
R e d - B l a c k T r e e H e i g h t
L e m m a : A r e d - b l a c k t r e e w i t h n i n t e r n a l n o d e s h a s
h e i g h t a t m o s t 2 l g n + 1 .
P r o o f : O u r s t r a t e g y ; r s t w e b o u n d t h e n u m b e r o f
n o d e s i n a n y s u b t r e e , t h e n w e b o u n d t h e h e i g h t o f a n y
s u b t r e e .
W e c l a i m t h a t a n y s u b t r e e r o o t e d a t x h a s a t l e a s t
2
b h x
- 1 i n t e r n a l n o d e s , w h e r e b h x i s t h e b l a c k h e i g h t
o f n o d e x
P r o o f , b y i n d u c t i o n :
b h x = 0 ! x i s a l e a f , ! 2
0
, 1 = 0
N o w a s s u m e i t i s t r u e f o r a l l t r e e w i t h b l a c k h e i g h t
b h x
I f x i s b l a c k , b o t h s u b t r e e s h a v e b l a c k h e i g h t b h x , 1
I f x i s r e d , t h e s u b t r e e s h a v e b l a c k h e i g h t b h x
T h e r e f o r e , t h e n u m b e r o f i n t e r n a l n o d e s i n a n y s u b t r e e
i s
n 2
b h x , 1
, 1 + 2
b h x , 1
, 1 + 1 2
b h x
, 1
-
7/29/2019 Lecture 8 - Binary Trees
19/19
N o w , l e t h b e t h e h e i g h t o f o u r r e d - b l a c k t r e e . A t
l e a s t h a l f t h e n o d e s o n a n y s i n g l e p a t h f r o m r o o t t o
l e a f m u s t b e b l a c k i f w e i g n o r e t h e r o o t .
T h u s b h x h = 2 a n d n 2
h = 2
, 1 , s o n + 1 2
h = 2
T h i s i m p l i e s t h a t l g n + 1 h = 2 , s o h 2 l g n + 1 .
T h e r e f o r e r e d - b l a c k t r e e s h a v e h e i g h t a t m o s t t w i c e
o p t i m a l . W e h a v e a b a l a n c e d s e a r c h t r e e i f w e c a n
m a i n t a i n t h e r e d - b l a c k t r e e s t r u c t u r e u n d e r i n s e r t i o n
a n d d e l e t i o n .