T r i e s , r a d i x t r e e s , s u ffjx t r - - PowerPoint PPT Presentation

t r i e s r a d i x t r e e s s u ffjx t r e e s t r i e s
SMART_READER_LITE
LIVE PREVIEW

T r i e s , r a d i x t r e e s , s u ffjx t r - - PowerPoint PPT Presentation

T r i e s , r a d i x t r e e s , s u ffjx t r e e s T r i e s A t r i e ( p r o n o u n c e d t r y ) i s a d a t a s t r u c t u r e f o r r e p r e s e n t i n g a


slide-1
SLIDE 1

T r i e s , r a d i x t r e e s , s u ffjx t r e e s

slide-2
SLIDE 2

T r i e s

A t r i e ( p r

  • n
  • u

n c e d t r y ) i s a d a t a s t r u c t u r e f

  • r

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

  • f

s t r i n g s

  • I

t c a n a l s

  • b

e u s e d f

  • r

a m a p w h e r e t h e k e y s a r e s t r i n g s

  • O

r w h e r e t h e k e y i s a l i s t

  • f

s

  • m

e k i n d

I t i s a k i n d

  • f

t r e e , b u t n

  • t

b a s e d

  • n

c

  • m

p a r i s

  • n

s N a m e : p u n

  • n

r e t r i e v a l a n d t r e e

  • O

r i g i n a l l y p r

  • n
  • u

n c e d “ t r e e ” , b u t n

  • w

p r

  • n
  • u

n c e d “ t r y ” t

  • a

v

  • i

d c

  • n

f u s i

  • n

w i t h t r e e s …

slide-3
SLIDE 3

T r i e s

Tii s t r i e r e p r e s e n t s t h e s e t { “ c a t ” , “ c a t s ” , “ c

  • w

” , “ p i g ” , “ p i n ” } :

E d g e s l a b e l l e d w i t h c h a r a c t e r s . I n v a r i a n t : n

  • n
  • d

e h a s t w

  • e

d g e s w i t h t h e s a m e l a b e l D

  • u

b l e c i r c l e : c a t i s i n t h e s e t ( C

  • n

c a t e n a t e a l l t h e c h a r a c t e r s

  • n

t h e p a t h f r

  • m

t h e r

  • t

t

  • t

h i s n

  • d

e – c

  • a
  • t

) I n v a r i a n t : a l l l e a v e s a r e “ d

  • u

b l e c i r c l e d ” S i n g l e c i r c l e : “ c

n

  • t

i n t h e s e t

slide-4
SLIDE 4

T r i e s , m

  • r

e f

  • r

m a l l y

A t r i e i s a t r e e w h e r e e d g e s a r e l a b e l l e d w i t h c h a r a c t e r s

  • R

e p r e s e n t s a s e t

  • r

m a p

  • f

s t r i n g s

  • M
  • r

e g e n e r a l l y , k e y s c a n b e l i s t s ; t h e e d g e s a r e l a b e l l e d w i t h s i n g l e e l e m e n t s

E a c h n

  • d

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

  • Wh

i c h s t r i n g ? F

  • l

l

  • w

t h e p a t h f r

  • m

t h e r

  • t

t

  • t

h e n

  • d

e a n d c

  • n

c a t e n a t e t h e c h a r a c t e r s

  • n

t h

  • s

e e d g e s

S

  • m

e n

  • d

e s a r e m a r k e d a s c

  • r

r e s p

  • n

d i n g t

  • a

n e l e m e n t

  • f

t h e s e t

  • I

n d i a g r a m s : a d

  • u

b l e c i r c l e

I n v a r i a n t :

  • E

a c h n

  • d

e h a s a t m

  • s

t

  • n

e c h i l d l a b e l l e d w i t h a g i v e n e d g e

  • A

l l l e a f n

  • d

e s a r e “ d

  • u

b l e c i r c l e s ” ( t h e y r e p r e s e n t e l e m e n t s

  • f

t h e s e t )

slide-5
SLIDE 5

T r i e s

T

  • c

h e c k i f a s t r i n g i s i n t h e s e t , j u s t f

  • l

l

  • w

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

  • m

t h e r

  • t

!

D

  • u

b l e c i r c l e : “ c a t ” i s i n t h e s e t

slide-6
SLIDE 6

T r i e s

T

  • i

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

  • f
  • l

l

  • w

t h e e d g e s , m a k i n g n e w n

  • d

e s a s y

  • u

g

  • Tie

fj n a l n

  • d

e s h

  • u

l d b e a “ d

  • u

