Transcript Sequences

Sequences and
Iterators
© 2004 Goodrich, Tamassia
Sequences and Iterators
1
The Vector ADT (a.k.a. ArrayList)
The Vector ADT extends
the notion of array by
storing a sequence of
arbitrary objects
An element can be
accessed, inserted or
removed by specifying its
rank (number of elements
preceding it)
An exception is thrown if
an invalid rank is specified
(e.g., a negative rank)
No exception for “full”!
© 2004 Goodrich, Tamassia
Main vector operations:
 object elemAtRank(integer r):
returns the element at rank r
without removing it
 object replaceAtRank(integer r,
object o): replace the element at
rank with o and return the old
element
 insertAtRank(integer r, object o):
insert a new element o to have
rank r
 object removeAtRank(integer r):
removes and returns the element
at rank r
Additional operations size() and
isEmpty()
Sequences and Iterators
2
Array-based Vector
Use an array V of size N
A variable n keeps track of the size of the vector
(number of elements stored)
Operation elemAtRank(r) is implemented in O(1)
time by returning V[r]
V
0 1 2
© 2004 Goodrich, Tamassia
r
n
Sequences and Iterators
3
Insertion
In operation insertAtRank(r, o), we need to make
room for the new element by shifting forward the
n - r elements V[r], …, V[n - 1]
In the worst case (r = 0), this takes O(n) time
V
0 1 2
r
n
0 1 2
r
n
0 1 2
o
r
V
V
© 2004 Goodrich, Tamassia
Sequences and Iterators
n
4
Deletion
In operation removeAtRank(r), we need to fill the
hole left by the removed element by shifting
backward the n - r - 1 elements V[r + 1], …, V[n - 1]
In the worst case (r = 0), this takes O(n) time
V
0 1 2
o
r
n
0 1 2
r
n
0 1 2
r
V
V
© 2004 Goodrich, Tamassia
Sequences and Iterators
n
5
Performance
In the array based implementation of a Vector



The space used by the data structure is O(n)
size, isEmpty, elemAtRank and replaceAtRank run in
O(1) time
insertAtRank and removeAtRank run in O(n) time
If we use the array in a circular fashion,
insertAtRank(0) and removeAtRank(0) run in
O(1) time
In an insertAtRank operation, when the array
is full, instead of throwing an exception, we
can replace the array with a larger one
© 2004 Goodrich, Tamassia
Sequences and Iterators
6
Growable Array-based Vector
In a push operation, when Algorithm push(o)
the array is full, instead of
if t = S.length - 1 then
throwing an exception, we
A  new array of
can replace the array with
size …
a larger one
for i  0 to t do
A[i]  S[i]
How large should the new
SA
array be?


incremental strategy:
increase the size by a
constant c
doubling strategy: double
the size
© 2004 Goodrich, Tamassia
Sequences and Iterators
tt+1
S[t]  o
7
Sequence ADT
The Sequence ADT is the
union of the Vector ADT
and the List ADT
Elements accessed by


List-based methods:

Rank, or
Position
Generic methods:

size(), isEmpty()
Vector-based methods:

elemAtRank(r),
replaceAtRank(r, o),
insertAtRank(r, o),
removeAtRank(r)
© 2004 Goodrich, Tamassia
first(), last(), prev(p),
next(p), replace(p, o),
insertBefore(p, o),
insertAfter(p, o),
insertFirst(o),
insertLast(o),
remove(p)
Bridge methods:

Sequences and Iterators
atRank(r), rankOf(p)
8
Applications of Sequences
The Sequence ADT is a basic, generalpurpose, data structure for storing an ordered
collection of elements
Direct applications:


Generic replacement for stack, queue, vector, or
list
simple database (e.g., address book)
Indirect applications:

Building block of more complex data structures
© 2004 Goodrich, Tamassia
Sequences and Iterators
9
Linked List Implementation
A doubly linked list provides a
reasonable implementation of the
Sequence ADT
Nodes implement Position and store:



element
link to the previous node
link to the next node
Special trailer and header nodes
Position-based methods
run in constant time
Rank-based methods
require searching from
header or trailer while
keeping track of ranks;
hence, run in linear time
Adapter design pattern
nodes/positions
header
trailer
elements
© 2004 Goodrich, Tamassia
Sequences and Iterators
10
Array-based Implementation
elements
We use a
circular array
storing
positions
A position
object stores:


Element
Rank
Indices f and l
keep track of
first and last
positions
0
1
3
positions
S
f
© 2004 Goodrich, Tamassia
2
Sequences and Iterators
l
11
Sequence Implementations
Operation
size, isEmpty
atRank, rankOf, elemAtRank
first, last, prev, next
replace
replaceAtRank
insertAtRank, removeAtRank
insertFirst, insertLast
insertAfter, insertBefore
remove
© 2004 Goodrich, Tamassia
Sequences and Iterators
Array
1
1
1
1
1
n
1
n
n
List
1
n
1
1
n
n
1
1
1
12
Iterators
An iterator abstracts the
process of scanning through
a collection of elements
Methods of the ObjectIterator
ADT:




object object()
boolean hasNext()
object nextObject()
reset()
Extends the concept of
Position by adding a traversal
capability
Implementation with an array
or singly linked list
© 2004 Goodrich, Tamassia
An iterator is typically
associated with another data
structure
We can augment the Stack,
Queue, Vector, List and
Sequence ADTs with method:

ObjectIterator elements()
Two notions of iterator:


Sequences and Iterators
snapshot: freezes the
contents of the data
structure at a given time
dynamic: follows changes to
the data structure
13