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.

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.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 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.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Sequences. The Sequence Abstract Data Type Implementing a Sequence.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Vectors, Lists, and Sequences - Ed. 2 and 3.: Chapter 5 - Ed. 4: Chapter 6.
© 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.
Data Structures Using C++ 2E
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Using Java1 Chapter 4 Linked Lists.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
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.
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Chapter 5 Linked Lists II
Linked Structures, LinkedStack
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Java linked list.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Priority Queues CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
LINKED LISTS.
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.
Elementary Data Structures
Lists Rem Collier Room A1.02
Linked List Stacks, Linked List Queues, Dequeues
COMP9024: Data Structures and Algorithms
Java collections library
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Sequences and Iterators
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Recall What is a Data Structure Very Fundamental Data Structures
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.
CS210- Lecture 6 Jun 13, 2005 Announcements
General List.
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
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.
The Ranked Sequence ADT
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] 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 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: 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 { 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) { 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.a sequence of items, S, 2.a starting position in S, and 3.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.