CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList
Question of the Day What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side. A A B B
Deque ADT Another type of Collection class Stands for Double Ended QUEue Combines ideas from Stack and Queue ADT Manipulate elements at front & rear addFront(), addLast() removeFront(), removeLast() getFront(), getLast() Can be implemented with array or linked list If using linked list, should be doubly-linked list
List ADT Sometime want use of entire Collection Add new element before existing one Get the 3 rd element in the Collection Loop over all elements without removing them Cannot be done with Stack, Queue & Deque Lists can access all of its elements But provide different means for this access Will discuss over next several lectures
IndexList ADT Also called a Vector Names are used interchangeably First example of a List Extends idea of an array Stores arbitrary sequence of elements Elements may appear multiple times Access elements using integer rank Like array indexes, does not imply ordering
Ranks ArrayList organizes collection using rank Item at front of list has rank of 0 2 nd item has rank of 1 3 rd item has rank of 2 n th item has rank of n – 1 Ranks increase sequentially Merely discusses position of element Cannot skip over a rank Cannot repeat a rank
IndexList Interface public interface IndexList extends Collection { public void add(int rank, E e) throws IndexOutOfBoundsException; public E get(int rank) throws IndexOutOfBoundsException; public E remove(int rank) throws IndexOutOfBoundsException; public E set(int rank, E newValue) throws IndexOutOfBoundsException; }
IndexList != array Extends the idea of an array, but… IndexList does not have constant size Elements’ ranks may change over time Can implement using: Array, Singly-linked list, Doubly-linked list, Specially trained monkeys, College students
Insertion add( r, e ) “shifts” existing elements down to make room for e For linked-list, insert node at proper location For array, must shift elements down Can take O(n) time S 012n r S 012n r S 012n e r
Deletion remove( r ) “shifts” remaining elements up to fill hole created by removal For linked-list, happens automatically For array, must shift elements up Can also take O(n) time S 012n r S 012n r S 012n r
IndexList’s Operations add, get, & remove similar to past operations add ≈ addFront, addLast, enqueue, push get ≈ getFront, getLast, front, top remove ≈ removeFront,removeLast,dequeue,pop But, set is a brand new operation Stores the new element at the given rank Removes (& returns) element already there Does not change rank of other elements
IndexList’s Exceptions Any of these methods may throw IndexOutOfBoundsException Thrown when rank is illegal for operation add accepts ranks 0 - size() add(0, e) e added to front of List add(size(), e) e added at end of List get, set, & remove use ranks 0 - size()-1 Ranks start at 0, so no element at size() rank
Implement IndexList with array Can allocating & copy into larger array Increase array length by constant, c -or- Double length of the array each time Both approaches have O( n ) complexity Cost of copying entire array But have different amortized complexities Consider cost of growth due to n calls to add()
Constant Growth Need to grow k = n / c times Copy entire array with each growth, so total copies is: 1 + ( c +1) + (2 c +1) + + ((( k -1) *c )+1) + ( k*c +1) = (( k * c ) + 2) + (( k * c ) + 2) + + (( k * c ) + 2) = k / 2 * (( k * c ) + 2) = O( c*k 2 )= O( c * ( n / c ) 2 ) = O( n 2 * 1 / c ) = O( n 2 ) Average cost: O( n 2 ) / n = O( n )
Doubling Growth Array grows k = log n times Still copy entire array, for this many copies: k k = (2 k - 1) + 2 k = 2 k = (2 * 2 k ) - 1 = (2 * 2 log n ) - 1 = O(2 n - 1) = O( n ) Average cost: O( n ) / n = O(1) But, moving elements in add() still costs O( n )!
Your Turn Get back into groups and do activity
Before Next Lecture… Keep up with your reading! Continue Week #9 Assignment Work on Programming Assignment #2