cse 373 data structures & algorithms€¦ · cse 373 data structures & algorithms lecture...

39
CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Upload: others

Post on 10-Jul-2020

49 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

CSE 373

Data Structures & AlgorithmsData Structures & Algorithms

Lecture 02

Lists, Stacks, and Queues

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Page 2: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Announcements

Guest Lecturers next week:

• Monday: Vibhor Rastogi

• Wednesday: Sean Shih-Yen Liu

(I will be giving a keynote lecture at a conference

in Brazil, about my research on probabilistic

databases)

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 2

Page 3: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Today’s Outline

• Lists

• Stacks and queues

• Begin trees

Remember:

• Reading assignment for Monday: Ch. 1- 3

• For today: 3.1, 3.2, 3.3 (did you read them ?)

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 3

Page 4: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

The List ADT

• List of items, e.g. 7, 3, 9, 5

• Insert, delete, find, replace, …

• Implementations: linked list or array list

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 4

7 3 9 5

7 3 9 5

Page 5: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Linked List Implementation

private static class Node<AnyType>{

First, need to define a single node:

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 5

public Node( AnyType d, Node<AnyType> n ){

data = d; next = n;}

public AnyType data;public Node<AnyType> next;

}

Page 6: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Simple Linked v.s. Double Linked

7 3 9 5

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 6

7 3 9 5

What are the tradeoffs ?

Page 7: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Double Linked List Implementation

private static class Node<AnyType>{

public Node( AnyType d, Node<AnyType> p , Node<AnyType> n ){

data = d; prev = p; next = n;

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 7

data = d; prev = p; next = n;}

public AnyType data;public Node<AnyType> prev; /* now we can go back */public Node<AnyType> next;

}

Page 8: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Linked List Implementation

public class MyLinkedList<AnyType> implements Itera ble<AnyType>{

/* all methods go here (see book, code examples) */

private int theSize ;

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 8

private int theSize ;private Node<AnyType> beginMarker;private Node<AnyType> endMarker;

}

7 3 9 5

beginMarker

endMarker

Page 9: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Operations on the Linked List

7 3 9 5

beginMarker

endMarker

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 9

Discuss in class the following operations:1. Find(9); Find(2);2. Insert(11); at the beginning, at the end, in the middle;3. Remove(9);

Page 10: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Array Implementation

7 3 9 5

theSize

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 10

Discuss in class the following operations:1. Find(9); Find(2);2. Insert(11); at the beginning, at the end, in the middle;3. Remove(9);

Page 11: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Review: Big O Notation

DEFINITION: The Big-O notation

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 11

T(n) = O(f(n)) if there exist constantsc and n’ such that: T(n) ≤ c f(n) for all n ≥ n’

Page 12: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Summary: Some Complexities

Linked list Array

AnyType get( int idx) ? ?

int find(AnyTypex) ? ?

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 12

int find(AnyTypex) ? ?

void add(x,idx)/* before/after) */

? ?

void remove(int idx) ? ?

InsertAnywhere(x) ? ?

Page 13: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Summary: Some Complexities

Linked list Array

AnyType get( int idx)O(k)

where k=idxO(1)

int find(AnyTypex) O(n) O(n)

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 13

int find(AnyTypex) O(n) O(n)

void add(x,idx)/* before/after) */

O(1) O(n)

void remove(int idx) O(1) O(n)

InsertAnywhere(x) O(1) O(1)

Page 14: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Iterators

• Review questions from Weiss, Chapt. 3.3

• What is an Iterator ?

• What is an Iterable ?

• What is a Collection ?

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 14

Page 15: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Answers…

public interface Iterator<AnyType>{ boolean hasNext( );

AnyType next( );void remove( );

}

An Iterable provides a method named iterator

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 15

An Iterable provides a method named iteratorthat returns and object of typeIterator . It can be usedin a for loop (see book…).

> public interface Collection<AnyType> extends Iterable<AnyType>

{ . . .boolean add( AnyType x);boolean remove( AnyType x );

. . .}

Page 16: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Application: Polynomial ADT

Attempt 1:

• linked list implementation:

• Ai is the coefficient of the xi-1 term:

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 16

5 + 2x + 3x2 ( 5 2 3 )

7 + 8x ( 7 8 )

3 + x2 ( 3 0 1 )

Problem?

Page 17: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

4 + 3x2001

( 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 )

Page 18: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Sparse Vector Data Structure:

4 + 3x2001

4 3

(<4 0> <3 2001>)

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 18

40

32001

Page 19: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Addition of Two Polynomials?

Complexity?

1050

31200

15+10x50+3x1200

150

p

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 19

50 12000

3050

4100

5+30x50+4x100

50

q

Page 20: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Addition of Two Polynomials

One pass down each list: O(m+n)

1050

31200

15+10x50+3x1200

150

p

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 20

50 12000

3050

4100

5+30x50+4x100

50

q

4100

31200

4050

200

r

Page 21: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Sparse Matrices

