csc 212 vectors, lists, & sequences. announcement daily quizzes accepted electronically only ...
Post on 20-Dec-2015
214 views
TRANSCRIPT
CSC 212
Vectors, Lists, & Sequences
Announcement
Daily quizzes accepted electronically onlySubmit via one or other DropboxE-mail also accepted, if necessary
Next homework assignment due Thursday Midterm in a little over one week
Will cover through chapter 5 of bookMidterm will be open book, open note (but, closed
computer)Thursday’s lecture includes a review
Rank of Element
Describes the position of element in a list Item at front of list has rank of 02nd item has rank of 1; 3rd item has rank of 2Generally, nth item has rank of n – 1
Ranking Details
Ranks do not depend on arraysThey specify how to order lists of elements
independent of implementation details Arrays can maintain ranks, but so can:
Linked listsDoubly linked listsSpecially trained monkeys…
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 dequesCan insert and remove any elementCan also replace a specific element
Item Retrieval
Any element in a Vector can be retrieved using Object elemAtRank(int r)
Note: this is different than using an arrayObject[] arr;Vector vect;...Object o = arr[4]; // legalo = vect[4]; // No!o = vect.elemAtRank(4) // legalo = arr.elemAtRank(4) // No go!
Insertion into Vectors
Insertion can occur anywherevoid 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?
Removal from Vectors
Can also remove any elementObject 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()?
New Option Excitement!
Vectors can also replace elementsObject replaceAtRank(int r, Object e)
Throws same exceptions as previous methods Returns element previously at rank r Ranks of remaining elements are unchanged
Vector Implementation
What could we use to implement vectors and how?
Array-based Vector
Arrays make implementing Vectors simple, except…
… inserting an element. What is big-Oh?
Vector 0 1 2 r
Vector 0 1 2 r
Vector0 1 2
er
Array-based Vector
Removing from a Vector is not easy either…
Vector 0 1 2 r
Vector0 1 2
er
Vector 0 1 2 r
Array-based Vector
How would you implement size()?Vector 0 1 2 r
Linked Lists Not a Panacea
What is complexity of insertAtRank, removeAtRank, and replaceAtRank?
Vector Growth
Using linked lists removes size limitBut we could get unbounded size using arrays
Whenever array fills, allocate larger arrayObject[] temp = new Object[...];for (int i = 0; i < arr.size; i++)
temp[i] = arr[i];arr = temp;
Growth Strategies
Two different strategies for growth:Grow by constant, c, each timeDouble size each time
Both have same big-Oh complexity.What is it?When does it occur?
Instead compare amortized (average) timeConsider inserting n elements
Constant Growth
Need to grow k = n/c timesEach time we grow we must copy entire arrayTotal 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)= ½ck2 + 1= O(n2)
For each of n inserts, avg complexity is O(n)
Doubling Growth
Array grows k = log n timesEach time we grow we must copy entire arrayTotal copies:
1 + 2 + 4 + • • • + 2k-1 + 2k
= 2k + 2k
= 2k+1
= 2 * 2log n
= 2 * nFor each insert, average complexity only O(1)
Adapter Pattern
Implementation using other class instanceExample: Implementing Stack with Deque
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); } •••
Positions
Sometimes rankings may not make senseElements may not have absolute positionsMay not want to pay time overhead
Instead use relative rankingsRetrieve elements before or after current oneAdd new element before or after current oneReplace/remove current elementGet first/last elements
Position ADT
ADT implementing these relative rankings
public interface Position { public Object element();}
List ADT
Simplest collection of Positions is List Defines usual int size() & boolean isEmpty() methods
List ADT
Also defines accessors:Position first()
Position last()
Position next(Position p) throws InvalidPositionException, BoundaryViolationException
Position prev(Position p) throws InvalidPositionException, BoundaryViolationException
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
Implementation of List
How could we implement List & Position?
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 rankpublic interface Sequence extends List, Vector {public Position atRank(int r) throws •••
public int rankOf(Position p) throws •••
}
Daily Quiz
Use adapter pattern to write a Deque implementationAdapt using an implementation of VectorA 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