b i n a r y h e a p s c h a p t e r s 2 0 3 2 0 5 l e f t
play

B i n a r y h e a p s ( c h a p t e r s 2 0 . - PowerPoint PPT Presentation

B i n a r y h e a p s ( c h a p t e r s 2 0 . 3 2 0 . 5 ) L e f t i s t h e a p s B i n a r y h e a p s a r e a r r a y s ! A b i n a r y h e a p i s r e a l l y i


  1. B i n a r y h e a p s ( c h a p t e r s 2 0 . 3 – 2 0 . 5 ) L e f t i s t h e a p s

  2. B i n a r y h e a p s a r e a r r a y s ! A b i n a r y h e a p i s r e a l l y i m p l e m e n t e d u s i n g a n a r r a y ! 8 P o s s i b l e b e c a u s e 18 29 o f c o m p l e t e n e s s p r o p e r t y 20 39 66 28 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89

  3. C h i l d p o s i t i o n s Tie l e f t c h i l d o f n o d e i . . . t h e r i g h t c h i l d i s a t i n d e x 2 i + 1 8 i s a t i n d e x 2 i + 2 i n t h e a r r a y . . . 18 29 39 66 20 28 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89 L. Child R. Child Parent

  4. C h i l d p o s i t i o n s Tie l e f t c h i l d o f n o d e i . . . t h e r i g h t c h i l d i s a t i n d e x 2 i + 1 8 i s a t i n d e x 2 i + 2 i n t h e a r r a y . . . 18 29 39 66 20 28 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89 L. Child R. Child Parent

  5. C h i l d p o s i t i o n s Tie l e f t c h i l d o f n o d e i . . . t h e r i g h t c h i l d i s a t i n d e x 2 i + 1 8 i s a t i n d e x 2 i + 2 i n t h e a r r a y . . . 18 29 39 66 20 28 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89 L. Child Parent R. Child

  6. P a r e n t p o s i t i o n Tie p a r e n t o f n o d e i 8 i s a t i n d e x ( i - 1 ) / 2 18 29 39 66 20 28 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89 Parent Child

  7. R e m i n d e r : i n s e r t i n g i n t o a b i n a r y h e a p T o i n s e r t a n e l e m e n t i n t o a b i n a r y h e a p : ● A d d t h e n e w e l e m e n t a t t h e e n d o f t h e h e a p ● S i f t t h e e l e m e n t u p : w h i l e t h e e l e m e n t i s l e s s t h a n i t s p a r e n t , s w a p i t w i t h i t s p a r e n t W e c a n d o e x a c t l y t h e s a m e t h i n g f o r a b i n a r y h e a p r e p r e s e n t e d a s a n a r r a y !

  8. I n s e r t i n g i n t o a b i n a r y h e a p S t e p 1 : a d d t h e n e w e l e m e n t t o t h e e n d o f t h e a r r a y , s e t t o i t s i n d e x c h i l d 6 29 18 39 66 20 28 37 26 76 32 74 89 8 Child 0 1 2 3 4 5 6 7 8 9 10 11 12 13 6 18 29 20 28 39 66 37 26 76 32 74 89 8

  9. I n s e r t i n g i n t o a b i n a r y h e a p S t e p 2 : c o m p u t e p a r e n t = ( c h i l d - 1 ) / 2 6 18 29 20 39 66 28 8 37 26 76 32 74 89 Parent Child 0 1 2 3 4 5 6 7 8 9 10 11 12 13 6 18 29 20 28 39 66 37 26 76 32 74 89 8

  10. I n s e r t i n g i n t o a b i n a r y h e a p S t e p 3 : i f > , a r r a y [ p a r e n t ] a r r a y [ c h i l d ] s w a p t h e m 6 18 29 20 39 8 28 66 37 26 76 32 74 89 Parent Child 0 1 2 3 4 5 6 7 8 9 10 11 12 13 6 18 29 20 28 39 8 37 26 76 32 74 89 66

  11. I n s e r t i n g i n t o a b i n a r y h e a p S t e p 4 : s e t , c h i l d = p a r e n t p a r e n t = – , a n d r e p e a t ( c h i l d 1 ) / 2 6 18 29 20 39 8 28 66 37 26 76 32 74 89 Parent Child 0 1 2 3 4 5 6 7 8 9 10 11 12 13 6 18 29 20 28 39 8 37 26 76 32 74 89 66

  12. I n s e r t i n g i n t o a b i n a r y h e a p S t e p 4 : s e t , c h i l d = p a r e n t p a r e n t = – , a n d r e p e a t ( c h i l d 1 ) / 2 6 18 8 20 39 29 28 66 37 26 76 32 74 89 Parent Child 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 28 39 29 37 26 76 32 74 89 66 6 18 8

  13. B i n a r y h e a p s a s a r r a y s B i n a r y h e a p s a r e “ m o r a l l y ” t r e e s ● Tii s i s h o w w e v i e w t h e m w h e n w e d e s i g n t h e h e a p a l g o r i t h m s B u t w e i m p l e m e n t t h e t r e e a s a n a r r a y ● Tie a c t u a l i m p l e m e n t a t i o n t r a n s l a t e s t h e s e t r e e c o n c e p t s t o u s e a r r a y s I n t h e r e s t o f t h e l e c t u r e , w e w i l l o n l y s h o w t h e h e a p s a s t r e e s ● B u t y o u s h o u l d h a v e t h e “ a r r a y v i e w ” i n y o u r h e a d w h e n l o o k i n g a t t h e s e t r e e s

  14. B u i l d i n g a h e a p O n e m o r e o p e r a t i o n , b u i l d h e a p ● T a k e s a n a r b i t r a r y a r r a y a n d m a k e s i t i n t o a h e a p ● I n - p l a c e : m o v e s t h e e l e m e n t s a r o u n d t o m a k e t h e h e a p p r o p e r t y h o l d I d e a : ● H e a p p r o p e r t y : e a c h e l e m e n t m u s t b e l e s s t h a n i t s c h i l d r e n ● I f a n o d e b r e a k s t h i s p r o p e r t y , w e c a n fj x i t b y s i f t i n g d o w n ● S o s i m p l y l o o p i n g t h r o u g h t h e a r r a y a n d s i f t i n g d o w n e a c h e l e m e n t i n t u r n o u g h t t o fj x t h e i n v a r i a n t ● B u t w h e n w e s i f t a n e l e m e n t d o w n , i t s c h i l d r e n m u s t a l r e a d y h a v e t h e h e a p p r o p e r t y ( o t h e r w i s e t h e s i f t i n g d o e s n ' t w o r k ) ● T o e n s u r e t h i s , l o o p t h r o u g h t h e a r r a y i n r e v e r s e

  15. B u i l d i n g a h e a p G o t h r o u g h e l e m e n t s i n r e v e r s e o r d e r , s i f t i n g e a c h d o w n 25 28 13 37 32 29 6 20 26 18 28 31 18

  16. B u i l d i n g a h e a p L e a v e s n e v e r n e e d s i f t i n g d o w n ! 25 28 13 37 32 29 6 20 26 18 28 31 18

  17. B u i l d i n g a h e a p 2 9 i s g r e a t e r t h a n 1 8 s o n e e d s s w a p p i n g 25 28 13 37 32 18 6 20 26 18 28 31 29

  18. B u i l d i n g a h e a p 3 2 i s g r e a t e r t h a n 1 8 s o n e e d s s w a p p i n g 25 28 13 37 18 18 6 20 26 32 28 31 29

  19. B u i l d i n g a h e a p 3 7 i s g r e a t e r t h a n 2 0 s o n e e d s s w a p p i n g 25 28 13 20 18 18 6 37 26 32 28 31 29

  20. B u i l d i n g a h e a p 2 8 i s g r e a t e r t h a n 6 s o n e e d s s w a p p i n g 25 6 13 20 18 18 28 37 26 32 28 31 29

  21. B u i l d h e a p c o m p l e x i t y Y o u w o u l d e x p e c t O ( n l o g n ) c o m p l e x i t y : ● n “ s i f t d o w n ” o p e r a t i o n s ● e a c h s i f t d o w n h a s O ( l o g n ) c o m p l e x i t y A c t u a l l y , i t ' s O ( n ) ! S e e b o o k 2 0 . 3 . ● ( R o u g h r e a s o n : s i f t i n g d o w n i s m o s t e x p e n s i v e f o r e l e m e n t s n e a r t h e r o o t o f t h e t r e e , b u t t h e v a s t m a j o r i t y o f e l e m e n t s a r e n e a r t h e l e a v e s )

  22. H e a p s o r t T o s o r t a l i s t u s i n g a h e a p : ● s t a r t w i t h a n e m p t y h e a p ● a d d a l l t h e l i s t e l e m e n t s i n t u r n ● r e p e a t e d l y fj n d a n d r e m o v e t h e s m a l l e s t e l e m e n t f r o m t h e h e a p , a n d a d d i t t o t h e r e s u l t l i s t ( t h i s i s a k i n d o f s e l e c t i o n s o r t ) H o w e v e r , t h i s a l g o r i t h m i s n o t i n - p l a c e . H e a p s o r t u s e s t h e s a m e i d e a , b u t w i t h o u t a l l o c a t i n g a n y e x t r a m e m o r y .

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