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

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

Embed Size (px)

Citation preview

Page 1: 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

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)

Page 2: 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

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!

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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)

Page 7: 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

Heaps Modeled as Binary Tree

c

qd

fe vt

a

Page 8: 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

Heaps Modeled as Binary Tree

c

qd

fe vt

ae

a

Page 9: 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

Heaps Modeled as Binary Tree

c

qd

fe vt

ae

a d

a

Page 10: 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

Heaps Modeled as Binary Tree

c

qd

fe vt

ae

a d

a c

a

Page 11: 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

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!

Page 12: 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

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

Page 13: 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

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, …)

Page 14: 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

Graphs

eb

f g

ah

di

c

Applications1. Simulation2. Matrix representation3. General systems representation4. Networks: telecom, transport, hydraulic, electrical, ...

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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

Page 18: 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

Stacks

Top

Stack s

-1

0

1

2

3

4 = Size -1

Page 19: 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

Stacks

‘a’

Top

Stack s

-1

0

1

2

3

4 = Size -1

Push(‘a’)

Top

Page 20: 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

Stacks

‘a’

‘b’

Top

Stack s

-1

0

1

2

3

4 = Size -1

Push(‘a’)

Top

Push(‘b’)Top

Page 21: 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

Stacks

‘a’

‘c’

‘b’

Top

Stack s

-1

0

1

2

3

4 = Size -1

Push(‘a’)

Top

Push(‘b’)TopPush(‘c’)

Top

Page 22: 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

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’

Page 23: 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

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’

Page 24: 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

Stack Interface

public interface Stack {     public boolean isEmpty();     public void push( Object o );     public Object pop() throws

EmptyStackException;     public void clear(); }

Page 25: 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

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()); }}

Page 26: 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

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 ); }

Page 27: 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

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

Page 28: 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

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); }

Page 29: 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

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

Page 30: 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

ArrayStack, 4

public Object pop()

throws EmptyStackException { if ( isEmpty() ) throw new EmptyStackException(); else { Object o = stack[top]; stack[top--] = null; return o; } }

Page 31: 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

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();

}

Page 32: 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

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

Page 33: 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

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();

}

Page 34: 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

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