(< row (<column data> <column data> …)>

18 0 33 0 0 00 0 0 0 0 00 0 0 0 0 00 0 0 99 0 00 0 0 0 0 00 0 0 0 0 27

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 21

(< row (<column data> <column data> …)><row (<column data> <column data> …)>… )

( <1 (<1 18> <3 33>)>, <4 (<4 99>)> <6 (<6 27>) )

( <1, 1, 18> <1, 3, 33> <4, 4, 99> <6, 6, 27>)

(<row column data> <row column data> …)

Page 22: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

First Example: Stack ADT

• LIFO: Last In First Out

• Stack operations

– create

– destroy

– push

A E D C B A– push

– pop

– top

– is_empty

BCDEF F

10/02/2009 22CSE 373 Fall 2009 -- Dan Suciu

Page 23: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Stacks in Practice

• Function call stack

• Removing recursion

• Balancing symbols (parentheses)

• Evaluating Postfix Notation• Evaluating Postfix Notation

Infix: 7 + 8 * 4 – 30

Postfix: 7 8 4 * + 30 -

10/02/2009 23CSE 373 Fall 2009 -- Dan Suciu

Page 24: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30 #

10/02/2009 24CSE 373 Fall 2009 -- Dan Suciu

7

Page 25: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30 #

Stack:Stack:

Output:

10/02/2009 25CSE 373 Fall 2009 -- Dan Suciu

+

7

Page 26: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30 #

Stack:Stack:

Output:

10/02/2009 26CSE 373 Fall 2009 -- Dan Suciu

+

7 8

Page 27: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30 #

Stack:

*Stack:

Output:

10/02/2009 27CSE 373 Fall 2009 -- Dan Suciu

*

+

7 8

Page 28: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30 #

Stack:

*Stack:

Output:

10/02/2009 28CSE 373 Fall 2009 -- Dan Suciu

*

+

7 8 4

Page 29: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30

Stack:Stack:

Output:

10/02/2009 29CSE 373 Fall 2009 -- Dan Suciu

+

7 8 4 *

Page 30: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30 #

(empty stack)Stack:Stack:

Output:

10/02/2009 30CSE 373 Fall 2009 -- Dan Suciu

7 8 4 * +

Page 31: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30 #

Stack:Stack:

Output:

10/02/2009 31CSE 373 Fall 2009 -- Dan Suciu

-

7 8 4 * +

Page 32: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30 #

Stack:Stack:

Output:

10/02/2009 32CSE 373 Fall 2009 -- Dan Suciu

-

7 8 4 * + 30

Page 33: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Infix to Postfix with a Stack

7 + 8 * 4 – 30 #

Stack:Stack:

Output:

10/02/2009 33CSE 373 Fall 2009 -- Dan Suciu

7 8 4 * + 30 -

Page 34: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Array vs. Linked List

• Too much space, or not

enough space

• Not as complex

• Could make array more

• Can grow as needed

• More memory per item

in the queue• Could make array more

robust

• Kth element accessed

“easily”

• Insert requires shift

• Linked list code

more complex

• Kth element access

requires linear scan??

10/02/2009 34CSE 373 Fall 2009 -- Dan Suciu

Page 35: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

• FIFO: First In First Out

• Queue operations

create

Second Example: Queue ADT

We briefly mentioned

it in Lecture 01…

destroy

enqueue

dequeue

is_empty

F E D C Benqueue dequeueG A

10/02/2009 35CSE 373 Fall 2009 -- Dan Suciu

http://courses.cs.vt.edu/csonline/DataStructures/Lessons/QueuesImplementationView/applet.html

Page 36: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Circular Array Queue Data Structure

enqueue(Object x) {

b c d e f

Q0 size - 1

front backHow test for empty list?enqueue(Object x) {

Q[back] = x ;

back = (back + 1) % size

}

dequeue() {

x = Q[front] ;

front = (front + 1) % size;

return x ;

}

How test for empty list?How to find K-th element in the queue?What is complexity of these operations?Limitations of this structure?

10/02/2009 36CSE 373 Fall 2009 -- Dan Suciu

Page 37: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Linked List Queue Data Structure

b c d e f

front backvoid enqueue(Object x) {

if (is_empty())

Object dequeue() {

assert(!is_empty)if (is_empty())

front = back = new Node(x)

else

back.next = new Node(x)

back = back.next

}

bool is_empty() {

return front == null

}

assert(!is_empty)

return_data = front.data

temp = front

front = front.next

delete temp

return return_data

}

10/02/2009 37CSE 373 Fall 2009 -- Dan Suciu

Page 38: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Circular Array vs. Linked List

• Too much space, or not

enough space

• Not as complex

• Could make array more

• Can grow as needed

• More memory per item

in the queue• Could make array more

robust • Linked list code

more complex

10/02/2009 38CSE 373 Fall 2009 -- Dan Suciu

Page 39: CSE 373 Data Structures & Algorithms€¦ · CSE 373 Data Structures & Algorithms Lecture 02 Lists, Stacks, and Queues 10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 1

Applications of Queues

• Tree traversals, graph traversals

– Will see in coming lectures

• Wherever fairness is needed:

– Printer queues– Printer queues

– Packets in a network

– http requests at a web server

– Etc.

10/02/2009 CSE 373 Fall 2009 -- Dan Suciu 39