Vectors, Lists, and Sequences - Ed. 2 and 3.: Chapter 5 - Ed. 4: Chapter 6 Vectors, Lists, and Sequences (Chapter 5) • Vectors - Vector ADT and implementation • Lists - List ADT and implementation • Sequences - Sequence ADT and implementation • Iterators Vectors In a 2-D plane, a vector is defined as (x,y). y 3 2 (2, 1) 1 0 (1, 2) 1 2 3 x sequence: Each object comes before or after another. Examples: - A book is broken down into pages: p.1, p.2,…, p.205, … - A computer program is broken down into instructions - Stacks, queues and deques are special types of sequences with some constraints such as that elements can be added and removed at the ends. Non-examples: - A bag of coins; a box of bullets - Plates on a desk vector: A linear sequence that supports access to its elements by their ranks. The rank of a given element: The number of the elements that are before the given element. Example: (4, 3, 5, 2, 9) Element 4 3 5 2 9 Rank 0 1 2 3 4 The ranks are between 0 and n - 1 (n = 5 in this example). Examples of vectors: A point in a 3-D space: (x, y, z) A lottery number: 34, 3, 12, 8, 28, 6 Employee records stored in an array Examples of non-vectors: A stack – the elements in a stack can be visited only at the top (not by the ranks.) A queue – the elements in a queue can be visited at and taken from the head and added at the rear (not by the ranks.) A table: age sex height weight 25 M 250 180 36 F 180 168 However, we can use vector to represent the data: (25, 0, 250, 180) (36, 1, 180, 168) This table shows a series of rank-based operations on an initially empty vector S. Operation insert 7 at rank 0 insert 4 at rank 0 return the element at rank 1 insert 2 at rank 2 return the element at rank 3 Insert 5 at rank 4 remove the element at rank 1 insert 5 at rank 1 insert 3 at rank 1 insert 9 at rank 4 return the element at rank 2 Output 7 “error” “error” 5 S (7) (4,7) (4,7) (4,7,2) (4,7,2) (4,7,2) (4,2) (4,5,2) (4,3,5,2) (4,3,5,2,9) (4,3,5,2,9) The Vector Abstract Data Type A vector S is an abstract data type that supports the following fundamental methods: elemAtRank(r): Return the element of S with rank r; an error condition occurs if r < 0 or r > n - 1, where n is the current number of elements. Input: Integer; Output: Object replaceAtRank(r,e): Replace with e the element at rank r and return it; an error condition occurs if r < 0 or r > n - 1, where n is the current number of elements. Input: Integer r and Object e; Output: Object insertAtRank(r,e): Insert a new element e into S to have rank r; an error condition occurs if r < 0 or r > n, where n is the number of elements before the insertion. Input: Integer r and Object e; Output: None. removeAtRank(r): Remove from S the element at rank r; an error condition occurs if r < 0 or r > n - 1, where n is the current number of elements. Input: Integer; Output: Object It also supports the usual methods size() and isEmpty(). Realization of a Deque with a Vector A deque can be easily realized with a vector. The table below shows the mapping of the methods. Deque Method Vector Method size() size() isEmpty() isEmpty() first() elemAtRank(0) last() elemAtRank(size() - 1) insertFirst(e) insertAtRank(0, e) insertLast(e) insertAtRank(size(),e) removeFirst() removeAtRank(0) removeLast() removeAtRank(size() - 1) A Simple Array-Based Implementation Vector ADT rank Array index A Simple Array-Based Implementation Let N be the size of array A and n be the number of elements in the vector. Algorithms: insertAtRank(r,e): for i = n – 1 to r do assign element A[ i ] to A[ i + 1 ] assign e to A[ r ] increment the size n The loop in the algorithm for insertAtRank() is to make room for the new element: S 0 1 r n-1 N-1 removeAtRank(r): save element A[ r ] to a variable e for i = r to n – 1 do assign A[ i ] to A[ i - 1 ] decrement the size n return e The loop is to remove the element: S 0 1 r n-1 N-1 The Running Times of Vector Methods insertAtRank(r,e) and removeAtRank(r): Running time is proportional to n, the size of the vector. running time = O(n). Java Implementation public interface Vector { public int size(); public boolean isEmpty(); public Object elemAtRank(int r); public Object replaceAtRank(int r, Object e); public void insertAtRank(int r, Object e); public Object removeAtRank(int r); } class InvalidRankException extends Exception { public InvalidPositionException() {super();} public InvalidPositionException(String s) { super(s);} } class EmptyContainerExeption extends Exception { public EmptyContainerExeption() {super();} public EmptyContainerExeption(String s) { super(s);} } class BoundaryViolationExeption extends Exception { public BoundaryViolationExeption() {super();} public BoundaryViolationExeption(String s) { super(s);} } Java Implementation public class ArrayVector implements Vector { private Object[] A; // array storing the elements of the vector private int capacity = 16; // initial length of array A private int size = 0; // number of elements stored in the vector /** Creates the vector with initial capacity 16. */ public ArrayVector() { A = new Object[capacity]; } public Object elemAtRank(int r) throws BoundaryViolationException { checkRank(r, size()); return a[r]; } public int size() {return size;} public boolean isEmpty {return size()==0;} /** Inserts an element at the given rank. */ public void insertAtRank(int r, Object e) throws BoundaryViolationException { checkRank(r, size() + 1); if (size == capacity) { // an overflow capacity *= 2; Object[] B = new Object[capacity]; for (int i=0; i<size; i++) B[i] = A[i]; A = B;} for (int i=size-1; i>=r; i--) // shift elements up A[i+1] = A[i]; A[r] = e; size++; } /** Removes the element stored at the given rank. */ public Object removeAtRank(int r) throws BoundaryViolationException { checkRank(r, size()); Object temp = A[r]; for (int i=r; i<size-1; i++) // shift elements down A[i] = A[i+1]; size--; return temp; } public Object replaceAtRank(int r, Object e) throws BoundaryViolationException { checkRank(r, size()); Object temp = A[r]; A[r] = e; return temp; } public void checkRank(int r, int s) throws BoundaryViolationException { if (0 > r) || (r > s) throw new BoundaryViolationException; } } Data Structure Exercises 7.2