Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data
The Stack
Empty Stack with 7 Spaces Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed ERROR! Stack Overflow Jack cannot be added
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer We would now like to remove Stephen from the stack Stacks work on a last in first out principal (LIFO)
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped Stephen remains in the stack but the pointer moves down
Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer We now wish to add Jack
Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed Jack overwrites Stephen and the stack pointer moves back up
Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped
Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped
Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped
Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped
Jack Alex Janine Andrew Ben Stelhan James Stack Pointer We now wish to add Jim Jim
Jack Alex Janine Jim Ben Stelhan James Stack Pointer Stack is Pushed
What if the stack is empty and you pop it? ERROR! Stack Underflow
Uses of a stack When a program is being run, and another program interrupts it e.g. to access the hard disk, the details of the first program are put on the stack until the interruption is dealt with. The details are then taken off the stack (popped) in order to continue. Or if you are using a loop, the details of each successive loop may be stored on the stack
The Queue A good example is a print queue or when you type on a keyboard the letters typed are queued There are two pointers in a queue, the first points at the next item to be leave the queue and the other points at the location for the next item to go into the queue Unlike a stack, a queue uses FIFO – first in first out
The Queue Stephen Next item to take Next available location Andrew Stelhan James Ben Janine Alex Jack
The Queue Stephen Next item to take Next available location Andrew Stelhan James Ben Janine Alex Jack
The Queue Stephen Next item to take Next available location Andrew Stelhan James Ben JanineAlex Jack
The Queue Next item to take Next available location Andrew Stelhan James Ben JanineAlex Jack
The Queue Next item to take Next available location Stelhan James BenJanineAlex Jack
The Queue Next item to take Next available location James BenJanineAlex Jack
Circular Queue Next Location to take Next available location Steve Ben Jack James
Using a stack to reverse the elements of a queue
The Queue Back of queue Front of queue James BenJanineAlex Jack Stack
The Queue Back of queue Front of queue James Ben Janine Alex Jack Stack
The Queue Back of queue Front of queue James Ben JanineAlexJack Stack
The Binary Tree
ABCDEFGHIJKLMNOPQRSTUVWXYZ This alphabet is a visual aid to help us assemble our binary tree Jack Stephen Alex Janine Andrew Ben Stelhan James These names will be added to the Binary Tree
ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew Ben Stelhan James
ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew Ben Stelhan James
ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew Ben Stelhan James Stelhan comes after James in the alphabet so goes right
ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew BenStelhan James Ben is before James in the alphabet so goes left
ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew BenStelhan James Andrew is before James so goes left, he is also before Ben so goes left again
ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Janine is after James but before Stelhan
ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Alex comes before James, Ben and Andrew
ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Stephen comes after James and Stelhan
ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Jack is before James and after Ben
Jack Stephen Alex JanineAndrew BenStelhan James Root Node Parent Child Node Leaf Node Left SubtreeRight Subtree
Traversing a binary Tree Preorder Traversal Inorder Traversal Postorder Traversal
Preorder Traversal Start at root node Traverse the left hand subtree Traverse the right hand subtree D B A C F E G C BF AEG D
Inorder Traversal Traverse the left hand subtree Visit the root node Traverse the right hand subtree A B C D E F G C BF AEG D
Postorder Traversal Traverse the left hand subtree Traverse the right hand subtree Return to the root node A C B E G F D C BF AEG D
Questions What is meant by the term ‘pushing’ and ‘popping’ The name ‘Robert’ is pushed onto an empty stack, followed by ‘Felicity’, ‘Martin’ and ‘Sam’. What data will be on the stack after the following operations? Pop stack, push ‘Henry’, push ‘George’, pop stack. Explain the purpose of the stack pointer
Tasks Create a binary tree for the following people: Jim, Tom, Bob, Bill, Percy, Toby, John, Jack and Terry Create your own notes to explain a stack Create your own notes to explain a queue