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