The Ranked Sequence ADT Definition A ranked sequence

  • Slides: 17
Download presentation
The Ranked Sequence ADT Definition A ranked sequence is a collection of items arranged

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: return. Item (rank) replace. Item (rank, new. Item) insert. Item (rank, new. Item) delete. Item (rank) size () empty () traverse () Returns the item at the specified rank Replaces the item at the specified rank with new. Item Inserts new. Item 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

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 return. Item (rank): return S[rank] O(1) efficiency Algorithm replace. Item (rank, new. Item): O(1) efficiency S[rank] : = new. Item Algorithm insert. Item (rank, new. Item): O(n) efficiency for i = n-1 to rank do S[i+1] : = S[i] S[rank] : = new. Item n++ Algorithm delete. Item (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 insert. Item method Consider

A doubly linked list implementation of a ranked sequence: the insert. Item method Consider the following support methods: check. Rank(rank), which checks if rank is within the legal bounds and if not throws an out of bounds exception, and node. At. Rank(rank), which returns the node at the specified rank. private void check. Rank (int rank) throws Boundary. Violation. Exception { if (rank < 0 || rank > size() - 1) throw new Boundary Violation. Exception (“Invalid rank. ”); } private DLNode node. At. Rank (int rank) { DLNode node; if (rank <= size()/2) { node = header. get. Next(); for (int i=0; i<rank; i++) node = node. get. Next(); } else { node = trailer. get. Prev(); for (int i=0; i < size()-rank-1; i++) node = node. get. Prev(); } return node; }

A doubly linked list implementation of a ranked sequence: the insert. Item method (cont.

A doubly linked list implementation of a ranked sequence: the insert. Item method (cont. ) public void insert. Item (int rank, Object item) throws Boundary. Violation. Exception { if (rank != size()) check. Rank (rank); DLNode next = node. At. Rank (rank); DLNode prev = next. get. Prev(); DLNode node = new DLNode (item, next, prev); next. set. Prev(node); prev. set. Next(node); size++; } The efficiency of this method is O(n/2), because of the node. At. Rank method, which in the worst case traverses the half of the sequence.

A doubly linked list implementation of a ranked sequence: the delete. Item method public

A doubly linked list implementation of a ranked sequence: the delete. Item method public Object delete. Item (int rank) throws Boundary Violation. Exception { check. Rank (rank); DLNode node = node. At. Rank(rank); DLNode next = node. get. Next(); DLNode prev = node. get. Prev(); prev. set. Next(next); next. set. Prev(prev); size - -; return node. get. Data(); } The efficiency of this method is O(n/2), because of the node. At. Rank 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

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 (position 1, position 2) first () last () before (position) after (position) insert. First (item) insert. Last (item) insert. Before (position, item) insert. After (position, item) delete (position) delete. First () delete. Last () 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

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+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

Example: Consider an initially empty sequence to which the following set of operations is applied: Operation: insert. First(8) Current list: data 8; position 1 Operation: insert. After(1, 5) Current list: data 8; position 1 data 5; position 2 Operation: insert. Before(2, 3) Current list: data 8; position 1 data 3; position 3 data 5; position 2 Operation: insert. First(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: insert. After(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,

The Positional Sequence ADT interface public interface PSDLL { public int replace (int place, int item) throws Invalid. Position. Exception; public void insert. Before (int place, int item) throws Invalid. Position. Exception; public void insert. After (int place, int item) throws Invalid. Position. Exception; public void swap (int place 1, int place 2) throws Invalid. Position. Exception; public int delete (int place) throws Invalid. Position. Exception; public int before (int place) throws Invalid. Position. Exception; public int after (int place) throws Invalid. Position. Exception; public void insert. First (int item); public void insert. Last (int item); public PSDLNode delete. First (); public PSDLNode delete. Last (); public int size(); public int last (); public int first (); public boolean empty (); public void traverse (); }

Positional Sequence nodes class PSDLNode { public void set. Prev (PSDLNode new. Prev) {

Positional Sequence nodes class PSDLNode { public void set. Prev (PSDLNode new. Prev) { prev = new. Prev; } private int data; private PSDLNode next, prev; int pos; public PSDLNode () { public void set. Position (int new. Pos) { pos = new. Pos; } } public int get. Data () { return data; } public PSDLNode (int d) { data = d; } public PSDLNode get. Next () { return next; } public PSDLNode (int new. Data, PSDLNode new. Next, PSDLNode new. Prev, int new. Pos) { data = new. Data; next = new. Next; prev = new. Prev; pos = new. Pos; } public PSDLNode get. Prev () { return prev; } public int get. Position () { return pos; } public void set. Data (int new. Data) { data = new. Data; } public void display. PSDLNode () { System. out. print ("data " + data + "; position " + pos); System. out. println (); } public void set. Next (PSDLNode new. Next) { next = new. Next; } }

Positional Sequence ADT -- doubly linked list implementation class PSDLLADT implements PSDLL { private

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. set. Next(trailer); header. set. Prev(null); header. set. Data(0); header. set. Position(0); trailer. set. Prev(header); trailer. set. Next(null); trailer. set. Data(0); trailer. set. Position(0); size = 0; }

public boolean empty () { return (size == 0); } public int size ()

public boolean empty () { return (size == 0); } public int size () { return size; } public int before (int place) throws Invalid. Position. Exception { return (search. Node(place)). get. Prev(). get. Position(); } public int after (int place) throws Invalid. Position. Exception { return (search. Node(place)). get. Next(). get. Position(); } public void insert. First (int new. Data) { position++; PSDLNode old. First = header. get. Next(); PSDLNode new. First = new PSDLNode (new. Data, old. First, header, position); old. First. set. Prev(new. First); header. set. Next(new. First); size++; } public void insert. Last (int new. Data) { position++; PSDLNode old. Last = trailer. get. Prev(); PSDLNode new. Last = new PSDLNode (new. Data, trailer, old. Last, position); old. Last. set. Next(new. Last); trailer. set. Prev(new. Last); size++; }

public PSDLNode delete. First () { PSDLNode old. First = header. get. Next(); PSDLNode

public PSDLNode delete. First () { PSDLNode old. First = header. get. Next(); PSDLNode new. First = old. First. get. Next(); new. First. set. Prev(header); header. set. Next(new. First); size--; return old. First; } public PSDLNode delete. Last () { PSDLNode old. Last = trailer. get. Prev(); PSDLNode new. Last = old. Last. get. Prev(); trailer. set. Prev(new. Last); new. Last. set. Next(trailer); size--; return old. Last; } private PSDLNode search. Node (int position) throws Invalid. Position. Exception { PSDLNode current = header. get. Next(); while (current != trailer) { if (current. get. Position () == position) return current; else current = current. get. Next(); } throw new Invalid. Position. Exception ("An invalid position was passed to the search. Node method. "); }

public int delete (int place) throws Invalid. Position. Exception { PSDLNode temp = search.

public int delete (int place) throws Invalid. Position. Exception { PSDLNode temp = search. Node (place); int old. Item = temp. get. Data(); PSDLNode old. Next = temp. get. Next(); PSDLNode old. Prev = temp. get. Prev(); old. Next. set. Prev(old. Prev); old. Prev. set. Next(old. Next); size--; return old. Item; } public int replace (int place, int new. Data) throws Invalid. Position. Exception { PSDLNode temp = search. Node (place); int old. Item = temp. get. Data(); temp. set. Data(new. Data); return old. Item; } public void swap (int place 1, int place 2) throws Invalid. Position. Exception { PSDLNode node 1 = search. Node(place 1); PSDLNode node 2 = search. Node(place 2); PSDLNode temp = new PSDLNode(); temp. set. Data(node 1. get. Data()); node 1. set. Data(node 2. get. Data()); node 2. set. Data(temp. get. Data()); }

public void insert. Before (int place, int new. Data) throws Invalid. Position. Exception {

public void insert. Before (int place, int new. Data) throws Invalid. Position. Exception { position++; PSDLNode temp = search. Node (place); PSDLNode new. Node = new PSDLNode (new. Data, temp. get. Prev(), position); temp. get. Prev(). set. Next(new. Node); temp. set. Prev(new. Node); size++; } public void insert. After (int place, int new. Data) throws Invalid. Position. Exception { position++; PSDLNode temp = search. Node (place); PSDLNode new. Node = new PSDLNode (new. Data, temp. get. Next(), temp, position); temp. get. Next(). set. Prev(new. Node); temp. set. Next(new. Node); size++; } public int last () { return (trailer. get. Prev(). get. Position()); } public int first () { return (header. get. Next(). get. Position()); }

public boolean search (int key) { boolean result = false; PSDLNode current = header.

public boolean search (int key) { boolean result = false; PSDLNode current = header. get. Next(); while (current != trailer) { if (current. get. Data () == key) { result = true; return result; } else current = current. get. Next(); } return result; } public void traverse () { System. out. println ("Current list: "); PSDLNode current = header. get. Next(); while (current != trailer) { current. display. PSDLNode (); current = current. get. Next(); } System. out. println (); } } // the class PSDLLADT ends here

Iterators and Enumerators An iterator is a software construct that scans through a collection

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: Ø Ø has. More. Elements(); returns true if the Enumeration object contains more items. next. Element(); returns a reference to the next elements in the Enumeration. If no next element exists, an exception No. Such. Element. Exception 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. has. More. Elements(); returns true if more elements remain in the enum object.