b l e c i r c l e ”

slide-7
SLIDE 7

T r i e s

I n s e r t i n g “ p i ” c r e a t e s n

  • n

e w n

  • d

e s , b u t w e m a r k t h e fj n a l n

  • d

e a s a “ d

  • u

b l e c i r c l e ”

slide-8
SLIDE 8

T r i e s

T

  • d

e l e t e a s t r i n g , w e fj r s t t u r n t h e n

  • d

e i n t

  • a

“ s i n g l e c i r c l e ” … E x a m p l e : d e l e t i n g “ c r

  • w

slide-9
SLIDE 9

T r i e s

I f t h e n

  • d

e i s a l e a f , w e s h

  • u

l d r e m

  • v

e i t . W e g

  • u

p t h e t r e e r e m

  • v

i n g a n y s i n g l e

  • c

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

  • r

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

slide-10
SLIDE 10

T r i e s –

  • t

h e r n e a t t h i n g s w e c a n d

  • G

i v e n a s e t

  • f

s t r i n g s s t

  • r

e d a s a t r i e , w e c a n :

  • F

i n d a l l s t r i n g s s t a r t i n g w i t h a g i v e n p r e fj x ( f

  • r

t h i s r e a s

  • n

a t r i e i s

  • f

t e n c a l l e d a p r e fj x t r e e )

W e c a n t a k e t h e u n i

  • n
  • r

i n t e r s e c t i

  • n
  • f

t w

  • t

r i e s

  • L

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

  • t

r i e s a r e m

  • s

t l y d i s j

  • i

n t

I f w e c a n i t e r a t e

  • v

e r a l l e d g e s

  • f

a n

  • d

e i n a l p h a b e t i c a l

  • r

d e r , w e c a n a l s

  • :
  • G

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

  • f

s t r i n g s i n d i c t i

  • n

a r y

  • r

d e r ( i . e . , w e c a n u s e a t r i e f

  • r

s

  • r

t i n g )

  • F

i n d a l l s t r i n g s l y i n g b e t w e e n t w

  • w
  • r

d s i n d i c t i

  • n

a r y

  • r

d e r ( e . g . , a l l w

  • r

d s i n t h e s e t t h a t a r e a f t e r “ c h i c k e n ” b u t b e f

  • r

e “ p i c k l e ” i n t h e d i c t i

  • n

a r y )

slide-11
SLIDE 11

T r i e s

T

  • fj

n d a l l s t r i n g s s t a r t i n g w i t h a p r e fj x , j u s t f

  • l

l

  • w

t h e e d g e s a l

  • n

g t h a t p r e fj x :

R e t u r n a l l w

  • r

d s i n t h i s s u b t r e e

slide-12
SLIDE 12

T r i e s

T

  • fj

n d a l l s t r i n g s b e t w e e n “ c h i c k e n ” a n d “ p i c k l e ” , j u s t f

  • l

l

  • w

a l l e d g e s t h a t l i e b e t w e e n t h e m i n d i c t i

  • n

a r y

  • r

d e r :

“ c a ” i s t

  • s

m a l l “ p i n ” i s t

  • b

i g

slide-13
SLIDE 13

T r i e s – i m p l e m e n t a t i

  • n

H

  • w

t

  • r

e p r e s e n t a t r i e ? N

  • t
  • b

v i

  • u

s :

  • E

d g e s a r e l a b e l l e d

  • E

a c h n

  • d

e c a n h a v e m a n y e d g e s

O n e r e a s

  • n

a b l e c h

  • i

c e : e a c h n

  • d

e c a r r i e s a m a p f r

  • m

l a b e l t

  • c

h i l d n

  • d

e

  • e

. g . , u s i n g a h a s h t a b l e m e a n s t h a t f

  • l

l

  • w

i n g a n e d g e w i l l t a k e O ( 1 ) t i m e

“ D

  • u

b l e c i r c l e s ” a r e r e c

  • r

d e d b y h a v i n g a B

  • l

e a n fj e l d i n t h e n

  • d

e

  • b

j e c t

  • I

f t h e t r i e i s u s e d a s a m a p , e a c h n

  • d

e

  • b

j e c t c a n c

  • n

t a i n a v a l u e

J u s t a s w i t h a n y t r e e d a t a s t r u c t u r e , t h e t r i e i t s e l f i s r e p r e s e n t e d a s a r e f e r e n c e t

  • t

h e r

  • t

n

  • d

e F a i r l y s i m p l e t

  • i

m p l e m e n t !

slide-14
SLIDE 14

