53
Dynamic Data Structures • Stacks, Queues and Binary Trees hold dynamic data

Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Embed Size (px)

Citation preview

Page 1: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Dynamic Data Structures

• Stacks, Queues and Binary Trees hold dynamic data

Page 2: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Stack

Page 3: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Empty Stack with 7 Spaces

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Page 4: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

JamesStack Pointer

Stack is Pushed

Page 5: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Pushed

Page 6: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Pushed

Page 7: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

JanineAndrew

Ben

Stelhan

James

Stack Pointer

Stack is Pushed

Page 8: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Pushed

Page 9: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Pushed

Page 10: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Pushed

Page 11: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Pushed

ERROR!

Stack Overflow

Jack cannot be added

Page 12: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

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)

Page 13: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Popped

Stephen remains in the stack but the pointer moves down

Page 14: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

We now wish to add Jack

Page 15: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Pushed

Jack overwrites Stephen and the stack pointer moves back up

Page 16: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Popped

Page 17: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Popped

Page 18: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Popped

Page 19: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

Stack is Popped

Page 20: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Alex

Janine

Andrew

Ben

Stelhan

James

Stack Pointer

We now wish to add Jim

Jim

Page 21: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack

Alex

Janine

Jim

Ben

Stelhan

James

Stack Pointer

Stack is Pushed

Page 22: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

What if the stack is empty and you pop it?

ERROR!

Stack Underflow

Page 23: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

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

Page 24: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

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

Page 25: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Queue

Stephen

Next item to take

Next available location

AndrewStelhan

James

Ben

Janine

Alex

Jack

Page 26: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Queue

Stephen

Next item to take

Next available location

AndrewStelhan

James

Ben

Janine

Alex

Jack

Page 27: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Queue

Stephen

Next item to take

Next available location

AndrewStelhan

James

Ben

JanineAlex

Jack

Page 28: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Queue

Next item to take

Next available location

AndrewStelhan

James

Ben

JanineAlex Jack

Page 29: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Queue

Next item to take

Next available location

Stelhan

James

BenJanineAlex Jack

Page 30: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Queue

Next item to take

Next available location

James BenJanineAlex Jack

Page 31: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Circular Queue

Next Location to take

Next available location

Steve

Ben

JackJames

Page 32: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Using a stack to reverse the elements of a queue

Page 33: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Queue

Back of queue

Front of queue

James BenJanineAlex Jack

Stack

Page 34: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Queue

Back of queue

Front of queue

James

Ben

Janine

Alex

Jack Stack

Page 35: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Queue

Back of queue

Front of queue

JamesBen Janine AlexJack

Stack

Page 36: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Binary Tree

Page 37: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

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

Page 38: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Page 39: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Page 40: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Jack

Stephen

Alex

Janine

Andrew

Ben

Stelhan

James

Stelhan comes after James in the alphabet so goes right

Page 41: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Jack

Stephen

Alex

Janine

Andrew

Ben Stelhan

James

Ben is before James in the alphabet so goes left

Page 42: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Jack

Stephen

Alex

Janine

Andrew

Ben Stelhan

James

Andrew is before James so goes left, he is also before Ben so goes left again

Page 43: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Jack

Stephen

Alex

JanineAndrew

Ben Stelhan

James

Janine is after James but before Stelhan

Page 44: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Jack

Stephen

Alex

JanineAndrew

Ben Stelhan

James

Alex comes before James, Ben and Andrew

Page 45: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Jack

Stephen

Alex

JanineAndrew

Ben Stelhan

James

Stephen comes after James and Stelhan

Page 46: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Jack Stephen

Alex

JanineAndrew

Ben Stelhan

James

Jack is before James and after Ben

Page 47: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Jack Stephen

Alex

JanineAndrew

Ben Stelhan

JamesRoot Node Parent Child Node

Leaf Node

Left Subtree Right Subtree

Page 48: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Traversing a binary Tree

• Preorder Traversal

• Inorder Traversal

• Postorder Traversal

Page 49: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Preorder Traversal

• Start at root node• Traverse the left hand subtree• Traverse the right hand subtree• D B A C F E G

C

B F

A E G

D

Page 50: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

Inorder Traversal

• Traverse the left hand subtree• Visit the root node• Traverse the right hand subtree• A B C D E F G

C

B F

A E G

D

Page 51: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

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

B F

A E G

D

Page 52: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

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

Page 53: Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

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