B i n a r y s e a r c h t r e e s B i n a r y - - PowerPoint PPT Presentation

b i n a r y s e a r c h t r e e s b i n a r y s e a r c h
SMART_READER_LITE
LIVE PREVIEW

B i n a r y s e a r c h t r e e s B i n a r y - - PowerPoint PPT Presentation

B i n a r y s e a r c h t r e e s B i n a r y s e a r c h t r e e s A ( B S T ) i s a b i n a r y t r e e b i n a r y s e a r c h t r e e w h e r e e a c h n o d e i


slide-1
SLIDE 1

B i n a r y s e a r c h t r e e s

slide-2
SLIDE 2

h

  • r

s e h

  • r

s e

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 ( B S T ) i s a b i n a r y t r e e w h e r e e a c h n

  • d

e i s g r e a t e r t h a n a l l i t s l e f t d e s c e n d a n t s , a n d l e s s t h a n a l l i t s r i g h t d e s c e n d a n t s

  • 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 a p e a p e h a m s t e r h a m s t e r

slide-3
SLIDE 3

h

  • r

s e h

  • r

s e

S e a r c h i n g i n a B S T

F i n d i n g a n e l e m e n t i n a B S T i s e a s y , b e c a u s e b y l

  • k

i n g a t t h e r

  • t

y

  • u

c a n t e l l w h i c h s u b t r e e t h e e l e m e n t i s i n

  • 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 a p e a p e h a m s t e r h a m s t e r

l e m u r m u s t b e i n l e f t s u b t r e e

  • f
  • w

l l e m u r m u s t b e i n r i g h t s u b t r e e

  • f

h a m s t e r

slide-4
SLIDE 4

S e a r c h i n g i n a b i n a r y s e a r c h t r e e

T

  • s

e a r c h f

  • r

t a r g e t i n a B S T :

  • I

f t h e t a r g e t m a t c h e s t h e r

  • t

n

  • d

e ' s d a t a , w e ' v e f

  • u

n d i t

  • I

f t h e t a r g e t i s l e s s t h a n t h e r

  • t

n

  • d

e ' s d a t a , r e c u r s i v e l y s e a r c h t h e l e f t s u b t r e e

  • I

f t h e t a r g e t i s g r e a t e r t h a n t h e r

  • t

n

  • d

e ' s d a t a , r e c u r s i v e l y s e a r c h t h e r i g h t s u b t r e e

  • I

f t h e t r e e i s e m p t y , f a i l

A B S T c a n b e u s e d t

  • i

m p l e m e n t a s e t ,

  • r

a m a p f r

  • m

k e y s t

  • v

a l u e s

slide-5
SLIDE 5

I n s e r t i n g i n t

  • a

B S T

T

  • i

n s e r t a v a l u e i n t

  • a

B S T :

  • S

t a r t b y s e a r c h i n g f

  • r

t h e v a l u e

  • B

u t w h e n y

  • u

g e t t

  • n

u l l ( t h e e m p t y t r e e ) , m a k e a n

  • d

e f

  • r

t h e v a l u e a n d p l a c e i t t h e r e

h

  • r

s e h

  • r

s e

  • 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 a p e a p e h a m s t e r h a m s t e r m

  • n

k e y m

  • n

k e y

slide-6
SLIDE 6

D e l e t i n g f r

  • m

a B S T

T

  • d

e l e t e a v a l u e i n t

  • a

B S T :

  • F

i n d t h e n

  • d

e c

  • n

t a i n i n g t h e v a l u e

  • I

f t h e n

  • d

e i s a l e a f , j u s t r e m

  • v

e i t

h

  • r

s e h

  • r

s e

  • 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 a p e a p e h a m s t e r h a m s t e r

T

  • d

e l e t e w

  • l

f , j u s t r e m

  • v

e t h i s n

  • d

e f r

  • m

t h e t r e e

slide-7
SLIDE 7

D e l e t i n g f r

  • m

a B S T , c

  • n

t i n u e d

I f t h e n

  • d

e h a s

  • n

e c h i l d , r e p l a c e t h e n

  • d

e w i t h i t s c h i l d

h

  • r

s e h

  • r

s e

  • 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 a p e a p e h a m s t e r h a m s t e r

T

  • d

