data structures part 2 stacks, queues, trees, and graphs briana b. morrison cse 1302c spring 2010

68
Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

Upload: iliana-sansom

Post on 30-Mar-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

Data Structures Part 2

Stacks, Queues, Trees, and Graphs

Briana B. MorrisonCSE 1302C

Spring 2010

Page 2: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

2CSE 1302C

Topics• Stacks• Queues• Trees and Graphs

Page 3: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

3CSE 1302C

Classic Data Structures• Now we'll examine some classic data

structures

• Classic linear data structures include queues and stacks

• Classic nonlinear data structures include trees and graphs

Page 4: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

4CSE 1302C

A Stack Using a Linked List• A stack is a linear data structure that

organizes items in a last in, first out (LIFO) manner.

• Items are added and removed from only one end of a stack

• Analogies: cafeteria trays are typically organized in a stack, a stack of bills to be paid

• The tray at the top of the stack was put on the stack last, and will be taken off the stack first.

Page 5: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

5CSE 1302C

Stacks• Stacks often are drawn vertically:

poppush

Page 6: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

6CSE 1302C

Stacks• Some stack operations:

– push - add an item to the top of the stack– pop - remove an item from the top of the stack– peek (or top) - retrieves the top item without

removing it– empty - returns true if the stack is empty

• A stack can be represented by a singly-linked list

• A stack can be represented by an array, but the new item should be placed in the next available place in the array rather than at the end

Page 7: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

7CSE 1302C

A Stack can be Implemented using a Linked List

• In a stack implemented as a linked list:– To push, we insert at the beginning of the linked

list.– To pop, we delete the first item in the linked list.

Thus, deletion is not based on value; we always delete the first item in the list.

Page 8: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

8CSE 1302C

PlayerStackLinkedList MethodsReturn value Method name and argument listvoid push( Player p )

inserts Player p at the top of the stack Player pop( )

returns and removes the first Player of the list. If the list is empty, the method throws a DataStructureException.

Player peek( ) returns a copy of the first Player on the list without deleting it. If the list is empty, the method throws a DataStructureException.

Page 9: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

9CSE 1302C

A Stack Can be Implemented using an Array

• If we know in advance the maximum number of objects on the stack, we can represent the stack using an array. – This is easier to implement than a linked list.

• We add items to the stack starting at index 0.• We maintain an index top, short for top of the

stack. • The last element inserted is at index top.

Page 10: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

10CSE 1302C

Array Representing a Stack• There are three items on the stack:

index Player object

top 2 (8, Gino, Diablo )

1 ( 7, Sarah, Mario )

0 ( 2, Jin, Golf )

Page 11: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

11CSE 1302C

Stack after Inserting Player (6, Steve, NFL )

• Now there are four items on the stack:

index Player object

top 3 ( 6, Steve, NFL )

2 (8, Gino, Diablo )

1 ( 7, Sarah, Mario )

0 ( 2, Jin, Golf )

Page 12: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

12CSE 1302C

Our Stack After Popping Once • Now there are three items on the stack.

Player ( 6, Steve, NFL ) is not on the stack.index Player object

3 ( 6, Steve, NFL )

top 2 (8, Gino, Diablo )

1 ( 7, Sarah, Mario )

0 ( 2, Jin, Golf )

Page 13: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

13CSE 1302C

Instance Variables for Stack• We will have three instance variables:

– A constant STACK_SIZE, representing the capacity of the stack.

– An array stack, storing the elements of the stack.– An int top, representing the top of the stack.

public class ArrayStack { private static final int STACK_SIZE = 100; private Player [] stack; private int top; …. }

Page 14: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

14CSE 1302C

Constructor for our Stack• The constructor does two things:

– It instantiates the array, stack.– It initializes top to -1 to reflect that the stack is

empty. If top were initialized to 0, then there would be an element on the stack, at index 0.

