Lecture 7 notes

Download Report

Transcript Lecture 7 notes

The Ranked Sequence ADT
Definition A ranked sequence is a collection of items arranged in a linear
order, where each item has a rank defining the relative ordering of that item in
the sequence.
Note: Stacks, queues and dequeues are restricted type of sequences which
provide an access to specific items only.
Operations (methods) on ranked sequences:
returnItem (rank)
replaceItem (rank, newItem)
insertItem (rank, newItem)
deleteItem (rank)
size ()
empty ()
traverse ()
Returns the item at the specified rank
Replaces the item at the specified rank with
newItem
Inserts newItem at the specified rank
Deletes the item at the specified rank
Returns the size of the sequence
Returns true is the sequence is empty
Processes each node in the sequence in
a specified way
An array-based implementation of a ranked sequence
Consider array S[i] with n objects, where i is the reference to the object
with rank i.
Algorithm returnItem (rank): return S[rank]
O(1) efficiency
Algorithm replaceItem (rank, newItem):
O(1) efficiency
S[rank] := newItem
Algorithm insertItem (rank, newItem):
O(n) efficiency
for i = n-1 to rank do
S[i+1] := S[i]
S[rank] := newItem
n++
Algorithm deleteItem (rank):
O(n) efficiency
item := S[rank]
for i = rank to n-1 do
S[i] := S[i+1]
n-return item
size() and empty() methods are O(1), and traverse() is O(n) method.
A doubly linked list implementation of a ranked sequence: the
insertItem method
Consider the following support methods: checkRank(rank), which checks if rank
is within the legal bounds and if not throws an out of bounds exception, and
nodeAtRank(rank), which returns the node at the specified rank.
private void checkRank (int rank) throws BoundaryViolationException {
if (rank < 0 || rank > size() - 1)
throw new Boundary ViolationException (“Invalid rank.”); }
private DLNode nodeAtRank (int rank) {
DLNode node;
if (rank <= size()/2) {
node = header.getNext();
for (int i=0; i<rank; i++)
node = node.getNext(); }
else {
node = trailer.getPrev();
for (int i=0; i < size()-rank-1; i++)
node = node.getPrev(); }
return node; }
A doubly linked list implementation of a ranked sequence: the
insertItem method (cont.)
public void insertItem (int rank, Object item) throws
BoundaryViolationException {
if (rank != size())
checkRank (rank);
DLNode next = nodeAtRank (rank);
DLNode prev = next.getPrev();
DLNode node = new DLNode (item, next, prev);
next.setPrev(node);
prev.setNext(node);
size++;
}
The efficiency of this method is O(n/2), because of the nodeAtRank method,
which in the worst case traverses the half of the sequence.
A doubly linked list implementation of a ranked sequence: the
deleteItem method
public Object deleteItem (int rank) throws Boundary ViolationException {
checkRank (rank);
DLNode node = nodeAtRank(rank);
DLNode next = node.getNext();
DLNode prev = node.getPrev();
prev.setNext(next);
next.setPrev(prev);
size - -;
return node.getData();
}
The efficiency of this method is O(n/2), because of the nodeAtRank method,
which in the worst case traverses the half of the sequence.
The Positional Sequence ADT
Definition A positional sequence is a collection of items arranged in a linear
order, where each item has a position defined in terms of the neighboring nodes.
Positions (contrary to ranks) are defined relatively to each other, and are not
tied to items.
Operations (methods) on positional sequences:
replace (position, item)
swap (position1, position2)
first ()
last ()
before (position)
after (position)
insertFirst (item)
insertLast (item)
insertBefore (position, item)
insertAfter (position, item)
delete (position)
deleteFirst ()
deleteLast ()
size ()
empty ()
traverse ()
Replaces data at position with item
Swaps items in positions 1 and 2
Returns the position of the first item in the sequence
Returns the position of the last item in the sequence
Returns the position of the item preceding the one in position
Returns the position of the item following the one in position
Inserts item at the beginning of the sequence
Inserts item at the end of the sequence
Inserts item before the one in position
Inserts item after the one in position
Deletes the item in position
Deletes the first item in the sequence
Deletes the last item in the sequence
Returns the size of the sequence
Returns true is the sequence is empty
Processes each node in the sequence in a specified way
Defining positions is the Positional Sequence ADT
To implement operations on PS ADT, we must define the notion of a position.
Think of a sequence as a container with spots arranged in a linear order, where
elements are to be placed.
...
position(i-1)
position(i)
position(i+1)
...
where position(i) is always after position(i-1) and before position(i+1)
Note that a position of an item does not change if a rank of this item changes as
a result of adding or removing a previous item in the sequence. Also, a position
will not change if an item currently in that position is removed from the
sequence.
Example: Consider an initially empty sequence to which the
following set of operations is applied:
Operation: insertFirst(8)
Current list:
data 8; position 1
Operation: insertAfter(1, 5)
Current list:
data 8; position 1
data 5; position 2
Operation: insertBefore(2, 3)
Current list:
data 8; position 1
data 3; position 3
data 5; position 2
Operation: insertFirst(9)
Current list:
data 9; position 4
data 8; position 1
data 3; position 3
data 5; position 2
Operation: before(3)
Position of the item before the one in position 3: 1
Operation: last()
Position of the last item in the sequence: 2
Operation: remove(4)
Current list:
data 8; position 1
data 3; position 3
data 5; position 2
Operation: swap(1, 2)
Current list:
data 5; position 1
data 3; position 3
data 8; position 2
Operation: replace(3, 7)
Current list:
data 5; position 1
data 7; position 3
data 8; position 2
Operation: insertAfter(first(), 2)
Current list:
data 5; position 1
data 2; position 5
data 7; position 3
data 8; position 2
The Positional Sequence ADT interface
public interface PSDLL {
public int replace (int place, int item) throws InvalidPositionException;
public void insertBefore (int place, int item) throws InvalidPositionException;
public void insertAfter (int place, int item) throws InvalidPositionException;
public void swap (int place1, int place2) throws InvalidPositionException;
public int delete (int place) throws InvalidPositionException;
public int before (int place) throws InvalidPositionException;
public int after (int place) throws InvalidPositionException;
public void insertFirst (int item);
public void insertLast (int item);
public PSDLNode deleteFirst ();
public PSDLNode deleteLast ();
public int size();
public int last ();
public int first ();
public boolean empty ();
public void traverse ();
}
Positional Sequence nodes
class PSDLNode {
public void setPrev (PSDLNode newPrev) {
prev = newPrev; }
private int data;
private PSDLNode next, prev;
int pos;
public PSDLNode () {
public void setPosition (int newPos) {
pos = newPos; }
}
public int getData () {
return data; }
public PSDLNode (int d) {
data = d; }
public PSDLNode getNext () {
return next; }
public PSDLNode (int newData, PSDLNode
newNext, PSDLNode newPrev, int newPos) {
data = newData;
next = newNext;
prev = newPrev;
pos = newPos; }
public PSDLNode getPrev () {
return prev; }
public int getPosition () {
return pos; }
public void setData (int newData) {
data = newData; }
public void displayPSDLNode () {
System.out.print ("data " + data + "; position "
+ pos);
System.out.println ();
}
public void setNext (PSDLNode newNext) {
next = newNext; }
}
Positional Sequence ADT -- doubly linked list implementation
class PSDLLADT implements PSDLL {
private PSDLNode header;
private PSDLNode trailer;
private int size;
int position;
/* first and last are dummy nodes that do not store any data */
public PSDLLADT () {
header = new PSDLNode();
trailer = new PSDLNode();
header.setNext(trailer);
header.setPrev(null);
header.setData(0);
header.setPosition(0);
trailer.setPrev(header);
trailer.setNext(null);
trailer.setData(0);
trailer.setPosition(0);
size = 0;
}
public boolean empty () {
return (size == 0); }
public int size () {
return size; }
public int before (int place) throws InvalidPositionException {
return (searchNode(place)).getPrev().getPosition(); }
public int after (int place) throws InvalidPositionException {
return (searchNode(place)).getNext().getPosition(); }
public void insertFirst (int newData) {
position++;
PSDLNode oldFirst = header.getNext();
PSDLNode newFirst = new PSDLNode (newData, oldFirst, header, position);
oldFirst.setPrev(newFirst);
header.setNext(newFirst);
size++; }
public void insertLast (int newData) {
position++;
PSDLNode oldLast = trailer.getPrev();
PSDLNode newLast = new PSDLNode (newData, trailer, oldLast, position);
oldLast.setNext(newLast);
trailer.setPrev(newLast);
size++; }
public PSDLNode deleteFirst () {
PSDLNode oldFirst = header.getNext();
PSDLNode newFirst = oldFirst.getNext();
newFirst.setPrev(header);
header.setNext(newFirst);
size--;
return oldFirst; }
public PSDLNode deleteLast () {
PSDLNode oldLast = trailer.getPrev();
PSDLNode newLast = oldLast.getPrev();
trailer.setPrev(newLast);
newLast.setNext(trailer);
size--;
return oldLast; }
private PSDLNode searchNode (int position)
throws InvalidPositionException {
PSDLNode current = header.getNext();
while (current != trailer) {
if (current.getPosition () == position)
return current;
else
current = current.getNext();
}
throw new InvalidPositionException ("An invalid position was passed to the searchNode method."); }
public int delete (int place) throws
InvalidPositionException {
PSDLNode temp = searchNode (place);
int oldItem = temp.getData();
PSDLNode oldNext = temp.getNext();
PSDLNode oldPrev = temp.getPrev();
oldNext.setPrev(oldPrev);
oldPrev.setNext(oldNext);
size--;
return oldItem; }
public int replace (int place, int newData) throws InvalidPositionException {
PSDLNode temp = searchNode (place);
int oldItem = temp.getData();
temp.setData(newData);
return oldItem; }
public void swap (int place1, int place2) throws InvalidPositionException {
PSDLNode node1 = searchNode(place1);
PSDLNode node2 = searchNode(place2);
PSDLNode temp = new PSDLNode();
temp.setData(node1.getData());
node1.setData(node2.getData());
node2.setData(temp.getData()); }
public void insertBefore (int place, int newData) throws InvalidPositionException {
position++;
PSDLNode temp = searchNode (place);
PSDLNode newNode = new PSDLNode
(newData, temp, temp.getPrev(), position);
temp.getPrev().setNext(newNode);
temp.setPrev(newNode);
size++;
}
public void insertAfter (int place, int newData) throws InvalidPositionException {
position++;
PSDLNode temp = searchNode (place);
PSDLNode newNode = new PSDLNode
(newData, temp.getNext(), temp, position);
temp.getNext().setPrev(newNode);
temp.setNext(newNode);
size++;
}
public int last () {
return (trailer.getPrev().getPosition());
}
public int first () {
return (header.getNext().getPosition());
}
public boolean search (int key) {
boolean result = false;
PSDLNode current = header.getNext();
while (current != trailer) {
if (current.getData () == key) {
result = true;
return result;
}
else
current = current.getNext();
}
return result;
}
public void traverse () {
System.out.println ("Current list: ");
PSDLNode current = header.getNext();
while (current != trailer) {
current.displayPSDLNode ();
current = current.getNext();
}
System.out.println ();
}
}
// the class PSDLLADT ends here
Iterators and Enumerators
An iterator is a software construct that scans through a collection of items,
processing one item at a time. To specify an iterator, we have to define:
1.
2.
3.
a sequence of items, S,
a starting position in S, and
a way of accessing the next position in S.
Java has a class called Enumeration (part of java.util package), which is a simple
iterator that allows for only one pass through the sequence. It has two methods:


hasMoreElements(); returns true if the Enumeration object contains more
items.
nextElement(); returns a reference to the next elements in the Enumeration. If
no next element exists, an exception NoSuchElementException is thrown.
An Enumerator object can use a Vector object to refer to a sequence of items. For
example, the statement
Enumeration enum = vector.elements();
uses Vector method elements to return a reference to Enumeration object, enum,
containing the elements of the Vector object, vector. Now,
enum.hasMoreElements();
returns true if more elements remain in the enum object.