Download presentation

Presentation is loading. Please wait.

Published byMariah Northcutt Modified about 1 year ago

1
© 2004 Goodrich, Tamassia Sequences and Iterators1

2
© 2004 Goodrich, Tamassia Sequences and Iterators2 The Vector ADT (a.k.a. ArrayList) The Vector ADT extends the notion of array by storing a sequence of arbitrary objects An element can be accessed, inserted or removed by specifying its rank (number of elements preceding it) An exception is thrown if an invalid rank is specified (e.g., a negative rank) No exception for “full”! Main vector operations: object elemAtRank(integer r): returns the element at rank r without removing it object replaceAtRank(integer r, object o): replace the element at rank with o and return the old element insertAtRank(integer r, object o): insert a new element o to have rank r object removeAtRank(integer r): removes and returns the element at rank r Additional operations size() and isEmpty()

3
© 2004 Goodrich, Tamassia Sequences and Iterators3 Array-based Vector Use an array V of size N A variable n keeps track of the size of the vector (number of elements stored) Operation elemAtRank ( r ) is implemented in O(1) time by returning V[r] V 012n r

4
© 2004 Goodrich, Tamassia Sequences and Iterators4 Insertion In operation insertAtRank ( r, o ), we need to make room for the new element by shifting forward the n r elements V[r], …, V[n 1] In the worst case ( r 0 ), this takes O(n) time V 012n r V 012n r V 012n o r

5
© 2004 Goodrich, Tamassia Sequences and Iterators5 Deletion In operation removeAtRank ( r ), we need to fill the hole left by the removed element by shifting backward the n r 1 elements V[r 1], …, V[n 1] In the worst case ( r 0 ), this takes O(n) time V 012n r V 012n o r V 012n r

6
© 2004 Goodrich, Tamassia Sequences and Iterators6 Performance In the array based implementation of a Vector The space used by the data structure is O(n) size, isEmpty, elemAtRank and replaceAtRank run in O(1) time insertAtRank and removeAtRank run in O(n) time If we use the array in a circular fashion, insertAtRank(0) and removeAtRank(0) run in O(1) time In an insertAtRank operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one

7
© 2004 Goodrich, Tamassia Sequences and Iterators7 Growable Array-based Vector In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c doubling strategy: double the size Algorithm push(o) if t = S.length 1 then A new array of size … for i 0 to t do A[i] S[i] S A t t + 1 S[t] o

8
© 2004 Goodrich, Tamassia Sequences and Iterators8 Sequence ADT The Sequence ADT is the union of the Vector ADT and the List ADT Elements accessed by Rank, or Position Generic methods: size(), isEmpty() Vector-based methods: elemAtRank(r), replaceAtRank(r, o), insertAtRank(r, o), removeAtRank(r) List-based methods: first(), last(), prev(p), next(p), replace(p, o), insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o), remove(p) Bridge methods: atRank(r), rankOf(p)

9
© 2004 Goodrich, Tamassia Sequences and Iterators9 Applications of Sequences The Sequence ADT is a basic, general- purpose, data structure for storing an ordered collection of elements Direct applications: Generic replacement for stack, queue, vector, or list simple database (e.g., address book) Indirect applications: Building block of more complex data structures

10
© 2004 Goodrich, Tamassia Sequences and Iterators10 Linked List Implementation A doubly linked list provides a reasonable implementation of the Sequence ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes trailer header nodes/positions elements Position-based methods run in constant time Rank-based methods require searching from header or trailer while keeping track of ranks; hence, run in linear time Adapter design pattern

11
© 2004 Goodrich, Tamassia Sequences and Iterators11 Array-based Implementation We use a circular array storing positions A position object stores: Element Rank Indices f and l keep track of first and last positions 0123 positions elements S lf

12
© 2004 Goodrich, Tamassia Sequences and Iterators12 Sequence Implementations OperationArrayList size, isEmpty 11 atRank, rankOf, elemAtRank 1n first, last, prev, next 11 replace 11 replaceAtRank 1n insertAtRank, removeAtRank nn insertFirst, insertLast 11 insertAfter, insertBefore n1 remove n1

13
© 2004 Goodrich, Tamassia Sequences and Iterators13 Iterators An iterator abstracts the process of scanning through a collection of elements Methods of the ObjectIterator ADT: object object() boolean hasNext() object nextObject() reset() Extends the concept of Position by adding a traversal capability Implementation with an array or singly linked list An iterator is typically associated with another data structure We can augment the Stack, Queue, Vector, List and Sequence ADTs with method: ObjectIterator elements() Two notions of iterator: snapshot: freezes the contents of the data structure at a given time dynamic: follows changes to the data structure

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google