Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Vector ADT versus Arrays • Vectors “borrow” the concept of array indexing: the user can perform operations on a given index (rank r) but are unaware of the internal implementation Lists and Sequences – Iterators • Internal implementation can be (extendable) arrays or doubly linked lists COSC 2011 • The software developer must ensure the operations are efficient extendable array implementation 5/25/2005 11:26 AM COSC 2011 1 5/25/2005 11:26 AM List ADT versus Linked Lists COSC 2011 2 Position ADT • Intuitive notion of “place” of an element • Similarly, List ADT borrows the concepts of nodes and “pointers” from linked lists • The user does not know about the internal implementation (e.g., singly or doubly linked list) • How can an element of the List be accessed? By position (equivalent to “node” of a linked list) • Position ADT supports one simple method: object element(): returns the object stored at this position • Positions are defined relatively to other positions (“before”, “after” relation) • Positions are not tied to an element or rank • Position p: a “pointer” p pointing to a node 5/25/2005 11:26 AM COSC 2011 3 List ADT • The List ADT models a sequence of positions storing arbitrary objects • It establishes a before/after relation between positions • Generic methods: – size(), isEmpty() • Query methods: – isFirst(p), isLast(p): return true if position p is the first (last) one in the list; false otherwise • Accessor methods: – first(), last(): return the position of the first (last) element of the list – before(p), after(p): return the position that comes before (after) position p 5/25/2005 11:26 AM COSC 2011 5/25/2005 11:26 AM COSC 2011 4 More List Operations • Update methods: – insertBefore(p, o), insertAfter(p, o): insert o into position before (after) p; return the position of the new element – insertFirst(o), insertLast(o): insert o as the first (last) element; return the position of the new element – replaceElement(p, o): replace the element at position p with o; return the old element at p – swapElements(p, q): swap the elements store at positions p and q – remove(p): remove from the list the element at posotion p; return the removed element 5 • Note: There are no references to “node” objects or their prev or next links 5/25/2005 11:26 AM COSC 2011 6 1 Notes on List Methods Doubly Linked List • • Some methods are redundant: isFirst(p) ⇔ p == first() isLast(p) ⇔ p == last() insertFirst(o) ⇔ insertBefore(first(), o) insertLast(o) ⇔ insertAfter(last(), o) • Reason: for code readability of common operations • • • Error conditions: – p = null – p was previously deleted from the list – p is a position in a different list 5/25/2005 11:26 AM COSC 2011 7 A doubly linked list provides a natural implementation of the List ADT Nodes implement Position ADT and store: – element – link to the previous node – link to the next node Special trailer and header nodes 5/25/2005 11:26 AM Insertion We visualize remove(p), where p = last() COSC 2011 9 Exceptions • BoundaryViolationException: thrown by – before(p) when p is already the first element of the list – after(p) when p is already the last element of the list 5/25/2005 11:26 AM COSC 2011 5/25/2005 11:26 AM COSC 2011 10 Performance Analysis • EmptyContainerException: thrown by first() and last() when the list is empty • InvalidPositionException: thrown by any method which requires at least one parameter Position p when – p is null – p is the position of the dummy header or trailer node – p was previously deleted (prev and next were set to null by method remove(p)) 8 Deletion We visualize operation insertAfter(p, X), which returns position q 5/25/2005 11:26 AM COSC 2011 • In the implementation of the List ADT using a doubly linked list – The space used by a list with n elements is O(n) – The space used by each position of the list is O(1) – Operation element() of the Position ADT runs in O(1) time – All the operations of the List ADT run in O(1) time • Note: The search for the position to insert/remove may take O(n) 11 5/25/2005 11:26 AM COSC 2011 12 2 Sequence ADT Applications of Sequences • The Sequence ADT is the • List-based methods: union of the Vector and – first(), last(), before(p), after(p), List ADTs replaceElement(p, o), • Elements accessed by swapElements(p, q), – Rank, or – Position – size(), isEmpty() – elemAtRank(r), replaceAtRank(r, o), insertAtRank(r, o), removeAtRank(r) 5/25/2005 11:26 AM • Direct applications: – Generic replacement for stack, queue, vector, list – Small database (e.g., address book) insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o), remove(p) • Generic methods: • Vector-based methods: • The Sequence ADT is a basic, general-purpose, data structure for storing an ordered collection of elements • Bridge methods: • Indirect applications: – Building block of more complex data structures – atRank(r): return a position – rankOf(p): return an integer rank COSC 2011 13 Implementation Using Doubly Linked Lists – Element – Rank • All vector-based methods (involving rank) run in O(n) time • Indices f and l keep track of first and last positions 15 Analysis of Array-based Implementation 0 1 2 3 S 5/25/2005 11:26 AM f COSC 2011 l 16 Array Implementation of Sequences public class MyNode implements Position { private int rank; private Object elem; • insertFirst, insertBefore, insertAfter, and remove run in O(n) time (shifting position objects) • All other methods take O(1) time // Constructor(s) Using a circular array: • insertFirst takes O(1) time • insertBefore, insertAfter, and remove still take O(n) time Worst case: insertion/deletion at rank n/2 // Method // setRank(), getRank() // setElement(), getElement() } 5/25/2005 11:26 AM 5/25/2005 11:26 AM COSC 2011 14 • We use an array storing positions • A position object stores: • All list-based methods run in O(1) time COSC 2011 COSC 2011 Array-based Implementation • The space used by a list with n elements is O(n) 5/25/2005 11:26 AM 5/25/2005 11:26 AM 17 public class MySequence implements Sequence { private MyNode[] A; private int capacity = 100; private int size = 0; public MySequence{ A = new MyNode[capacity]; } // Sequence methods // should cast Position back to MyNode before using “p” } COSC 2011 18 3 Comparison of Sequence Implementations Operation size, isEmpty atRank, rankOf, elemAtRank first, last, before, after replaceElement, swapElements replaceAtRank insertAtRank, removeAtRank insertFirst, insertLast insertAfter, insertBefore remove 5/25/2005 11:26 AM Array 1 1 1 1 1 n 1 n n Homework • Implement the Sequence methods using doubly linked lists and circular arrays. Verify the running times of the methods you write using the above table (Table 5.4 on page 226 of the textbook) List 1 n 1 1 n n 1 1 1 COSC 2011 19 5/25/2005 11:26 AM COSC 2011 20 Iterators • • • • • An iterator abstracts the • process of scanning through a collection of elements Methods of the Iterator ADT: • – boolean hasNext() – object nextObject() Extends the concept of Position by adding a traversal • capability Notion of “current” element First element: returned by first call to nextObject(). 5/25/2005 11:26 AM An iterator is typically associated with an another data structure (for sequential accesses) We can augment the Stack, Queue, Vector, List and Sequence ADTs with method: – Iterator elements() For lists and sequences: – Iterator positions() COSC 2011 21 public static void printVector(java.util.Vector vec) { java.util.Iterator iter = vec.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } } /** Returns an iterator of all the nodes in the list. */ public Iterator positions() { return new PositionIterator(this); } /** Returns an iterator of all the elements in the list. */ public Iterator elements() { return new ElementIterator(this); } 5/25/2005 11:26 AM COSC 2011 22 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 cur = list.next(cur); // move cursor to the next position return toReturn; } } 5/25/2005 11:26 AM COSC 2011 23 4