e l e t e p e n g u i n , r e p l a c e i t i n t h e t r e e w i t h w

  • l

f

slide-8
SLIDE 8

D e l e t i n g f r

  • m

a B S T

T

  • d

e l e t e a v a l u e f r

  • m

a B S T :

  • F

i n d t h e n

  • d

e

  • I

f i t h a s n

  • c

h i l d r e n , j u s t r e m

  • v

e i t f r

  • m

t h e t r e e

  • I

f i t h a s

  • n

e c h i l d , r e p l a c e t h e n

  • d

e w i t h i t s c h i l d

  • I

f i t h a s t w

  • c

h i l d r e n . . . ? C a n ' t r e m

  • v

e t h e n

  • d

e w i t h

  • u

t r e m

  • v

i n g i t s c h i l d r e n t

  • !
slide-9
SLIDE 9

D e l e t i n g a n

  • d

e w i t h t w

  • c

h i l d r e n

D e l e t e t h e b i g g e s t v a l u e f r

  • m

t h e n

  • d

e ' s l e f t s u b t r e e a n d p u t t h i s v a l u e [ w h y t h i s

  • n

e ? ] i n p l a c e

  • f

t h e n

  • d

e w e w a n t t

  • d

e l e t e

D e l e t e

  • w

l b y r e p l a c i n g i t w i t h m

  • n

k e y D e l e t e m

  • n

k e y

h

  • r

s e h

  • r

s e

  • 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 a p e a p e h a m s t e r h a m s t e r m

  • n

k e y m

  • n

k e y

slide-10
SLIDE 10

D e l e t i n g a n

  • d

e w i t h t w

  • c

h i l d r e n

D e l e t e t h e b i g g e s t v a l u e f r

  • m

t h e n

  • d

e ' s l e f t s u b t r e e a n d p u t t h i s v a l u e [ w h y t h i s

  • n

e ? ] i n p l a c e

  • f

t h e n

  • d

e w e w a n t t

  • d

e l e t e

Tie r

  • t

i s n

  • w

m

  • n

k e y

h

  • r

s e h

  • r

s e m

  • n

k e y m

  • n

k e y 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 a p e a p e h a m s t e r h a m s t e r

slide-11
SLIDE 11

D e l e t i n g a n

  • d

e w i t h t w

  • c

h i l d r e n

H e r e i s t h e m

  • s

t c

  • m

p l i c a t e d c a s e :

T

  • d

e l e t e m

  • n

k e y , r e p l a c e i t b y l e m u r

h

  • r

s e h

  • r

s e m

  • n

k e y m

  • n

k e y 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 a p e a p e h a m s t e r h a m s t e r

B u t l e m u r h a s a c h i l d ! P u t h

  • r

s e w h e r e l e m u r w a s

slide-12
SLIDE 12

D e l e t i n g a n

  • d

e w i t h t w

  • c

h i l d r e n

H e r e i s t h e m

  • s

t c

  • m

p l i c a t e d c a s e :

T

  • d

e l e t e m

  • n

k e y , r e p l a c e i t b y l e m u r

h

  • r

s e h

  • r

s e m

  • n

k e y m

  • n

k e y 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 a p e a p e h a m s t e r h a m s t e r

B u t l e m u r h a s a c h i l d ! P u t h

  • r

s e w h e r e l e m u r w a s

slide-13
SLIDE 13

D e l e t i n g a n

  • d

e w i t h t w

  • c

h i l d r e n

H e r e i s t h e m

  • s

t c

  • m

p l i c a t e d c a s e :

l e m u r l e m u r h

  • r

s e h

  • r

s e 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 a p e a p e h a m s t e r h a m s t e r

slide-14
SLIDE 14

A b i g g e r e x a m p l e

Wh a t h a p p e n s i f w e d e l e t e i s ? c

  • w

? r a t ?

slide-15
SLIDE 15

D e l e t i n g a n

  • d

e w i t h t w

  • c

h i l d r e n

D e l e t i n g r a t , w e r e p l a c e i t w i t h p r i e s t ; n

  • w

w e h a v e t

  • d

e l e t e p r i e s t w h i c h h a s a c h i l d , m

  • r

n

slide-16
SLIDE 16

D e l e t i n g a n

  • d