T r i e s – p e r f

  • r

m a n c e

T r i e

  • p

e r a t i

  • n

s t a k e O ( w ) t i m e , w h e r e w i s t h e l e n g t h

  • f

t h e s t r i n g t

  • b

e i n s e r t e d

  • I

n d e p e n d e n t

  • f

t h e n u m b e r

  • f

s t r i n g s s t

  • r

e d i n t h e t r i e !

I s t h i s b e t t e r

  • r

w

  • r

s e t h a n B S T s ?

  • B

e t t e r i f t h e t r i e c

  • n

s i s t s

  • f

m a n y s h

  • r

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

  • d

e w i l l h a v e m a n y c h i l d r e n

  • W
  • r

s e i f t h e t r i e c

  • n

s i s t s

  • f

f e w l

  • n

g s t r i n g s , b e c a u s e m a n y n

  • d

e s w i l l

  • n

l y h a v e

  • n

e c h i l d

slide-15
SLIDE 15

T r i e s – a b a d c a s e f

  • r

p e r f

  • r

m a n c e

T r i e s c

  • n

t a i n i n g f e w l

  • n

g s t r i n g s p e r f

  • r

m w

  • r

s e t h a n B S T s M a n y n

  • d

e s h a v e

  • n

e c h i l d ! L

  • n

g c h a i n s

  • f

n

  • d

e s w i t h

  • u

t a n y b r a n c h i n g R a d i x t r e e s a r e a r e fj n e m e n t

  • f

t r i e s t h a t

  • n

l y i n t r

  • d

u c e n

  • d

e s w h e n b r a n c h i n g i s n e e d e d

slide-16
SLIDE 16

R a d i x t r e e s

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

  • m

p r e s s c h a i n s

  • f

n

  • d

e s i n t

  • a

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

slide-17
SLIDE 17

R a d i x t r e e s

F i n d i n g v a l u e s i n a r a d i x t r e e w

  • r

k s t h e s a m e a s i n a t r i e

  • I

m p

  • r

t a n t i n v a r i a n t : e a c h n

  • d

e

  • n

l y h a s

  • n

e

  • u

t g

  • i

n g e d g e s t a r t i n g w i t h e a c h l e t t e r !

  • C

a n a l s

  • m

a i n t a i n : e a c h n

  • n
  • d
  • u

b l e

  • c

i r c l e d n

  • d

e h a s a t l e a s t 2 c h i l d r e n

Tie

  • r

e m : n u m b e r

  • f

n

  • d

e s i s a t m

  • s

t 2 n , w h e r e n i s s i z e

  • f

s e t !

slide-18
SLIDE 18

R a d i x t r e e s

I n s e r t i

  • n

w

  • r

k s l i k e i n a t r i e , e x c e p t t h a t y

  • u

s

  • m

e t i m e s h a v e t

  • s

p l i t a n e d g e i n t

  • t

w

  • E

. g . t

  • i

n s e r t “ c a b b i e ” , w e h a v e t

  • s

p l i t “ b b a g e ” i n t

b b ” a n d “ a g e ” :

slide-19
SLIDE 19

R a d i x t r e e s – i m p l e m e n t a t i

  • n

T

  • n

a v i g a t e i n a r a d i x t r e e w e n e e d t

  • b

e a b l e t

  • l
  • k

u p a c h a r a c t e r a n d g e t t h e

  • u

t g

  • i

n g e d g e s t a r t i n g w i t h t h a t c h a r a c t e r S

  • ,

e a c h n

  • d

e s t

  • r

e s i t s

  • u

t g

  • i

n g e d g e s a s a m a p :

  • t

h e k e y i s t h e fj r s t c h a r a c t e r

  • f

t h e l a b e l

  • t

h e v a l u e i s a p a i r ( r e s t

  • f

t h e l a b e l , t a r g e t n

  • d

e )

A p a r t f r

  • m

t h a t , i m p l e m e n t a t i

  • n

i s s i m i l a r t

  • t

r i e s

  • M

a i n

  • t

h e r d i fg e r e n c e : s p l i t t i n g a n e d g e i n t w

slide-20
SLIDE 20

S u ffj x t r e e s

A s u ffj x t r e e i s a r a d i x t r e e t h a t s t

  • r

e s a l l s u ffj x e s

  • f

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

  • E

x a m p l e : s u ffj x e s

  • f

“ c a t i n t h e h a t ” a r e : “ c a t i n t h e h a t ” , “ a t i n t h e h a t ” , “ t i n t h e h a t ” , e t c .

