1 / 53

Sequences

Sequences. The Sequence Abstract Data Type. Implementing a Sequence. Implementation of a sequence with a doubly linked list:. Position rank. Sequence ADT. DNode atRank( r ) rankOf( p ). Doubly linked list. Implementing a Sequence with a Doubly Linked List. Class NodeSequence.

alvaroa
Télécharger la présentation

Sequences

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sequences

  2. The Sequence Abstract Data Type

  3. Implementing a Sequence

  4. Implementation of a sequence with a doubly linked list: Position rank Sequence ADT DNode atRank(r) rankOf(p) Doubly linked list

  5. Implementing a Sequence with a Doubly Linked List

  6. Class NodeSequence

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

  8. 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; } 1 2 n-3 0 r n-2 n-1 Number of hopping = r Number of hopping = n-r-1 i=0,1,...,r-1 i=1,2,...,n-r-1

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

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

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

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

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

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

  15. Implementing a Sequence with an Array

  16. Comparing Sequence Implementations

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

  18. Data Structure Exercises 9.1

  19. Iterators

  20. The Iterator Abstract Data Type

  21. Iterators in Java (in package java.util)

  22. 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 }

  23. 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; } }

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

  25. 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 }

  26. 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; } }

  27. Data Structure Exercises 10.1

  28. Case Study: Bubble-Sort on a Sequence

  29. The Bubble-Sort Algorithm

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

  31. Bubble Sort Using Ranks

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

More Related