Sequences. The Sequence Abstract Data Type Implementing a Sequence.

Slides:



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

Linked Lists Linear collections.
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.
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.
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
Tree representation and tree search - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 10.
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.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Data Structures and Algorithm Design (Review). Java basics Object-oriented design Stacks, queues, and deques Vectors, lists and sequences Trees and binary.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
UCD Computer Science COMP Vectors, Lists and Sequence Stacks, queues, deques -- access elements only at the “ends” –Useful for applications that.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic data structures, including –Stacks –Queues –Vectors –Lists.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Data Structures and Algorithm Design (Review). Java basics Object-oriented design Stacks, queues, and deques Vectors, lists and sequences Trees and binary.
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.
Chapter 6: Sequences : vectors and lists COL 106 Shweta Agrawal.
© 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.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
Information and Computer Sciences University of Hawaii, Manoa
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Vectors, Lists, Sequences. Vectors Linear sequence s of n elements e rank – number of elements before e in s Vector supports access to elements via their.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
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.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Lab - 11 Keerthi Nelaturu. Ordered Structure Using Doubly linked list All elements in the list are arranged in an order Implement the Ordered Structure.
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
EECE 310: Software Engineering
Recap: Solution of Last Lecture Activity
COMP9024: Data Structures and Algorithms
Stacks.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Sequences and Iterators
Data Structures and Algorithm Design (Review).
null, true, and false are also reserved.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Vectors, Lists and Sequences
Introduction to Stacks
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
Introduction to Stacks
Vectors and Array Lists
The Ranked Sequence ADT
Presentation transcript:

Sequences

The Sequence Abstract Data Type

Implementing a Sequence

Sequence ADT Positionrank Doubly linked list DNodeatRank(r) rankOf(p) Implementation of a sequence with a doubly linked list:

Implementing a Sequence with a Doubly Linked List

Class NodeSequence

The 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)

public Position atRank( int rank ) { DNode node; int n = size(); checkRank( rank, n ); if( rank <= n / 2 ) { node = header.getNext(); for( int i = 0; i < rank; i++ ) { node = node.getNext(); } } else { node = trailer.getPrev(); for( int i = 1; i < n - rank; i++ ) { node = node.getPrev(); } } return node; } 0 1 r 2 n-2n-1 Number of hopping = r i=0,1,...,r-1 Number of hopping = n-r-1 i=1,2,...,n-r-1 n-3

/** 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 i=1; i < size(); i++) { if (p == node) return i; else node = node.getNext();} }

/** 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 = n) throw new BoundaryViolationException("Illegal rank: " + r); }

/** 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 <= size()/2) { // scan forward from the head node = header.getNext(); for (int i=0; i < rank; i++) node = node.getNext(); } else { // scan backward from the tail node = trailer.getPrev(); for (int i=1; i < size()-rank; i++) node = node.getPrev();} return node; }

/** 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 i=1; i < size(); i++) { if (p == node) return i; else node = node.getNext();} }

/** Inserts an element at the given rank; O(n) time. */ public void insertAtRank (int rank, Object element) throws BoundaryViolationException { checkRank(rank, size() + 1); if (rank == size()) insertLast(element); else { insertBefore(atRank(rank), element); }

/** 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); }

Implementing a Sequence with an Array

Comparing Sequence Implementations

Interface Hierarchy for Vectors, Lists, and Sequences Vector (interface)List (interface) Sequence (interface)ArrayVector (class)NodeList (class) ArraySequence (class)NodeSequence (class) impl. extends impl. extends Supplement with the methods in Vector interface Supplement with the methods in List interface impl.

Data Structure Exercises 9.1

Iterators

The Iterator Abstract Data Type

Iterators in Java (in package java.util)

An 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 }

public boolean hasNext() { return (cur != null); } public Object next() throws NoSuchElementException { 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) } catch (InvalidPositionException e) { } } // move cursor to the next position return toReturn; }

class NoSuchElementException extends Exception { public NoSuchElementException() {super();} public NoSuchElementException(String s) { super(s); } }

In 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 }

public boolean hasNext() { return (cur != null); } public Object next() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException("No 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; }

Data Structure Exercises 10.1

Case Study: Bubble-Sort on a Sequence

The Bubble-Sort Algorithm

2, 3, 5, 6, 7, 9 i for a certain i r

Bubble Sort Using Ranks

If the sequence is array-based, the running time is proportional to the square of n, the size of the sequence: running time O(n 2 ). On the other hand, if the sequence is position-based, the running time is proportional to n 3. running time O(n 3 ). In this case, the atRank operation needs to perform link hopping.

Bubble Sort Using Positions

The running time is proportional to the square of n, the size of the sequence, regardless of how the sequence is implemented: running time O(n 2 ).

Data Structure Exercises 9.2