e w i t h t w

  • c

h i l d r e n

F i n d a n d d e l e t e 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 a n d p u t t h a t v a l u e i n t h e d e l e t e d n

  • d

e

  • U

s i n g t h e b i g g e s t v a l u e p r e s e r v e s t h e i n v a r i a n t ( c h e c k y

  • u

u n d e r s t a n d w h y )

  • T
  • fj

n d t h e b i g g e s t v a l u e : r e p e a t e d l y d e s c e n d i n t

  • t

h e r i g h t c h i l d u n t i l y

  • u

fj n d a n

  • d

e w i t h n

  • r

i g h t c h i l d

  • Tie

b i g g e s t n

  • d

e c a n ' t h a v e t w

  • c

h i l d r e n , s

  • d

e l e t i n g i t i s e a s i e r

slide-17
SLIDE 17

C

  • m

p l e x i t y

  • f

B S T

  • p

e r a t i

  • n

s

A l l

  • u

r

  • p

e r a t i

  • n

s a r e O ( h e i g h t

  • f

t r e e ) Tii s m e a n s O ( l

  • g

n ) i f t h e t r e e i s b a l a n c e d , b u t O ( n ) i f i t ' s u n b a l a n c e d ( l i k e t h e t r e e

  • n

t h e r i g h t )

  • h
  • w

m i g h t w e g e t t h i s t r e e ?

B a l a n c e d B S T s 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 e t r e e i s b a l a n c e d

  • t

h e n a l l

  • p

e r a t i

  • n

s a r e O ( l

  • g

n )

slide-18
SLIDE 18

S u m m a r y

  • f

B S T s

B i n a r y t r e e s w i t h B S T i n v a r i a n t C a n b e u s e d t

  • i

m p l e m e n t s e t s a n d m a p s

  • l
  • k

u p : c a n e a s i l y fj n d a v a l u e i n t h e t r e e

  • i

n s e r t : p e r f

  • r

m a l

  • k

u p , t h e n p u t t h e n e w v a l u e a t t h e p l a c e w h e r e t h e l

  • k

u p w

  • u

l d s t

  • p
  • d

e l e t e : fj n d t h e v a l u e , t h e n r e m

  • v

e i t s n

  • d

e f r

  • m

t h e t r e e – s e v e r a l c a s e s d e p e n d i n g

  • n

h

  • w

m a n y c h i l d r e n t h e n

  • d

e h a s

C

  • m

p l e x i t y :

  • a

l l

  • p

e r a t i

  • n

s O ( h e i g h t

  • f

t r e e )

  • t

h a t i s , O ( l

  • g

n ) i f t r e e i s b a l a n c e d , O ( n ) i f u n b a l a n c e d

  • i

n s e r t i n g r a n d

  • m

d a t a t e n d s t

  • g

i v e b a l a n c e d t r e e s , s e q u e n t i a l d a t a g i v e s u n b a l a n c e d

  • n

e s

slide-19
SLIDE 19

A V L t r e e s

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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 ?

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

W h y a r e t h e s e n

  • t

A V L t r e e s ?

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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

slide-28
SLIDE 28

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

Wi k i p e d i a )

slide-29
SLIDE 29

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

slide-30
SLIDE 30

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

slide-31
SLIDE 31

C a s e 1 : a l e f t

  • l

e f t t r e e

50 c 25 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

slide-32
SLIDE 32

C a s e 1 : a l e f t

  • l

e f t t r e e

50 c 25 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 !

slide-33
SLIDE 33

C a s e 1 : a l e f t

  • l

e f t t r e e

50 c 25 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
slide-34
SLIDE 34

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

50 c 25 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 !

slide-35
SLIDE 35

C a s e 2 : a r i g h t

  • r

i g h t t r e e

25 a 50 b c

Mi 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
slide-36
SLIDE 36

C a s e 3 : a l e f t

  • r

i g h t t r e e

50 c 25 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 !

slide-37
SLIDE 37

C a s e 3 : a l e f t

  • r

i g h t t r e e

50 c 25 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

slide-38
SLIDE 38

C a s e 3 : a l e f t

  • r

i g h t t r e e

50 c 25 a

R

  • t

a t e 2 5

  • s

u b t r e e t

  • t

