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
play

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


  1. A V L t r e e s ( W e i s s 4 . 4 )

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

  3. B a l a n c e d B S T s : t h e s o l u t i o 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 o f t r e e m u s t b e O ( l o g n ) ● Tie n a l l o p e r a t i o n s w i l l t a k e O ( l o g n ) t i m e O n e p o s s i b l e i d e a f o r a n i n v a r i a n t : ● H e i g h t o f l e f t c h i l d = h e i g h t o f r i g h t c h i l d ( f o r a l l n o d e s i n t h e t r e e ) ● T r e e w o u l d b e s o r t o f “ p e r f e c t l y b a l a n c e d ” Wh a t ' s w r o n g w i t h t h i s i d e a ?

  4. A t o o 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 o o r e s t r i c t i v e ! N u m b e r o f n o d e s c a n o n l y b e 1 , 3 , 7 , 1 5 , 3 1 , . . . o w l o w l p e n g u i n p e n g u i n h a m s t e r h a m s t e r g o r i l l a l e m u r p a n d a w o l f g o r i l l a l e m u r w o l f p a n d a

  5. 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 o v e r e d ( f r o m 1 9 6 2 ) – i t ' s n a m e d a f t e r A d e l s o 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 o l l o 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 o f a n y n o d e i s a t m o s t 1 ● ( c o m p a r e d t o 0 f o 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 o g n ) , s o i t ' s b a l a n c e d

  6. E x a m p l e o f a n A V L t r e e ( f r o m W i k i p e d i a ) L e f 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 2 R i g h 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 2 50 17 72 12 23 54 76 9 14 19 67 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 0

  7. W h y a r e t h e s e n o t A V L t r e e s ?

  8. W h y a r e t h e s e n o t A V L t r e e s ? L e f t c h i l d h e i g h t 0 R i g h t c h i l d h e i g h t 8

  9. W h y a r e t h e s e n o 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

  10. R o t a t i o n R o t a t i o n r e a r r a n g e s a B S T b y m o v i n g a d i fg e r e n t n o d e t o t h e r o o t , w i t h o u t c h a n g i n g t h e B S T ' s c o n t e n t s ( p i c f r o m W i k i p e d i a )

  11. R o t a t i o n W e c a n s t r a t e g i c a l l y u s e r o t a t i o n s t o 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 o s t b a l a n c e d B S T v a r i a n t s d o ! H e i g h t o f 3 H e i g h t o f 4

  12. A V L i n s e r t i o n S t a r t b y d o i n g a B S T i n s e r t i o 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 o u p w a r d s f r o m t h e n e w l y - i n s e r t e d n o d e , l o o k i n g f o r n o 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 o d e s ) I f y o u fj n d o n e , r o t a t e i t t o fj x t h e b a l a n c e Tie r e a r e f o u r c a s e s d e p e n d i n g o n h o w t h e n o d e b e c a m e u n b a l a n c e d

  13. C a s e 1 : a l e f t - l e f t t r e e A s s u m p t i o n : t h i s t r e e N o t i c e t h a t t h e t r e e i s t h e d e e p e s t w a s b a l a n c e d 5 0 n o d e t h a t b e f o r e t h e p u r p l e v i o l a t e s b i t w a s a d d e d t h e i n v a r i a n t 2 5 c 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 a b 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 o 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 o f t r e e a t o k + 1

  14. C a s e 1 : a l e f t - l e f t t r e e H e i g h t k H e i g h t k + 2 5 0 2 5 c a b 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 o k e n !

  15. C a s e 1 : a l e f t - l e f t t r e e Tii s i s c a l l e d a l e f t - l e f t t r e e 5 0 b e c a u s e b o t h t h e r o o 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 2 5 o n t h e l e f t c a b T o fj x i t w e d o a r i g h t r o t a t i o n

  16. 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 H e i g h t k + 1 H e i g h t k + 1 2 5 5 0 I n v a r i a n t r e s t o r e d ! a c b

  17. C a s e 2 : a r i g h t - r i g h t t r e e M i r r o r i m a g e o f l e f t - l e f t t r e e 2 5 C a n b e fj x e d w i t h l e f t r o t a t i o n 5 0 a c b

  18. C a s e 3 : a l e f t - r i g h t t r e e H e i g h t k H e i g h t k + 2 5 0 2 5 c a b 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 o k e n !

  19. C a s e 3 : a l e f t - r i g h t t r e e 5 0 2 5 c W e c a n ' t fj x t h i s w i t h a b o n e r o t a t i o n L e t ' s l o o k a t b ' s s u b t r e e s b a n d b L R

  20. C a s e 3 : a l e f t - r i g h t t r e e 5 0 2 5 c 4 0 a b b R o t a t e 2 5 - s u b t r e e t o t h e l e f t R L H e i g h t k - 1

  21. C a s e 3 : a l e f t - r i g h t t r e e H H e e i i g g h h t t k k + + 2 2 H e i g h t k 5 0 4 0 H e i g h t k + 1 c 2 5 b R W e n o w h a v e a l e f t - l e f t t r e e ! S o w e c a n fj x i t b y b L r o t a t i n g t h e w h o l e t r e e a t o t h e r i g h t

  22. C a s e 3 : a l e f t - r i g h t t r e e B a l a n c e d ! 4 0 N o t i c e i t w o r k s w h i c h e v e r o f b a n d b h a s t h e L R e x t r a h e i g h t 2 5 5 0 b b R L a c

  23. C a s e 4 : a r i g h t - l e f t t r e e M i r r o r i m a g e o f 2 5 l e f t - r i g h t t r e e 5 0 a c b

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend