Problem of the Day Rich old man tells his 2 children he will hold a race to decide who gets his...

Preview:

Citation preview

Problem of the Day

Rich old man tells his 2 children he will hold a race to decide who gets his fortune. Winner is one who owns SLOWEST horse.

1st two weeks, race is aimless mess, never to end Missing their families, kids consult a wise

man Upon leaving the wise man, kids jump

on horses Race at top speed to the city.

What did the wise man say?

Problem of the Day

Rich old man tells his 2 children he will hold a race to decide who gets his fortune. Winner is one who owns SLOWEST horse.

1st two weeks, race is aimless mess, never to end Missing their families, kids consult a wise

man Upon leaving the wise man, kids jump

on horses Race at top speed to the city.

What did the wise man say? He told them to ride the other's horse!

LECTURE 32:SEQUENCE ADT

CSC 212 – Data Structures

All elements available when stored in a List Without removing a thing can access all elements Add element anywhere in the List For existing location, set changes element in-

place Lists implement Iterable interface

iterator() returns Iterator over elements Instance of 2nd class returned by this method

Iterable makes it easy to access elements Use for-each to handle any Iterable instance

List ADT

But Lists still differ in how they are organized To use ArrayList need absolute index (an int)

Positions (references) used by NodeList Ideally have ADT with both sets of

methods Could use both absolute & relative ordering Conversion methods enable jumping back-

and-forth

Identical Access w/Iterable?

But Lists still differ in how they are organized To use ArrayList need absolute index (an int)

Positions (references) used by NodeList Ideally have ADT with both sets of

methods

Identical Access w/Iterable?

But Lists still differ in how they are organized To use ArrayList need absolute index (an int)

Positions (references) used by NodeList Ideally have ADT with both sets of

methods

Identical Access w/Iterable?

But Lists still differ in how they are organized To use ArrayList need absolute index (an int)

Positions (references) used by NodeList Ideally have ADT with both sets of

methods

Identical Access w/Iterable?

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

interface Sequence<E> extends IndexList<E>, PositionList<E>,

Deque<E> {public Position<E> atIndex(int r) throws BoundaryViolationException;

public int indexOf(Position<E> p) throws InvalidPositionException;

}

As ADT, must be implementation independent Specify Position okay, but not it's implementation SEQUENCE class free to use array or linked list

SEQUENCE Interface

Actually used by all of Java’s List classes Combines everything so far into one simple

idea Can serve as basis of STACK, QUEUE, or DEQUE Small, simple databases built from Sequences

Basic building block for many other structures Many use SEQUENCE somewhere in

implementation Need to know how Position & index works Use big-Oh costs to select which implementation

Why Use a Sequence?

Actually used by all of Java’s List classes Combines everything so far into one simple

idea Can serve as basis of STACK, QUEUE, or DEQUE Small, simple databases built from Sequences

Basic building block for many other structures Many use SEQUENCE somewhere in implementation Need to know how Position & index works Use big-Oh costs to select which

implementation

Why Use a Sequence?

Easier & faster if nodes doubly-linked Position-based methods come from

NODELIST To find indices, need to traverse Nodes

Could use Nodes via getNext() & getPrev()

next() & prev() if want Position traversal

Linked list-based Sequence

Nodes

List

Implementing atIndex(i)

Nodes

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Suppose call was:atIndex(3)

i 3trav

Implementing atIndex(i)

Nodes

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Suppose call was:atIndex(3)

i 3trav

Implementing atIndex(i)

Nodes

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Nodes

Suppose call was:atIndex(3)

i 3 2trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Nodes

Suppose call was:atIndex(3)

i 3 2 1trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Nodes

Suppose call was:atIndex(3)

i 3 2 1 0trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Position

Suppose call was:atIndex(3)

i 3 2 1 0trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Nodes

Suppose call was:atIndex(3)

i 3 2 1 0trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfitrav first()while (i > 0) do

trav next(trav) i--

endwhilereturn trav

Array-based Sequence

Based on IndexList Use and resize

array Easy to implement Store elements in

arrayS

0 1 2 3 N-1

List

Array-based Sequence

Based on IndexList Use and resize

array Easy to implement Store elements in

array

What about Positions? NodeList's

methods needs to use them

atIndex() returns it

We do not have them!

S0 1 2 3 N-1

List

Array-based Sequence

Array of Positions Can be used in

methods

Position contains: Element Index in array

No next or prev links Instead use the

index Update index when Position shifted

0 1 2 3

S0 1 2 3 N-1

List

Position

Array-based Sequence

Algorithm next(pos)if (pos == S[size()-1]) then throw BoundaryViolation…else idx pos.getIndex() return S[idx+1]fi

end0 1 2 3

S0 1 2 3 N-1

List

Position

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

0 1 2

S0 1 2 3 N-1

List

Position

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

0 1 3

S0 1 2 3 N-1

List

Position

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

0 2 3

S0 1 2 3 N-1

List

Position

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

1 2 3

S0 1 2 3 N-1

List

Position

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

1 2 3

S0 1 2 3 N-1

List

Position

0

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */for i size() downto 1 do S[i] S[i-1] S[i].setIndex(i)endforS[0] new Node(0, e)size++

end

1 2 3

S0 1 2 3 N-1

List

Position

0

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */for i size() downto 1 do S[i] S[i-1]

S[i].setIndex(i)endforS[0] new Node(0, e)size++

end

1 2 3

S0 1 2 3 N-1

List

Position

0

Your Turn

Get into your groups and complete activity

For Next Lecture

Review GT Chapter 6 for Wednesday’s Quiz Do your best to understand what all this is

about How do we use these in real-world problems What are the big-Oh complexities of these

methods?

As usual, week #11 assignment due tomorrow

Programming Assignment #2 due in 5 days If you have not started do not delay, will

take time

Recommended