Wh y ? C a n b e u s e d t

  • s

e a r c h f

  • r

a l l

  • c

c u r r e n c e s

  • f

g i v e n s u b s t r i n g i n a s t r i n g

  • I

n a r a d i x t r e e , y

  • u

c a n fj n d a l l s t r i n g s t h a t s t a r t w i t h a g i v e n p r e fj x

  • I

n a s u ffj x t r e e , y

  • u

c a n fj n d a l l s u ffj x e s

  • f

a s t r i n g t h a t s t a r t w i t h a g i v e n p r e fj x

  • Tii

s i s t h e s a m e a s fj n d i n g a l l

  • c

c u r r e n c e s

  • f

t h e p r e fj x

a t ” i s a s u b s t r i n g

  • f

“ c a t i n t h e h a t ” i f a n d

  • n

l y i f s

  • m

e s u ffj x

  • f

“ c a t i n t h e h a t ” s t a r t s w i t h “ a t ”

slide-21
SLIDE 21

S u ffj x t r e e s

A s u ffj x t r e e f

  • r

“ c a t i n t h e h a t ” : T

  • fj

n d “ a t ” i n “ c a t i n t h e h a t ” , l e t ’ s c h e c k w h i c h s u ffj x e s s t a r t w i t h “ a t ” . Tie r e a r e t w

  • :

a t i n t h e h a t ” a t

  • c

c u r s f

  • l

l

  • w

e d b y “ i n t h e h a t ” →

a t ” a t

  • c

c u r s f

  • l

l

  • w

e d b y t h e e n d

  • f

t h e s t r i n g →

F r

  • m

t h e l e n g t h

  • f

t h e s u ffj x w e c a n t e l l w h a t p

  • s

i t i

  • n

s “ a t ”

  • c

c u r s a t !

c a t i n t h e h a t ” . l e n g t h

a t i n t h e h a t ” . l e n g t h = 1

c a t i n t h e h a t ” . l e n g t h

a t ” . l e n g t h = 9

slide-22
SLIDE 22

S u ffj x t r e e s , i m p l e m e n t a t i

  • n

I f i m p l e m e n t e d c a r e l e s s l y , t h i s t a k e s O ( n

2

) m e m

  • r

y !

  • E

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

  • f

t h e i n p u t s t r i n g , w h i c h m a y t a k e O ( n ) m e m

  • r

y t

  • s

t

  • r

e

Tie t r i c k :

  • R

e m e m b e r t h e

  • r

i g i n a l i n p u t s t r i n g

  • L

a b e l e a c h e d g e w i t h d a t a t h a t r e c

  • r

d s w h i c h s u b s t r i n g

  • f

t h e i n p u t s t r i n g i t i s O n e w a y : a p a i r ( p

  • s

i t i

  • n

i n i n p u t s t r i n g , l e n g t h ) e . g . “ i n t h e h ” w

  • u

l d b e c

  • m

e ( 3 , 6 ) – s t a r t s a t i n d e x 3

  • f

“ c a t i n t h e h a t ” a n d g

  • e

s

  • n

f

  • r

6 c h a r a c t e r s

slide-23
SLIDE 23

S u ffj x t r e e s , i m p l e m e n t a t i

  • n

( n e e d t

  • a

l s

  • r

e m e m b e r t h a t t h e i n p u t s t r i n g i s c a t i n t h e h a t ) Tii s t a k e s O ( n ) m e m

  • r

y ! ( R e c a l l t h a t a r a d i x t r e e c

  • n

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

  • s

t 2 n n

  • d

e s )

slide-24
SLIDE 24

R a d i x t r e e s f

  • r

n u m b e r s ( n

  • t
  • n

e x a m )

Y

  • u

c a n v i e w a n i n t e g e r a s a s e q u e n c e

  • f

d i g i t s

  • e

. g . 1 2 3 4 5 [ 1 , 2 , 3 , 4 , 5 ] →

S

  • y
  • u

c a n u s e r a d i x t r e e s t

  • s

t

  • r

e s e t s

  • f

n u m b e r s ! E x a m p l e : { 1 2 3 , 4 5 6 , 1 2 4 5 6 } N

  • t

e : w e p a d t h e n u m b e r s w i t h l e a d i n g z e r

  • e

s w h e n n e c e s s a r y , s

  • t

h a t w e c a n d

  • r

a n g e q u e r i e s l i k e “ fj n d a l l t h e n u m b e r s b e t w e e n 1 a n d 5 ”

slide-25
SLIDE 25

R a d i x t r e e s f

  • r

