Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data."— Presentation transcript:

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

2 The Stack

3 Empty Stack with 7 Spaces Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer

4 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

5 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

6 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

7 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

8 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

9 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

10 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

11 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed ERROR! Stack Overflow Jack cannot be added

12 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)

13 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped Stephen remains in the stack but the pointer moves down

14 Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer We now wish to add Jack

15 Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed Jack overwrites Stephen and the stack pointer moves back up

16 Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped

17 Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped

18 Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped

19 Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped

20 Jack Alex Janine Andrew Ben Stelhan James Stack Pointer We now wish to add Jim Jim

21 Jack Alex Janine Jim Ben Stelhan James Stack Pointer Stack is Pushed

22 What if the stack is empty and you pop it? ERROR! Stack Underflow

23 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

24 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

25 The Queue Stephen Next item to take Next available location Andrew Stelhan James Ben Janine Alex Jack

26 The Queue Stephen Next item to take Next available location Andrew Stelhan James Ben Janine Alex Jack

27 The Queue Stephen Next item to take Next available location Andrew Stelhan James Ben JanineAlex Jack

28 The Queue Next item to take Next available location Andrew Stelhan James Ben JanineAlex Jack

29 The Queue Next item to take Next available location Stelhan James BenJanineAlex Jack

30 The Queue Next item to take Next available location James BenJanineAlex Jack

31 Circular Queue Next Location to take Next available location Steve Ben Jack James

32 Using a stack to reverse the elements of a queue

33 The Queue Back of queue Front of queue James BenJanineAlex Jack Stack

34 The Queue Back of queue Front of queue James Ben Janine Alex Jack Stack

35 The Queue Back of queue Front of queue James Ben JanineAlexJack Stack

36 The Binary Tree

37 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

38 ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew Ben Stelhan James

39 ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew Ben Stelhan James

40 ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew Ben Stelhan James Stelhan comes after James in the alphabet so goes right

41 ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew BenStelhan James Ben is before James in the alphabet so goes left

42 ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew BenStelhan James Andrew is before James so goes left, he is also before Ben so goes left again

43 ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Janine is after James but before Stelhan

44 ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Alex comes before James, Ben and Andrew

45 ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Stephen comes after James and Stelhan

46 ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Jack is before James and after Ben

47 Jack Stephen Alex JanineAndrew BenStelhan James Root Node Parent Child Node Leaf Node Left SubtreeRight Subtree

48 Traversing a binary Tree Preorder Traversal Inorder Traversal Postorder Traversal

49 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

50 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

51 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

52 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

53 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


Download ppt "Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data."

Similar presentations


Ads by Google