b i n a r y s e a r c h t r e e s w e i s s c h a p t e r
play

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

B i n a r y s e a r c h t r e e s ( W e i s s c h a p t e r 4 . 2 - 4 . 3 ) 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


  1. B i n a r y s e a r c h t r e e s ( W e i s s c h a p t e r 4 . 2 - 4 . 3 )

  2. 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 o d e i s g r e a t e r t h a n a l l t h e n o d e s i n t h e l e f t s u b t r e e , a n d l e s s t h a n a l l t h e n o d e s i n t h e r i g h t s u b t r e e 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 w o l f g o r i l l a l e m u r w o l f a p e h o r s e a p e h o r s e

  3. 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 o o k i n g a t t h e r o o t y o 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 l e m u r m u s t b e l e m u r m u s t b e o w l o w l i n r i g h t s u b t r e e i n l e f t s u b t r e e o f h a m s t e r o f 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 w o l f g o r i l l a l e m u r w o l f a p e a p e h o r s e h o r s e

  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 o s e a r c h f o 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 o o t n o d e ' s d a t a , w e ' v e f o u n d i t ● I f t h e t a r g e t i s t h a n t h e r o o t n o d e ' s d a t a , l e s s 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 o o t n o 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 o i m p l e m e n t a s e t , o r a m a p f r o m k e y s t o v a l u e s

  5. I n s e r t i n g i n t o a B S T T o i n s e r t a v a l u e i n t o a B S T : ● S t a r t b y s e a r c h i n g f o r t h e v a l u e ● B u t w h e n y o u g e t t o ( t h e e m p t y t r e e ) , m a k e a n u l l n o d e f o r t h e v a l u e a n d p l a c e i t t h e r e 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 w o l f g o r i l l a l e m u r w o l f a p e h o r s e a p e h o r s e m o n k e y m o n k e y

  6. F i n d i n g m i n i m u m / m a x i m u m i n a B S T T o fj n d t h e m a x i m u m v a l u e i n a B S T : ● R e p e a t e d l y g o r i g h t f r o m t h e r o o t ● Wh e n y o u r e a c h a n o d e w h o s e r i g h t c h i l d i s e m p t y , t h a t ’ s t h e m a x i m u m 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 w o l f g o r i l l a l e m u r w o l f p u ffj n a p e p u ffj n a p e h o r s e h o r s e

  7. D e l e t i n g f r o m a B S T T o d e l e t e a v a l u e f r o m a B S T : ● F i n d t h e n o d e c o n t a i n i n g t h e v a l u e ● I f t h e n o d e i s a l e a f , j u s t r e m o v e i t T o d e l e t e w o l f , j u s t r e m o v e o w l o w l t h i s n o d e f r o m t h e t r e e 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 w o l f g o r i l l a l e m u r w o l f a p e h o r s e a p e h o r s e

  8. D e l e t i n g f r o m a B S T , c o n t i n u e d I f t h e n o d e h a s o n e c h i l d , r e p l a c e t h e n o d e w i t h i t s c h i l d T o 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 o l f 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 w o l f g o r i l l a l e m u r w o l f a p e h o r s e a p e h o r s e

  9. D e l e t i n g f r o m a B S T T o d e l e t e a v a l u e f r o m a B S T : ● F i n d t h e n o d e ● I f i t h a s n o c h i l d r e n , j u s t r e m o v e i t f r o m t h e t r e e ● I f i t h a s o n e c h i l d , r e p l a c e t h e n o d e w i t h i t s c h i l d ● I f i t h a s t w o c h i l d r e n . . . ? C a n ' t r e m o v e t h e n o d e w i t h o u t r e m o v i n g i t s c h i l d r e n t o o !

  10. D e l e t i n g a n o d e w i t h t w o 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 o m t h e n o 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 o n e ? ] i n p l a c e o f t h e n o d e w e w a n t t o d e l e t e D e l e t e o w l b y r e p l a c i n g i t o w l o w l w i t h m o n k e y 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 w o l f g o r i l l a l e m u r w o l f D e l e t e m o n k e y a p e a p e h o r s e h o r s e m o n k e y m o n k e y

  11. D e l e t i n g a n o d e w i t h t w o 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 o m t h e n o 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 o n e ? ] i n p l a c e o f t h e n o d e w e w a n t t o d e l e t e Tie r o o t i s n o w m o n k e y m o n k e y m o n k e y 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 w o l f g o r i l l a l e m u r w o l f a p e a p e h o r s e h o r s e

  12. D e l e t i n g a n o d e w i t h t w o c h i l d r e n H e r e i s t h e m o s t c o m p l i c a t e d c a s e : T o d e l e t e m o n k e y , r e p l a c e i t b y l e m u r m o n k e y m o n k e y 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 w o l f g o r i l l a l e m u r w o l f B u t l e m u r h a s a a p e h o r s e a p e h o r s e c h i l d ! P u t h o r s e w h e r e l e m u r w a s

  13. D e l e t i n g a n o d e w i t h t w o c h i l d r e n H e r e i s t h e m o s t c o m p l i c a t e d c a s e : T o d e l e t e m o n k e y , r e p l a c e i t b y l e m u r m o n k e y m o n k e y 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 w o l f g o r i l l a l e m u r w o l f B u t l e m u r h a s a a p e h o r s e a p e h o r s e c h i l d ! P u t h o r s e w h e r e l e m u r w a s

  14. D e l e t i n g a n o d e w i t h t w o c h i l d r e n H e r e i s t h e m o s t c o m p l i c a t e d c a s e : l e m u r l e m u r 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 h o r s e w o l f g o r i l l a h o r s e w o l f a p e a p e

  15. 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 o w ? r a t ?

  16. D e l e t i n g a n o d e w i t h t w o 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 o w w e h a v e t o 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 o r n

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