h e l e f t

40

b

R

b

L

H e i g h t k

  • 1
slide-39
SLIDE 39

C a s e 3 : a l e f t

  • r

i g h t t r e e

50 c 25 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

40

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

slide-40
SLIDE 40

C a s e 3 : a l e f t

  • r

i g h t t r e e

50 c 25 a 40

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

slide-41
SLIDE 41

C a s e 4 : a r i g h t

  • l

e f t t r e e

25 a 50 b c

Mi r r

  • r

i m a g e

  • f

l e f t

  • r

i g h t t r e e

slide-42
SLIDE 42

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

slide-43
SLIDE 43

T h e f

  • u

r c a s e s

( p i c t u r e f r

  • m

Wi 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 ffjc 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

slide-44
SLIDE 44

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

slide-45
SLIDE 45

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

slide-46
SLIDE 46

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

slide-47
SLIDE 47

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

slide-48
SLIDE 48

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

slide-49
SLIDE 49

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

slide-50
SLIDE 50

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

slide-51
SLIDE 51

L a z y d e l e t i

  • n

( n

  • t
  • n

e x a m)

D e l e t i n g f r

  • m

a B S T i s q u i t e h a r d . . . d e l e t i n g f r

  • m

a n A V L t r e e i s r e a l l y c

  • m

p l i c a t e d !

  • L
  • a

d s

  • f

c a s e s , s u p e r a n n

  • y

i n g

A l t e r n a t i v e : l a z y d e l e t i

  • n

K e e p t h e n

  • d

e , m a r k i t a s d e l e t e d !

  • S

e a r c h s i m p l y s k i p s

  • v

e r t h e n

  • d

e

  • O

p p

  • r

t u n i s t i c a l l y r e

  • u

s e t h e n

  • d

e i n i n s e r t i

  • n

I f y

  • u

m a r k a n

  • d

e w i t h t w

  • c

h i l d r e n a s d e l e t e d , s e a r c h i n g c a n b e c

  • m

e e x p e n s i v e

  • N

e e d t

  • s

e a r c h b

  • t

h c h i l d r e n

  • U

s e s a m e t r i c k a s f r

  • m

B S T d e l e t i

  • n

t

  • h

a n d l e t h i s c a s e !

slide-52
SLIDE 52

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

( n

  • t
  • n

e x a m)

D e l e t i n g “

  • v

e r ” :

q u i c k j u m p s d

  • g

t h e b r

  • w

n X X X f

  • x

l a z y

slide-53
SLIDE 53

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

( n

  • t
  • n

e x a m)

D e l e t i n g “ j u m p s ” :

q u i c k f

  • x

d

  • g

t h e b r

  • w

n X X X X X X l a z y

slide-54
SLIDE 54

L a z y d e l e t i

  • n

( n

  • t
  • n

e x a m)

I n t h e l e c t u r e w e d i s c

  • v

e r e d t h a t d

  • i

n g l a z y d e l e t i

  • n

j u s t l i k e t h i s d

  • e

s n ' t w

  • r

k !

  • E

. g . , d e l e t i n g f

  • x

w i l l n

  • w

n

  • l
  • n

g e r p r e s e r v e t h e i n v a r i a n t

I t h i n k t h e c

  • r

r e c t i n v a r i a n t i s : a d e l e t e d n

  • d

e ' s c h i l d r e n m u s t b e d e l e t e d E x e r c i s e : w

  • r

k

  • u

t h

  • w

t

  • d
  • i

n s e r t i

  • n

a n d d e l e t i

  • n

t

  • p

r e s e r v e t h i s i n v a r i a n t !

slide-55
SLIDE 55

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

n – s

  • l
  • k

u p s a r e q u i c k

I n s e r t i

  • n

– B S T i n s e r 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 D e l e t i

  • n

( s e e Wi k i p e d i a i f y

  • u

' r e i n t e r e s t e d ) – s i m i l a r i d e a b u t a b i t h a r d e r ( m

  • r

e c a s e s )

  • o

r u s e l a z y d e l e t i

  • n

I m p l e m e n t a t i

  • n

– s e e H a s k e l l c

  • m

p e n d i u m

  • n

c

  • u

r s e w e b s i t e ! 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

/ A V L t r e e . h t m l