Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Similar presentations


Presentation on theme: "CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,"— Presentation transcript:

1 CSC 212 Vectors, Lists, & Sequences

2 Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted, if necessary Next homework assignment due Thursday Midterm in a little over one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed computer)  Thursday’s lecture includes a review

3 Rank of Element Describes the position of element in a list  Item at front of list has rank of 0  2 nd item has rank of 1; 3 rd item has rank of 2  Generally, n th item has rank of n – 1

4 Ranking Details Ranks do not depend on arrays  They specify how to order lists of elements independent of implementation details Arrays can maintain ranks, but so can:  Linked lists  Doubly linked lists  Specially trained monkeys…

5 Vector ADT Vector is first type which uses ranks Similar to previous ADTs in that it defines  int size()  boolean isEmpty() Differs from stacks, queues, and deques  Can insert and remove any element  Can also replace a specific element

6 Item Retrieval Any element in a Vector can be retrieved using Object elemAtRank(int r) Note: this is different than using an array Object[] arr; Vector vect;... Object o = arr[4]; // legal o = vect[4]; // No! o = vect.elemAtRank(4) // legal o = arr.elemAtRank(4) // No go!

7 Insertion into Vectors Insertion can occur anywhere  void insertAtRank(int r, Object e) Inserts object e at rank r Increases rank of objects now after it  What exceptions do we need to include?

8 Removal from Vectors Can also remove any element  Object removeAtRank(int r) Removes object at rank r Decreases rank of elements after object being removed  Could this throw any exceptions beyond those defined by insertAtRank() ?

9 New Option Excitement! Vectors can also replace elements Object replaceAtRank(int r, Object e) Throws same exceptions as previous methods Returns element previously at rank r Ranks of remaining elements are unchanged

10 Vector Implementation What could we use to implement vectors and how?

11 Array-based Vector Arrays make implementing Vectors simple, except… … inserting an element. What is big-Oh? Vector 012 r 012 r 012 e r

12 Array-based Vector Removing from a Vector is not easy either… Vector 012 r 012 e r 012 r

13 Array-based Vector How would you implement size() ? Vector 012 r

14 Linked Lists Not a Panacea What is complexity of insertAtRank, removeAtRank, and replaceAtRank ?

15 Vector Growth Using linked lists removes size limit  But we could get unbounded size using arrays Whenever array fills, allocate larger array Object[] temp = new Object[...]; for (int i = 0; i < arr.size; i++) temp[i] = arr[i]; arr = temp;

16 Growth Strategies Two different strategies for growth:  Grow by constant, c, each time  Double size each time Both have same big-Oh complexity.  What is it?  When does it occur? Instead compare amortized (average) time  Consider inserting n elements

17 Constant Growth Need to grow k = n/c times  Each time we grow we must copy entire array  Total copies is: 1 + (c+1) + (2c+1) + + ((k-1)*c)+1 + k*c+1 = k*c+2 + k*c+2 + + k*c+2 = k/2 * ((k*c)+2) = ½ck 2 + 1 = O(n 2 )  For each of n inserts, avg complexity is O(n)

18 Doubling Growth Array grows k = log n times  Each time we grow we must copy entire array  Total copies: 1 + 2 + 4 + + 2 k-1 + 2 k = 2 k + 2 k = 2 k+1 = 2 * 2 log n = 2 * n  For each insert, average complexity only O(1)

19 Adapter Pattern Implementation using other class instance  Example: Implementing Stack with Deque

20 Adapter Pattern public class Stack { protected Deque deque; public Stack() {deque = new Deque();} public int size() { return deque.size(); } public boolean isEmpty() { return deque.isEmpty(); } public void push(Object e) { deque.insertFirst(e); }

21 Positions Sometimes rankings may not make sense  Elements may not have absolute positions  May not want to pay time overhead Instead use relative rankings  Retrieve elements before or after current one  Add new element before or after current one  Replace/remove current element  Get first/last elements

22 Position ADT ADT implementing these relative rankings public interface Position { public Object element(); }

23 List ADT Simplest collection of Position s is List Defines usual int size() & boolean isEmpty() methods

24 List ADT Also defines accessors: Position first() Position last() Position next(Position p) throws InvalidPositionException, BoundaryViolationException Position prev(Position p) throws InvalidPositionException, BoundaryViolationException

25 List ADT Defines update methods:  insertFirst, insertLast, insertBefore, insertAfter Takes an object and returns the Position  remove Removes the position passed in as parameter  replace Substitutes object for element currently at position

26 Implementation of List How could we implement List & Position?

27 Sequences Sequences combine List and Vector ADTs Also enables conversion between the two  atRank – returns the position object for a given rank  rankOf – given a position, returns its rank public interface Sequence extends List, Vector { public Position atRank(int r) throws public int rankOf(Position p) throws }

28 Daily Quiz Use adapter pattern to write a Deque implementation  Adapt using an implementation of Vector  A vector class, LLVector, the exceptions LLVector needs, and the Deque & Vector interfaces available on quiz page. Useful for student looking to practice Java skills by compiling/testing solutions


Download ppt "CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,"

Similar presentations


Ads by Google