Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequences. The Sequence Abstract Data Type Implementing a Sequence.

Similar presentations


Presentation on theme: "Sequences. The Sequence Abstract Data Type Implementing a Sequence."— Presentation transcript:

1 Sequences

2

3 The Sequence Abstract Data Type

4

5 Implementing a Sequence

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

7 Implementing a Sequence with a Doubly Linked List

8 Class NodeSequence

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

10

11 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

12

13

14

15

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

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

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

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

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

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

22 Implementing a Sequence with an Array

23

24

25

26

27 Comparing Sequence Implementations

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

29 Data Structure Exercises 9.1

30 Iterators

31

32 The Iterator Abstract Data Type

33 Iterators in Java (in package java.util)

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

35 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; }

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

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

38 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; }

39

40

41 Data Structure Exercises 10.1

42 Case Study: Bubble-Sort on a Sequence

43 The Bubble-Sort Algorithm

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

45

46

47

48

49 Bubble Sort Using Ranks

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

51 Bubble Sort Using Positions

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

53 Data Structure Exercises 9.2


Download ppt "Sequences. The Sequence Abstract Data Type Implementing a Sequence."

Similar presentations


Ads by Google