Queues recap

513 views
400 views

Published on

A level presentation

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
513
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Queues recap

  1. 1. Queues - Recap
  2. 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. 3. Classic rubbish Array-Style QueueW 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. 4. Classic rubbish Array-Style QueueItem = WW 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. 5. Classic rubbish Array-Style QueueItem = WW H E R E sp T H E sp S Front of Queue Rear of Queue Limit• And added to the rear
  6. 6. Classic rubbish Array-Style QueueItem = 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. 7. Brilliant Idea! Item = WHERE E E T R E sp T H E sp S T R ERear of Queue Front of Queue Limit • If you get to the end .. • Wrap round to the front • Until you hit the Rear pointer
  8. 8. Circular queue E E E R T R E sp S T sp T H E Start EndRear of Queue Front of Queue • It’s not really a circle!
  9. 9. Trees• Dynamic structures• Nodes in a hirearchy
  10. 10. 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
  11. 11. Exercise• Create a binary tree with the following nodes: • Goldfish • Dove • Robin • Chaffinch • Blackbird • Wren • Jay • Sparrow • Partridge
  12. 12. How it’s done Left Pointer Right Pointer1 6 Jason 22 Niels 33 5 Richard 44 Steve5 Philip6 Andy 77 Callum
  13. 13. 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
  14. 14. 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
  15. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 28. 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’ ….

×