public ArrayStack( ) { stack = new Player[STACK_SIZE]; top = -1; // stack is empty }

Page 15: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

15CSE 1302C

Utility Methods for our Stack• We will have three utility methods:

– isEmpty, returning true if the stack is empty.– isFull, returning true if the stack is full.– toString, returning a String representation of the

stack.

public bool isEmpty( ) { return ( top == -1 ); }

public bool isFull( ) { return ( top == ( STACK_SIZE – 1 ) ); }

Page 16: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

16CSE 1302C

toString Method for our Stack

public String toString( ) { String stackString = ""; for ( int i = top; i >= 0; i-- ) stackString += ( i + ": " + stack[i] + "\n" ); return stackString; }

Page 17: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

17CSE 1302C

push Method for our Stack public boolean push( Player p ) { if ( !isFull( ) ) { stack[++top] = p; return true; } else return false; }

Page 18: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

18CSE 1302C

pop Method for our Stack public Player pop { if ( !isEmpty( ) ) return ( stack[top--] ); else throw new DSException ( "Stack empty: cannot pop" ); }

Page 19: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

19CSE 1302C

Common Error Trap

• Do not confuse the top of the stack with the last index in the array. Array elements with array indexes higher than top are not on the stack.

Page 20: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

20CSE 1302C

Queues• A queue is similar to a list but adds items only to

the rear of the list and removes them only from the front

• It is called a FIFO data structure: First-In, First-Out

• Analogy: a line of people at a bank teller’s window

• enqueue • dequeue

Page 21: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

21CSE 1302C

Queues• We can define the operations for a queue

– enqueue - add an item to the rear of the queue– dequeue (or serve) - remove an item from the front of

the queue– empty - returns true if the queue is empty

• As with our linked list example, by storing generic Object references, any object can be stored in the queue

• Queues often are helpful in simulations or any situation in which items get “backed up” while awaiting processing

Page 22: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

22CSE 1302C

Queues• A queue can be represented by a singly-

linked list; it is most efficient if the references point from the front toward the rear of the queue

• A queue can be represented by an array, using the remainder operator (%) to “wrap around” when the end of the array is reached and space is available at the front of the array

Page 23: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

23CSE 1302C

A Queue can be Implemented using a Linked List

• In a queue implemented as a linked list, we enqueue by inserting at the end of the linked list.

• In a stack implemented as a linked list, we dequeue by deleting the first item in the linked list. Again, deletion is not based on value; it is based on position.

Page 24: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

24CSE 1302C

A Linked List Class Implementing a Queue• Because a queue inserts items at the

end of the list, we will add an instance variable, tail, (a PlayerNode reference), representing the last node in the list. In this way, we do not have to traverse the list every time we insert an item.

• We will need to update that reference every time an item is inserted.

Page 25: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

25CSE 1302C

A Linked List Implementing a Queue• Here is an example of a queue of Player

objects represented by a linked list.

2

Jin

Golf

8

Gino

Diablo

5

Ajay

Sonic

head

null7

Sarah

Mario

tail

Page 26: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

26CSE 1302C

Return value Method name and argument listvoid enqueue( Player p )

inserts Player p at the end of the list Player dequeue( )

returns and removes the first Player of the list. If the list is empty, the method throws a DataStructureException

Player peek( ) returns a copy of the first Player on the list without deleting it. If the list is empty, the method throws a DataStructureException

Page 27: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

27CSE 1302C

Inserting in a (non-empty) Linked List Representing a Queue

• Our original list:2

Jin

Golf

8

Gino

Diablo

5

Ajay

Sonic

head

null7

Sarah

Mario

tail

Page 28: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

28CSE 1302C

Inserting in a (non-empty) Linked List Representing a Queue

• Step 1: Instantiate a new node:PlayerNode pn = new PlayerNode( p );

// here, p is Player( 6, Steve, NFL )2

Jin

Golf

8

Gino

Diablo

5

Ajay

Sonic

head

null7

Sarah

Mario

• 6• Steve• NFL

null

tail

pn

Page 29: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

29CSE 1302C

Inserting in a Linked List Representing a Queue• Step 2: Attach the new node at the end

of the list: tail.setNext( pn );

2

Jin

Golf

8

Gino

Diablo

5

Ajay

Sonic

head

7

Sarah

Mario

6

Steve

NFL

null

tail

Page 30: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

30CSE 1302C

Inserting in a Linked List Representing a Queue• Step 3: Update tail: tail = tail.getNext( );

2

Jin

Golf

8

Gino

Diablo

5

Ajay

Sonic

head

7

Sarah

Mario

6

Steve

NFL

null

tail

Page 31: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

31CSE 1302C

A Linked List Class Implementing a Queue• A queue has a front and a back, represented by

head and tail, respectively.• Could they be inverted, i.e. could head represent

the back of the queue and tail represent the front of the queue?

• This would be highly inefficient because when we delete, we would need to traverse the list in order to update tail.

• Indeed, we cannot go backward in the list from tail in order to access the next-to-last node (which becomes tail after the deletion).

Page 32: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

32CSE 1302C

Array Representation of Queues• We can also represent a queue using an array. • The first (inefficient) idea is to represent the queue

with a standard array; two indexes, front and back, represent the front and back of the queue.

• To enqueue, we could increment back by 1 and store the player at array index back.

• To dequeue, we could return the element at index front and increment front by 1.

• The problem with this approach is that the number of available elements in the array will shrink over time.

Page 33: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

33CSE 1302C

Queue after Inserting First 5 Elements• // Player( 5, Ajay, Sonic ) was enqueued first.

index Player object

7

6

5

back 4 ( 6, Steve, NFL )

3 (8, Gino, Diablo )

2 ( 7, Sarah, Mario )

1 ( 2, Jin, Golf )

front 0 ( 5, Ajay, Sonic )

Page 34: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

34CSE 1302C

Queue after Dequeueing Once• Element at index 0 is no longer usable.

index Player object

7

6

5

back 4 ( 6, Steve, NFL )

3 (8, Gino, Diablo )

2 ( 7, Sarah, Mario )

front 1 ( 2, Jin, Golf )

0 ( 5, Ajay, Sonic )

Page 35: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

35CSE 1302C

Using a Circular Array• The solution is to consider the array as a

circular array.• After back reaches the last array index,

we start enqueueing again at index 0.• If the array has size 8, the index "after" 7

is 0.• The useful capacity of the array never

shrinks. If the array has size 8, the useful capacity of the array is always 8.

Page 36: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

36CSE 1302C

An Empty Queue

Page 37: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

37CSE 1302C

Enqueueing One Element

Page 38: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

38CSE 1302C

Enqueueing a Second Element

Page 39: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

39CSE 1302C

Enqueueing a Third Element

Page 40: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

40CSE 1302C

Enqueueing a Fourth Element

Page 41: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

41CSE 1302C

Dequeueing Once

Page 42: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

42CSE 1302C

Dequeueing Again

Page 43: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

43CSE 1302C

Full Queue• In a queue implemented as a circular

array, how do we know when the queue is full?

• When the queue is full, we have the following relationship between front and back:

( back + 1 – front ) % QUEUE_SIZE == 0

Page 44: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

44CSE 1302C

A Full Queue

Page 45: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

45CSE 1302C

Empty Queue• When the queue is empty, we also have

the same relationship between front and back!

( back + 1 – front ) % QUEUE_SIZE == 0

Page 46: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

46CSE 1302C

An Empty Queue

Page 47: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

47CSE 1302C

Queue Full or Empty?• So when ( back + 1 – front ) % QUEUE_SIZE == 0

• Is the queue full or empty? We cannot tell.

• There is an easy way to solve this problem: Keep track of the number of items in the queue.

Page 48: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

48CSE 1302C

Instance Variables for our Queue• We will have five instance variables:

– A constant representing the capacity of the queue.– An array, storing the elements of the queue.– An int, front, representing the front of the queue.– An int, back, representing the back of the queue.– An int, numberOfItems, storing the number of items in

the queue. public class ArrayQueue { private static final int QUEUE_SIZE = 8; private Player [] queue; private int front; private int back; private int numberOfItems; … }

Page 49: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

49CSE 1302C

Constructor for our Queue• The constructor does four things:

– instantiates the array, queue.– initializes front to 0– initializes back to QUEUE_SIZE – 1– initializes numberOfItems to 0 to reflect that

the queue is empty.

public ArrayQueue( ) { queue = new Player[QUEUE_SIZE]; front = 0; back = QUEUE_SIZE - 1; numberOfItems = 0; }

Page 50: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

50CSE 1302C

Utility Methods for our Queue• We will have three utility methods:

– isEmpty, returning true if the the queue is empty.– isFull, returning true if the the queue is full.– toString, returning a String representation of the queue.

public boolean isEmpty( ) { return ( numberOfItems == 0 ); }

public boolean isFull( ) { return ( numberOfItems == QUEUE_SIZE ); }

Page 51: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

51CSE 1302C

toString Method for our Queuepublic String toString( ) { String queueString = ""; if ( back >= front ) { for ( int i = front; i <= back; i++ ) queueString += queue[i] + "\n"; } else { for ( int i = front; i < QUEUE_SIZE; i++ ) queueString += queue[i] + "\n"; for ( int i = 0; i <= back; i++ ) queueString += queue[i] + "\n"; } return queueString; }

Page 52: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

52CSE 1302C

enqueue Method for our Queue public boolean enqueue( Player p ) { if ( !isFull( ) ) { queue[( back + 1 )% QUEUE_SIZE] = p; back = ( back + 1 ) % QUEUE_SIZE; numberOfItems++; return true; } else return false; }

Page 53: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

53CSE 1302C

dequeue Method for our Queue public Player dequeue throws DataStructureException { if ( !isEmpty( ) ) { front = ( front + 1 ) % QUEUE_SIZE; numberOfItems--; return queue[( QUEUE_SIZE + front – 1 ) % QUEUE_SIZE]; } else throw new DataStructureException ( "Queue empty: cannot dequeue" ); }

Page 54: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

54CSE 1302C

Common ErrorTrap

• Do not confuse array index 0 and QUEUE_SIZE - 1 with front and back. In a queue represented by a circular array, the indexes 0 and QUEUE_SIZE - 1 are irrelevant.

Page 55: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

55CSE 1302C

Implementing a Stack or a Queueas an Array vs as a Linked List

Array Linked List

Easily expandable No Yes

Direct access to every item Yes No

Easy to code Yes No

Page 56: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

56CSE 1302C

Trees• A tree is a non-linear data structure that

consists of a root node and potentially many levels of additional nodes that form a hierarchy

• Nodes that have no children are called leaf nodes

• Nodes except for the root and leaf nodes are called internal nodes

• In a general tree, each node can have many child nodes

Page 57: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

57CSE 1302C

Binary Trees• In a binary tree, each node can have no more than

two child nodes

• A binary tree can be defined recursively. Either it is empty (the base case) or it consists of a root and two subtrees, each of which is a binary tree

• Trees are typically are represented using references as dynamic links, though it is possible to use fixed representations like arrays

• For binary trees, this requires storing only two links per node to the left and right child

Page 58: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

58CSE 1302C

Tree Example - Starcraft

Page 59: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

59CSE 1302C

Dungeon Siege 2

Page 60: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

60CSE 1302C

Diablo 2

Page 61: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

61CSE 1302C

Fallout 3

Page 62: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

62CSE 1302C

Visiting Every Nodevoid PrintTree (Node current)

{ if (current != null)

{ Console.WriteLine(current.data)

PrintTree(current.left);

PrintTree(current.right);

}

} • Of course we could order the 3 statements of work

in any order, visiting the nodes in a different order.• Not to be missed: http://oracleofbacon.org/

Page 63: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

63CSE 1302C

Graphs• A graph is a non-linear structure

• Unlike a tree or binary tree, a graph does not have a root

• Any node in a graph can be connected to any other node by an edge

• Analogy: the highway system connecting cities on a map

Page 64: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

64CSE 1302C

Digraphs• In a directed graph or digraph, each

edge has a specific direction.

• Edges with direction sometimes are called arcs

• Analogy: airline flights between airports

Page 65: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

65CSE 1302C

Representing Graphs• Both graphs and digraphs can be

represented using dynamic links or using arrays.

• As always, the representation should facilitate the intended operations and make them convenient to implement

Page 66: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

66CSE 1302C

Collection Classes• The C# standard library contains several

classes that represent collections, often referred to as the Generic Collection

• Their underlying implementation is implied in the class names such as ArrayList and LinkedList

Page 67: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

67CSE 1302C

Stacks• The System.Collections.Generic;

package contains a Stack class

• Like ArrayList operations, the Stack operations operate on Object references

Page 68: Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010

68CSE 1302C

Questions?