The Ranked Sequence ADT

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 Lists Dr Zeinab Eid.
Linked Lists Chapter 4.
Queues and Linked Lists
Linked Lists Linear collections.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
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.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
The Priority Queue ADT A priority queue is a restricted form of a list, where items are arranged according to their priorities (or keys). The key assigned.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
Sequences. The Sequence Abstract Data Type Implementing a Sequence.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Lists. The Position Abstract Data Type A positionp, which is associated with some elemente in a list S, does not change, even if the rank ofe changes.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Chapter 5 Linked Lists II
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right subtrees.
Linked List, Stacks Queues
Elementary Data Structures
CHAPTER 4: Linked Structures
Lists Rem Collier Room A1.02
C++ Programming:. Program Design Including
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
The List ADT.
Cpt S 122 – Data Structures Abstract Data Types
Lectures linked lists Chapter 6 of textbook
Big-O notation Linked lists
Linked List Stacks, Linked List Queues, Dequeues
Sequences and Iterators
COMP9024: Data Structures and Algorithms
UNIT-3 LINKED LIST.
CS212D: Data Structures Week 5-6 Linked List.
Stacks.
Java collections library
COP 3538 Data Structures with OOP
CS212D: Data Structures Week 5-6 Linked List.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Sequences and Iterators
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
" A list is only as strong as its weakest link. " - Donald Knuth
Linked Lists [AJ 15].
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Vectors, Lists and Sequences
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Vectors, Lists, and Sequences
CS210- Lecture 6 Jun 13, 2005 Announcements
General List.
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
A type is a collection of values
The Priority Queue ADT A priority queue is a restricted form of a list, where items are arranged according to their priorities (or keys). The key assigned.
Presentation transcript:

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) Returns the item at the specified rank replaceItem (rank, newItem) Replaces the item at the specified rank with newItem insertItem (rank, newItem) Inserts newItem at the specified rank deleteItem (rank) Deletes the item at the specified rank size () Returns the size of the sequence empty () Returns true is the sequence is empty traverse () 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] 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) Replaces data at position with item swap (position1, position2) Swaps items in positions 1 and 2 first () Returns the position of the first item in the sequence last () Returns the position of the last item in the sequence before (position) Returns the position of the item preceding the one in position after (position) Returns the position of the item following the one in position insertFirst (item) Inserts item at the beginning of the sequence insertLast (item) Inserts item at the end of the sequence insertBefore (position, item) Inserts item before the one in position insertAfter (position, item) Inserts item after the one in position delete (position) Deletes the item in position deleteFirst () Deletes the first item in the sequence deleteLast () Deletes the last item in the sequence size () Returns the size of the sequence empty () Returns true is the sequence is empty traverse () 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: remove(4) Current list: data 8; position 1 data 3; position 3 data 5; position 2 Operation: swap(1, 2) data 5; position 1 data 8; position 2 Operation: replace(3, 7) data 7; position 3 Operation: insertAfter(first(), 2) data 2; position 5 Operation: insertFirst(8) Current list: data 8; position 1 Operation: insertAfter(1, 5) data 5; position 2 Operation: insertBefore(2, 3) data 3; position 3 Operation: insertFirst(9) data 9; position 4 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

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 { private int data; private PSDLNode next, prev; int pos; public PSDLNode () { } public PSDLNode (int d) { data = d; } public PSDLNode (int newData, PSDLNode newNext, PSDLNode newPrev, int newPos) { data = newData; next = newNext; prev = newPrev; pos = newPos; } public void setData (int newData) { data = newData; } public void setNext (PSDLNode newNext) { next = newNext; } public void setPrev (PSDLNode newPrev) { prev = newPrev; } public void setPosition (int newPos) { pos = newPos; } public int getData () { return data; } public PSDLNode getNext () { return next; } public PSDLNode getPrev () { return prev; } public int getPosition () { return pos; } public void displayPSDLNode () { System.out.print ("data " + data + "; position " + pos); System.out.println (); }

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) { 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); 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 { temp.setData(newData); 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 { 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 { (newData, temp.getNext(), temp, position); temp.getNext().setPrev(newNode); temp.setNext(newNode); 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(); public void traverse () { System.out.println ("Current list: "); current.displayPSDLNode (); 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: 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.