1 / 68

Data Structures Part 2

Data Structures Part 2. Stacks, Queues, Trees, and Graphs. Briana B. Morrison CSE 1302C Spring 2010. Topics. Stacks Queues Trees and Graphs. Classic Data Structures. Now we'll examine some classic data structures Classic linear data structures include queues and stacks

dakota
Download Presentation

Data Structures Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Topics • Stacks • Queues • Trees and Graphs

  3. 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

  4. 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.

  5. push pop Stacks • Stacks often are drawn vertically:

  6. 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

  7. 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.

  8. PlayerStackLinkedList Methods

  9. 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.

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

  11. Stack after Inserting Player (6, Steve, NFL ) • Now there are four items on the stack:

  12. Our Stack After Popping Once • Now there are three items on the stack. Player ( 6, Steve, NFL ) is not on the stack.

  13. 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 inttop, representing the top of the stack. public class ArrayStack { private static final int STACK_SIZE = 100; private Player [] stack; private int top; …. }

  14. 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 }

  15. 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 boolisEmpty( ) { return ( top == -1 ); } public boolisFull( ) { return ( top == ( STACK_SIZE – 1 ) ); }

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

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

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

  19. 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.

  20. dequeue enqueue 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

  21. 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

  22. 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

  23. 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 dequeueby deleting the first item in the linked list. Again, deletion is not based on value; it is based on position.

  24. 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.

  25. A Linked List Implementing a Queue • Here is an example of a queue of Player objects represented by a linked list. 7 Sarah Mario 5 Ajay Sonic 8 Gino Diablo 2 Jin Golf null head tail

  26. Inserting in a (non-empty) Linked List Representing a Queue • Our original list: 7 Sarah Mario 5 Ajay Sonic 8 Gino Diablo 2 Jin Golf null head tail

  27. 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 ) 7 Sarah Mario 8 Gino Diablo 5 Ajay Sonic 2 Jin Golf null head tail 6 Steve NFL null pn

  28. Inserting in a Linked List Representing a Queue • Step 2: Attach the new node at the end of the list: tail.setNext( pn ); 8 Gino Diablo 6 Steve NFL 7 Sarah Mario 5 Ajay Sonic 2 Jin Golf null head tail

  29. Inserting in a Linked List Representing a Queue • Step 3: Update tail: tail = tail.getNext( ); 7 Sarah Mario 5 Ajay Sonic 8 Gino Diablo 6 Steve NFL 2 Jin Golf null head tail

  30. 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).

  31. 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.

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

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

  34. 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.

  35. An Empty Queue

  36. Enqueueing One Element

  37. Enqueueing a Second Element

  38. Enqueueing a Third Element

  39. Enqueueing a Fourth Element

  40. Dequeueing Once

  41. Dequeueing Again

  42. 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

  43. A Full Queue

  44. Empty Queue • When the queue is empty, we also have the same relationship between front and back! ( back + 1 – front ) % QUEUE_SIZE == 0

  45. An Empty Queue

  46. 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.

  47. 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 intnumberOfItems; … }

  48. 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; }

  49. 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 booleanisEmpty( ) { return ( numberOfItems == 0 ); } public booleanisFull( ) { return ( numberOfItems == QUEUE_SIZE ); }

More Related