SlideShare une entreprise Scribd logo
1  sur  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’ ….

Contenu connexe

En vedette (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
 

Plus de 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
 

Dernier

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
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
 
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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
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
 

Dernier (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
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
 
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
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
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
 
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
 
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
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 

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’ ….