aiti lecture 18 introduction to data structure, stack, and queue adapted from mit course 1.00 spring...
TRANSCRIPT
AITI Lecture 18 Introduction to Data
Structure, Stack, and Queue
Adapted from MIT Course 1.00 Spring 2003
Lecture 23 and Tutorial Note 8
(Teachers: Please do not erase the above note)
Data Structures• Set of primitives used in algorithms, simulations,
operating systems, applications– Store data required by algorithm– Provide only the access that is required– Disallow all other access
• There are 5 or 10 common data structures– We cover the basic version of the core structures– Many variations exist on each structure
• It’s common to make application-specific modifications
• We’ll both build them and use the Java built-in versions!
Stacks
a
d
c
b
Top
Single ended structureLast-in, first-out (LIFO) list
Applications:1. Simulation: robots, machines2. Recursion: pending function calls3. Reversal of data
Queues
a b c d e
Front Rear
Double ended structureFirst-in, first-out (FIFO) list
Applications:1. Simulation: lines2. Ordered requests: device drivers, routers, …3. Searches
Double ended Queues (Dequeues)
d a b c e
Double ended structureApplications:1. Simulation: production, operations
1 3 3 2 3 1 2 1 3 2 2
Track 1Track 2Track 3
Engine
Train
Engine
A dequeue can model both stacks and queues
Priority Queues or Heaps
e
a
b
c
d
Top
Bottom
Highest priority element at top“Partial sort”All enter at bottom, leave at top
Applications:1. Simulations: event list2. Emergency response modeling3. Searching (next most likely)
Heaps Modeled as Binary Tree
c
qd
fe vt
a
Heaps Modeled as Binary Tree
c
qd
fe vt
ae
a
Heaps Modeled as Binary Tree
c
qd
fe vt
ae
a d
a
Heaps Modeled as Binary Tree
c
qd
fe vt
ae
a d
a c
a
Binary Trees
m
pe
fd vn
Level Nodes0 20
1 21
2 22
…
k 2k
Binary tree has 2(k+1)-1nodesA maximum of k steps are required to find (or not find) a node
E.g. 220 nodes, or 2,000,000 nodes, in 20 steps!
Binary Trees
Applications:1. Searching and sorting (general purpose)2. Fast retrieval of data
Find/insert any item quickly (bottom, middle or top)More general than earlier data structures
Trees
m
pe
fd vo
n
a
Each node has variable number of children
Applications:1. Set operations (unions, intersections)2. Matrix operations (basis representation, etc.)3. Graphics and spatial data (quadtrees, R-trees, …)
Graphs
eb
f g
ah
di
c
Applications1. Simulation2. Matrix representation3. General systems representation4. Networks: telecom, transport, hydraulic, electrical, ...
Relationships of Data Structures
Stack
Dequeue
Priority queue
Binary tree
Tree
Graph
Queue
Set
Many variations
Many variations
Three possible implementations1. Dynamic arrays2. Linked lists3. Built-in Java classes
Exercise
• What data structure would you use to model:– People getting on and off the bus – A truss in a CAD program– A conveyor belt– The emergency room at a hospital– The lines at United Airlines at Logan– The local city street network– Books to be reshelved at the library
Exercise
• What data structure would you use to model:– People getting on and off the bus Dequeue– A truss in a CAD program
Dequeue– A conveyor belt Queue– The emergency room at a hospital Heap– The lines at United Airlines at Logan Queues– The local city street network Graph– Books to be reshelved at the library Stack
Stacks
Top
Stack s
-1
0
1
2
3
4 = Size -1
Stacks
‘a’
Top
Stack s
-1
0
1
2
3
4 = Size -1
Push(‘a’)
Top
Stacks
‘a’
‘b’
Top
Stack s
-1
0
1
2
3
4 = Size -1
Push(‘a’)
Top
Push(‘b’)Top
Stacks
‘a’
‘c’
‘b’
Top
Stack s
-1
0
1
2
3
4 = Size -1
Push(‘a’)
Top
Push(‘b’)TopPush(‘c’)
Top
Stacks
‘a’
‘c’
‘b’
Top
Stack s
-1
0
1
2
3
4 = Size -1
Push(‘a’)
Top
Push(‘b’)TopPush(‘c’)
Top
Pop() ‘c’
Stacks
‘a’
‘c’
‘b’
Top
Stack s
-1
0
1
2
3
4 = Size -1
Push(‘a’)
Top
Push(‘b’)TopPush(‘c’)
Top
Pop() ‘c’
Pop() ‘b’
Stack Interface
public interface Stack { public boolean isEmpty(); public void push( Object o ); public Object pop() throws
EmptyStackException; public void clear(); }
Using a Stack to Reverse an Arraypublic class Reverse { public static void main(String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int i; Stack stack = new ArrayStack1(); for ( i = 0; i < array.length; i++ ) stack.push( new Integer( array[ i ] )); i = 0; while ( !stack.isEmpty() ) System.out.println(
((Integer) stack.pop()).intValue()); }}
ArrayStack, 1
import java.util.*;public class ArrayStack implements Stack { static public final int DEFAULT_CAPACITY = 8; private Object[] stack; private int top = -1; private int capacity; public ArrayStack(int cap) { capacity = cap; stack = new Object[capacity]; } public ArrayStack() { this( DEFAULT_CAPACITY ); }
ArrayStack, 2
public boolean isEmpty() { return ( top < 0 ); }
public void clear() { for ( int i = 0; i < top; i++ ) stack[ i ] = null; // for garbage collection top = -1; }
ArrayStack, 3
public void push(Object o) { if (++top == capacity) grow(); stack[top] = o; }
private void grow() { capacity *= 2; Object[] oldStack = stack; stack = new Object[capacity]; System.arraycopy(oldStack, 0, stack, 0, top); }
ArrayStack, 4
public Object pop() throws EmptyStackException { if ( isEmpty() ) throw new EmptyStackException(); else { // your code goes here // remove and return item on top of the stack; // adjust member variable(s); free memory } }
ArrayStack, 4
public Object pop()
throws EmptyStackException { if ( isEmpty() ) throw new EmptyStackException(); else { Object o = stack[top]; stack[top--] = null; return o; } }
What is a Stack• A stack is a data structure that can contain objects• Objects are inserted and removed in the last-in
first-out (LIFO) fashion– Think of a stack as a can of tennis balls - the last
ball inserted in the can is the first one taken out• Methods:
public interface Stack {
public boolean isEmpty(); public void push(Object o); public Object pop() throws EmptyStackException; public void clear();
}
Stack Methods• void push(Object item)
– Pushes an item into the stack– Note that the item can be any object type (String, user-
defined class, etc.)• Object pop()
– Pops the top item of the stack and returns it to the caller – Note that what returned is a generic Object
• How do we convert it into the original data type?• void clear()
– Empty the stack• boolean isEmpty()
– Return true if the stack is empty and false if otherwise
What is a Queue• A queue is another simple type of object
container• Objects are inserted in and removed from the
queue in the first-in first-out (FIFO) fashion– Think of a queue as a waiting line in the toll booth
• Methods
public interface Queue {
public boolean isEmpty(); public void add(Object o); public Object remove() throws
NoSuchElementException; public void clear();
}
Queue Methods• void add(Object item)
– Insert an item into the queue– Note that the item can be any object type (String, user-
defined class, etc.)• Object remove()
– Remove the first item from the queue and return it to the caller
– If the queue is empty, throw NoSuchElementException– Note that what is returned is a generic Object
• How do we convert it into the original data type?• void clear()
– Empty the queue• boolean isEmpty()
– Return true if the queue is empty and false if otherwise