n u m b e r s , i m p l e m e n t a t i

  • n

( n

  • t
  • n

e x a m )

W e c a n u s e s e v e r a l t r i c k s t

  • i

m p l e m e n t r a d i x t r e e s f

  • r

n u m b e r s s u p e r

  • e

ffj c i e n t l y !

  • I

n s t e a d

  • f

s t

  • r

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

  • f

e a c h n

  • d

e i n a m a p , s t

  • r

e t h e m i n a n a r r a y

  • f

s i z e 1 (

  • n

e f

  • r

e a c h d i g i t )

  • S

t

  • r

e s t r i n g s

  • f

d i g i t s a s a p a i r ( n u m b e r , l e n g t h

  • f

n u m b e r ) e . g . [ , 1 , 3 ] b e c

  • m

e s ( 1 3 , 3 )

  • D
  • n

’ t u s e b a s e 1 b u t ( e . g . ) b a s e 1 6 , s

  • t

h a t w e c a n u s e e . g . b i t

  • s

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

  • f

d i v i s i

  • n

– i n

  • t

h e r w

  • r

d s , w e v i e w a n i n t e g e r a s a l i s t

  • f

4

  • b

i t n u m b e r s ( “ h e x a d e c i m a l d i g i t s ” )

slide-26
SLIDE 26

R a d i x t r e e s f

  • r

s t r i n g s , u s i n g r a d i x t r e e s f

  • r

n u m b e r s

I f w e h a v e a r a d i x t r e e f

  • r

l i s t s

  • f

4

  • b

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

  • s

t

  • r

e s t r i n g s ! W e c a n v i e w a s t r i n g a s a s e r i e s

  • f

4

  • b

i t n u m b e r s :

  • E

a c h c h a r a c t e r h a s a c h a r a c t e r c

  • d

e , w h i c h i s a n 8 t

  • 3

2

  • b

i t n u m b e r ( d e p e n d i n g

  • n

t h e e n c

  • d

i n g )

  • C

h

  • p

u p t h e s t r i n g i n t

  • a

l i s t

  • f

c h a r a c t e r c

  • d

e s e . g . “ h e l l

[ 1 4 , 1 1 , 1 8 , 1 8 , 1 1 1 ] →

  • C

h

  • p

u p e a c h c h a r a c t e r c

  • d

e i n t

  • 4
  • b

i t p i e c e s e . g . 1 8 = ( b i n a r y ) 1 1 1 1 = [ 1 1 , 1 1 ]

  • N
  • w

y

  • u

h a v e a s e r i e s

  • f

4

  • b

i t n u m b e r s

F

  • r

e ffj c i e n c y , r a d i x t r e e s f

  • r

s t r i n g s a r e

  • f

t e n i m p l e m e n t e d t h i s w a y !

slide-27
SLIDE 27

S u m m a r y

R a d i x t r e e s c a n b e u s e d t

  • i

m p l e m e n t s e t s

  • r

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

  • e

. g . s t r i n g s ,

  • r

n u m b e r s t r e a t e d a s s t r i n g s

  • f

b a s e

  • 1

6 “ d i g i t s ”

  • R

e q u i r e s a m a p d a t a s t r u c t u r e f

  • r

l i s t e l e m e n t s ,

  • f

t e n i m p l e m e n t e d a s a n a r r a y

T i m e t a k e n b y e a c h

  • p

e r a t i

  • n

i s l

  • w

!

  • O

( m i n ( w , l

  • g

n ) ) w h e r e w i s l e n g t h

  • f

s t r i n g , n i s s i z e

  • f

s e t

T r i e s a r e s i m p l e r t

  • i

m p l e m e n t , b u t h a v e O ( w ) p e r f

  • r

m a n c e B

  • t

h a l s

  • s

u p p

  • r

t : fj n d i n g s t r i n g s s t a r t i n g w i t h a g i v e n p r e fj x , r a n g e q u e r i e s

  • A

l s

  • u

n i

  • n

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

  • n

, w h i c h w e d i d n ’ t s e e

S u ffj x t r e e s a r e r a d i x t r e e s w h i c h s t

  • r

e a l l s u ffj x e s

  • f

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

  • fj

n d a l l

  • c

c u r r e n c e s

  • f

a g i v e n s u b s t r i n g

  • A

s u ffj x t r e e c a n b e b u i l t i n O ( n ) t i m e ( w h i c h w e d i d n ’ t s e e )

  • Tie

n s e a r c h i n g t a k e s O ( l

  • g

n ) w

  • r

s t

  • c

a s e t i m e