SlideShare a Scribd company logo
1 of 30
Queues - Recap
Recap

• Queues work as you would expect
  – First in, First out - FIFO
  – Later items take their turn behind earlier
    ones.
• Because of the ‘no shuffling’ rule,
  these turn out to be tricky to
  implement
Classic rubbish Array-Style
                Queue

W      H    E        R   E   sp   T   H   E       sp




    Front of Queue                Rear of Queue        Limit


• Items are read off the front of the
  queue
Classic rubbish Array-Style
               Queue
Item = W


W    H       E    R     E   sp   T   H   E       sp




           Front of Queue        Rear of Queue        Limit


• Items are read off the front of the
  queue
Classic rubbish Array-Style
               Queue
Item = W


W    H       E    R     E   sp   T   H   E   sp      S




           Front of Queue            Rear of Queue       Limit


• And added to the rear
Classic rubbish Array-Style
              Queue
Item = WHER                                Warning: Queue Full!

W    H   E    R   E   sp   T       H   E    sp   S    T    R       E



                  Front of Queue                           Limit
                                                 Rear of Queue
• Until we run out of space
• Then we’re stuffed.
Brilliant Idea!
  Item = WHERE


   E    E       T   R   E   sp   T    H    E      sp   S   T   R       E




Rear of Queue                    Front of Queue                Limit




 • If you get to the end ..
 • Wrap round to the front
 • Until you hit the Rear pointer
Circular queue


   E                                                                 E
          E                                                    R
                T   R   E                         sp   S   T
                            sp   T    H    E

       Start                                                   End

Rear of Queue
                                 Front of Queue




 • It’s not really a circle!
Trees
• Dynamic structures
• Nodes in a hirearchy
Making a binary tree
                                Jason
• Put first item in root        Niels
• Insert items in order you     Richard
                                Steve
  get them:                     Philip
• If less than current          Andy
                                Callum
   – Go left
• If greater than current
                                     Jason
   – Go right
                              Andy           Niels

                                 Callum               Richard

                                             Philip       Steve
Exercise
• Create a binary tree with the following
  nodes:
     •   Goldfish
     •   Dove
     •   Robin
     •   Chaffinch
     •   Blackbird
     •   Wren
     •   Jay
     •   Sparrow
     •   Partridge
How it’s done
    Left Pointer       Right Pointer

1      6     Jason         2

2            Niels         3

3      5     Richard       4

4            Steve

5            Philip

6            Andy          7

7            Callum
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Moving around the tree

               Start here
               Is there a new left?
                        If yes, go left
                        And start over

               Is there a new right?
                        If yes, go left
                        And start over

               All done?
                           Print current
                           Go back up
Types of traversal
• Preorder                 • Postorder
  – Start at the root         – Do the left
  – Do the left               – Do the right
  – Do the right              – Go up to the root
• Inorder
  – Do the left subtree    • If the order is reversed, ie
  – Go up to the root        right is done before left,
  – Do the right subtree     we call it ‘reversed’ ….

More Related Content

Viewers also liked (15)

Erate and Funding Alts
Erate and Funding AltsErate and Funding Alts
Erate and Funding Alts
 
Que Trabajaremos 2009
Que Trabajaremos 2009Que Trabajaremos 2009
Que Trabajaremos 2009
 
Practica 5
Practica 5Practica 5
Practica 5
 
Practica 5
Practica 5Practica 5
Practica 5
 
Muntligt berättande
Muntligt berättandeMuntligt berättande
Muntligt berättande
 
La belleza azul
La belleza azulLa belleza azul
La belleza azul
 
Discontinued_ express scripts
Discontinued_ express scriptsDiscontinued_ express scripts
Discontinued_ express scripts
 
Монро Майлс - Понимание своего потенциала
Монро Майлс - Понимание своего потенциалаМонро Майлс - Понимание своего потенциала
Монро Майлс - Понимание своего потенциала
 
Hisham Branded
Hisham BrandedHisham Branded
Hisham Branded
 
Creating A Snap Academy Pdf
Creating A Snap Academy PdfCreating A Snap Academy Pdf
Creating A Snap Academy Pdf
 
Islarchitecture Slideshow
Islarchitecture SlideshowIslarchitecture Slideshow
Islarchitecture Slideshow
 
ApresentaçãO2
ApresentaçãO2ApresentaçãO2
ApresentaçãO2
 
Cea104
Cea104Cea104
Cea104
 
