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
play

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


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

  2. 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 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 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 i n a B S T : t a r g e 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 l e s s 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 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 n u l l ( t h e e m p t y t r e e ) , m a k e a 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 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

  6. 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 i n t o 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 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 , c o n t i n u e d I f t h e n o d e h a s c h i l d , r e p l a c e t h e n o d e o n 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 a p e h o r s e h o r s e

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

  9. 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 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 ? ] s u b t r e 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 m o n k e y h o r s e m o n k e y

  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 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 ? ] s u b t r e 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

  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 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 a p e h o r s e h o r s e c h i l d ! P u t h o r s e w h e r e w a s l e m u r

  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 a p e h o r s e h o r s e c h i l d ! P u t h o r s e w h e r e w a s l e m u r

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

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

  15. 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 , w e r e p l a c e i t w i t h ; r a t p r i e s t n o w w e h a v e t o d e l e t e w h i c h h a s a p r i e s t c h i l d , m o r n

  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 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 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 s u b t r e e n o 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 o u u n d e r s t a n d w h y ) ● T o 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 o t h e r i g h t c h i l d u n t i l y o u fj n d a n o d e w i t h n o r i g h t c h i l d ● Tie b i g g e s t n o d e c a n ' t h a v e t w o c h i l d r e n , s o d e l e t i n g i t i s e a s i e r

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