Title: Sequences
1Sequences
2(No Transcript)
3The Sequence Abstract Data Type
4(No Transcript)
5Implementing a Sequence
6Implementation of a sequence with a doubly linked
list
Position rank
Sequence ADT
DNode atRank(r) rankOf(p)
Doubly linked list
7Implementing a Sequence with a Doubly Linked List
8Class NodeSequence
9The methods to be implemented in class
NodeSequence Methods defined in Vector void
insertAtRank(int rank, Object element) Object
removeAtRank(int rank) Object replaceAtRank(int
rank, Object element) Object elemAtRank (int
r) Auxiliary method void checkRank(int rank,
int range) Bridging methods Position
atRank(int rank) rankOf(Position p)
10(No Transcript)
11 12(No Transcript)
13(No Transcript)
14(No Transcript)
15(No Transcript)
16/ Gets an element at the given rank./ public
Object elemAtRank(int r) return
atRank(r).element() / Returns the rank of
a given position./ public int rankOf(Position
p) DNode node node
header.getNext() for (int i1 i lt size()
i) if (p node) return i else node
node.getNext()
17/ Implementation of a sequence by means of a
doubly linked list. / public class NodeSequence
extends NodeList implements Sequence /
Checks whether the given rank is in the range 0,
n - 1 / protected void checkRank(int r, int
n) throws BoundaryViolationException
if (r lt 0 r gt n) throw
new BoundaryViolationException("Illegal rank "
r)
18 / Returns the position containing the element
at the given rank O(n) time. / public
Position atRank (int rank) DNode node
checkRank(rank, size()) if (rank lt
size()/2) // scan forward from the head
node header.getNext() for (int i0 i lt
rank i) node node.getNext() else
// scan backward from the tail node
trailer.getPrev() for (int i1 i lt
size()-rank i) node node.getPrev()
return node
19 / Gets an element at the given rank./
public Object elemAtRank(int r) return
atRank(r).element() / Returns the rank
of a given position./ public int
rankOf(Position p) DNode node node
header.getNext() for (int i1 i lt size()
i) if (p node) return i else node
node.getNext()
20 / Inserts an element at the given rank O(n)
time. / public void insertAtRank (int rank,
Object element) throws BoundaryViolationExcept
ion checkRank(rank, size() 1) if
(rank size()) insertLast(element)
else insertBefore(atRank(rank),
element)
21 / Removes the element stored at the given
rank O(n) time. / public Object removeAtRank
(int rank) throws BoundaryViolationException
checkRank(rank, size()) return
remove(atRank(rank)) public Object
replaceAtRank(int rank, object element)
throws BoundadryViolationException
checkRank(rank) return replaceElement(atRank(
rank), element)
22Implementing a Sequence with an Array
23(No Transcript)
24(No Transcript)
25(No Transcript)
26(No Transcript)
27Comparing Sequence Implementations
28Interface Hierarchy for Vectors, Lists, and
Sequences
Vector (interface)
List (interface)
extends
impl.
impl.
extends
Sequence (interface)
ArrayVector (class)
NodeList (class)
impl.
impl.
extends
extends
ArraySequence (class)
NodeSequence (class)
Supplement with the methods in Vector interface
Supplement with the methods in List interface
29Data Structure Exercises 9.1
30Iterators
31(No Transcript)
32The Iterator Abstract Data Type
33Iterators in Java
(in package java.util)
34An implementation of the Iterator is always
related to container, i.e., a vector, a list, or
a sequence. The following is an exemplary
implementation of the List Iterator.
public class PositionIterator implements Iterator
protected List list // the underlying list
protected Position cur // the current (next)
position public PositionIterator() //
default constructor public PositionIterator(List
L) // preferred constructor list L
if (list.isEmpty()) cur null // list is
empty else cur list.first() // start with
the first position
35 public boolean hasNext() return (cur ! null)
public Object next() throws NoSuchElementExcept
ion if (!hasNext()) throw new
NoSuchElementException("No next position")
Position toReturn cur if (cur
list.last()) cur null // no positions left
else try cur list.after(cur) catc
h (InvalidPositionException e) // move
cursor to the next position return toReturn
36 class NoSuchElementException extends Exception
public NoSuchElementException() super()
public NoSuchElementException(String s)
super(s)
37In a similar way, we can establish an
ElementIterator as follows.
public class ElementIterator implements Iterator
protected List list // the underlying list
protected Position cur // the current (next)
position protected Object elementCur // the
current (next) element public ElementIterator()
// default constructor public
ElementIterator(List L) // preferred
constructor list L if (list.isEmpty())
cur null // list is empty else cur
list.first() // start with the first position
38public boolean hasNext() return (cur ! null)
public Object next() throws
NoSuchElementException if
(!hasNext()) throw new NoSuchElementException("N
o next position") elementCur
cur.element() if (cur list.last()) cur
null // no positions left else try
cur list.after(cur) catch
(InvalidPositionException e) // move
cursor to the next position return
elementCur
39(No Transcript)
40(No Transcript)
41Data Structure Exercises 10.1
42Case Study Bubble-Sort on a Sequence
43The Bubble-Sort Algorithm
44i
2, 3, 5, 6, 7, 9
r
for a certain i
45(No Transcript)
46(No Transcript)
47(No Transcript)
48(No Transcript)
49Bubble Sort Using Ranks
50If the sequence is array-based, the running time
is proportional to the square of n, the size of
the sequence running time O(n2). On the
other hand, if the sequence is position-based,
the running time is proportional to n3. running
time O(n3). In this case, the atRank operation
needs to perform link hopping.
51Bubble Sort Using Positions
52The running time is proportional to the square of
n, the size of the sequence, regardless of how
the sequence is implemented running time O(n2).
53Data Structure Exercises 9.2