Domashenko
DomashenkoDomashenko
Domashenko
 
Treball De Civilitzacions
Treball De CivilitzacionsTreball De Civilitzacions
Treball De Civilitzacions
 

More from grahamwell (20)

Pseudocode
PseudocodePseudocode
Pseudocode
 
Excel =if function
Excel =if functionExcel =if function
Excel =if function
 
Excel Min max-average
Excel Min max-average Excel Min max-average
Excel Min max-average
 
What is binary and why do we use it?
What is binary and why do we use it?What is binary and why do we use it?
What is binary and why do we use it?
 
Introduction to touch develop
Introduction to touch developIntroduction to touch develop
Introduction to touch develop
 
Introduction to touch develop
Introduction to touch developIntroduction to touch develop
Introduction to touch develop
 
The software story
The software storyThe software story
The software story
 
Turtle graphics
Turtle graphicsTurtle graphics
Turtle graphics
 
Database field types
Database field typesDatabase field types
Database field types
 
Databases 101
Databases 101Databases 101
Databases 101
 
Kodu controls
Kodu controlsKodu controls
Kodu controls
 
Pascal names and types
Pascal names and typesPascal names and types
Pascal names and types
 
Python part two names and types
Python part two names and typesPython part two names and types
Python part two names and types
 
Abstraction - Year 9
Abstraction - Year 9Abstraction - Year 9
Abstraction - Year 9
 
Thinking about your project
Thinking about your projectThinking about your project
Thinking about your project
 
The rail fence
The rail fenceThe rail fence
The rail fence
 
Lesson 1
Lesson 1Lesson 1
Lesson 1
 
Rsa encryption
Rsa encryptionRsa encryption
Rsa encryption
 
Server side scripts
Server side scriptsServer side scripts
Server side scripts
 
Revision topic 1 sensors and control
Revision topic 1 sensors and controlRevision topic 1 sensors and control
Revision topic 1 sensors and control
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 

Recently uploaded (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 

Queues recap

  • 2. Recap • Queues work as you would expect – First in, First out - FIFO – Later items take their turn behind earlier ones. • Because of the ‘no shuffling’ rule, these turn out to be tricky to implement
  • 3. Classic rubbish Array-Style Queue W H E R E sp T H E sp Front of Queue Rear of Queue Limit • Items are read off the front of the queue
  • 4. Classic rubbish Array-Style Queue Item = W W H E R E sp T H E sp Front of Queue Rear of Queue Limit • Items are read off the front of the queue
  • 5. Classic rubbish Array-Style Queue Item = W W H E R E sp T H E sp S Front of Queue Rear of Queue Limit • And added to the rear
  • 6. Classic rubbish Array-Style Queue Item = WHER Warning: Queue Full! W H E R E sp T H E sp S T R E Front of Queue Limit Rear of Queue • Until we run out of space • Then we’re stuffed.
  • 7. Brilliant Idea! Item = WHERE E E T R E sp T H E sp S T R E Rear of Queue Front of Queue Limit • If you get to the end .. • Wrap round to the front • Until you hit the Rear pointer
  • 8. Circular queue E E E R T R E sp S T sp T H E Start End Rear of Queue Front of Queue • It’s not really a circle!
  • 9.
  • 10.
  • 11. Trees • Dynamic structures • Nodes in a hirearchy
  • 12. Making a binary tree Jason • Put first item in root Niels • Insert items in order you Richard Steve get them: Philip • If less than current Andy Callum – Go left • If greater than current Jason – Go right Andy Niels Callum Richard Philip Steve
  • 13. Exercise • Create a binary tree with the following nodes: • Goldfish • Dove • Robin • Chaffinch • Blackbird • Wren • Jay • Sparrow • Partridge
  • 14. How it’s done Left Pointer Right Pointer 1 6 Jason 2 2 Niels 3 3 5 Richard 4 4 Steve 5 Philip 6 Andy 7 7 Callum
  • 15. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 16. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 17. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 18. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 19. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 20. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 21. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 22. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 23. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 24. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 25. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 26. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 27. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 28. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 29. Moving around the tree Start here Is there a new left? If yes, go left And start over Is there a new right? If yes, go left And start over All done? Print current Go back up
  • 30. Types of traversal • Preorder • Postorder – Start at the root – Do the left – Do the left – Do the right – Do the right – Go up to the root • Inorder – Do the left subtree • If the order is reversed, ie – Go up to the root right is done before left, – Do the right subtree we call it ‘reversed’ ….