Vectors Lists and Sequences Vectors Outline and Reading

  • Slides: 28
Download presentation
Vectors, Lists, and Sequences

Vectors, Lists, and Sequences

Vectors: Outline and Reading • The Vector ADT (§ 6. 1. 1) • Array-based

Vectors: Outline and Reading • The Vector ADT (§ 6. 1. 1) • Array-based implementation (§ 6. 1. 2)

The Vector ADT • The Vector ADT extends • Main vector operations: the notion

The Vector ADT • The Vector ADT extends • Main vector operations: the notion of array by – elem. At. Rank(int r): returns the element at rank r without removing it storing a sequence of arbitrary objects – replace. At. Rank(int r, Object o): replace the element at rank r with o • An element can be – insert. At. Rank(int r, Object o): insert a accessed, inserted or new element o to have rank r removed by specifying – remove. At. Rank(int r): removes the its rank (number of element at rank r elements preceding it) • An exception is thrown • Additional operations size() and is. Empty() if an incorrect rank is specified (e. g. , a negative rank)

Applications of Vectors • Direct applications – Sorted collection of objects (simple database) •

Applications of Vectors • Direct applications – Sorted collection of objects (simple database) • Indirect applications – Auxiliary data structure for algorithms – Component of other data structures

Array-based Vector • Use an array V of size N • A variable n

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 elem. At. Rank(r) is implemented in O(1) time by returning V[r] N-1 0 V 0 1 2 r n

Array based Vector: Insertion • In operation insert. At. Rank(r, o) we need to

Array based Vector: Insertion • In operation insert. At. Rank(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 0 1 2 r n 0 1 2 o r V V n

Deletion • In operation remove. At. Rank(r) we need to fill the hole left

Deletion • In operation remove. At. Rank(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 0 1 2 o r n 0 1 2 r V V n

Performance • In the array based implementation of a Vector – The space used

Performance • In the array based implementation of a Vector – The space used by the data structure is O(n) – Size(), is. Empty(), elem. At. Rank(r) and replace. At. Rank(r, o) run in O(1) time – insert. At. Rank(r, o) and remove. At. Rank(r) run in O(n) time • If we use the array in a circular fashion, insert. At. Rank(0, o) and remove. At. Rank(0) run in O(1) time • In an insert. At. Rank(r, o) operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one

Exercise: • Implement the Deque ADT using Vector functions – Deque functions: • first(),

Exercise: • Implement the Deque ADT using Vector functions – Deque functions: • first(), last(), insert. First(e), insert. Last(e), remove. First(), remove. Last(), size(), is. Empty() – Vector functions: • elem. At. Rank( r), replace. At. Rank(r, e), insert. At. Rank(r, e), remove. At. Rank(r ), size(), is. Empty()

Exercise Solution: • Implement the Deque ADT using Vector functions – – Deque functions:

Exercise Solution: • Implement the Deque ADT using Vector functions – – Deque functions: first(), last(), insert. First(e), insert. Last(e), remove. First(), remove. Last(), size(), is. Empty() Vector functions: elem. At. Rank( r), replace. At. Rank(r, e), insert. At. Rank(r, e), remove. At. Rank(r ), size(), is. Empty() – Deque function : Realization using Vector Functions – size() and is. Empty() fcns can simply call Vector fcns directly – first() => elem. At. Rank(0) – last() => elem. At. Rank(size()-1) – insert. First(e) => insert. At. Rank(0, e) – insert. Last(e) => insert. At. Rank(size(), e) – remove. First() => remove. At. Rank(0) – remove. Last() => remove. At. Rank(size()-1)

STL vector class • Functions in the STL vector class (incomplete) – – –

STL vector class • Functions in the STL vector class (incomplete) – – – – Size(), capacity() - return #elts in vector, #elts vector can hold empty() - boolean Operator[r] - returns reference to elt at rank r (no index check) At( r) - returns reference to elt at rank r (index checked) Front(), back() - return references to first/last elts push_back(e) - insert e at end of vector pop_back() - remove last elt vector(n) - creates a vector of size n • Similarities & Differences with book’s Vector ADT – STL assignment v[r]=e is equivalent to v. replace. At. Rank(r, e) – No direct STL counterparts of insert. At. Rank( r) & remove. At. Rank( r) – STL also provides more general fcns for inserting & removing from arbitrary positions in the vector - these use iterators

Iterators • An iterator abstracts the process • An iterator is typically of scanning

Iterators • An iterator abstracts the process • An iterator is typically of scanning through a collection associated with an another data of elements structure • Methods of the Object. Iterator • We can augment the Stack, ADT: Queue, Vector, and other container ADTs with method: – boolean has. Next() – object next() – reset() • Extends the concept of position by adding a traversal capability – Object. Iterator elements() • Two notions of iterator: – snapshot: freezes the contents of the data structure at a given time – dynamic: follows changes to the data structure

Iterators • Some functions supported by STL containers – begin(), end() - return iterators

Iterators • Some functions supported by STL containers – begin(), end() - return iterators to beginning or end of container – insert(I, e) - insert e just before the position indicated by iterator I (analogous to our insert. Before(p)) – erase(I) - removes the element at the position indicated by I (analogous to our remove(p)) • The functions can be used to insert/remove elements from arbitrary positions in the STL vector and list

Vector Summary • Vector Operation Complexity for Different Implementations Array Fixed-Size or Expandable List

Vector Summary • Vector Operation Complexity for Different Implementations Array Fixed-Size or Expandable List Singly or Doubly Linked Remove. At. Rank(r), Insert. At. Rank(r, o) O(1) Best Case (r=0, n) O(n) Worst Case O(n) Average Case ? elem. At. Rank(r), Replace. At. Rank(r, o) O(1) ? Size(), is. Empty() O(1) ?

Lists and Sequences

Lists and Sequences

Outline and Reading • • • Singly linked list Position ADT (§ 6. 2.

Outline and Reading • • • Singly linked list Position ADT (§ 6. 2. 1) List ADT (§ 6. 2. 2) Sequence ADT (§ 6. 3. 1) Implementations of the sequence ADT (§ 6. 3. 2 -3) Iterators (§ 6. 2. 5)

Position ADT • The Position ADT models the notion of place within a data

Position ADT • The Position ADT models the notion of place within a data structure where a single object is stored • A special null position refers to no object. • Positions provide a unified view of diverse ways of storing data, such as – a cell of an array – a node of a linked list • Member functions: – Object& element(): returns the element stored at this position – bool is. Null(): returns true if this is a null position

List ADT (§ 6. 2. 2) • The List ADT models a sequence of

List ADT (§ 6. 2. 2) • The List ADT models a sequence of positions storing arbitrary objects – establishes a before/after relation between positions • It allows for insertion and removal in the “middle” • Query methods: – is. First(p), is. Last(p) • Generic methods: – size(), is. Empty() • Accessor methods: – first(), last() – before(p), after(p) • Update methods: – replace. Element(p, o), swap. Elements(p, q) – insert. Before(p, o), insert. After(p, o), – insert. First(o), insert. Last(o) – remove(p)

List ADT • Query methods: – is. First(p), is. Last(p) : • return boolean

List ADT • Query methods: – is. First(p), is. Last(p) : • return boolean indicating if the given position is the first or last, resp. • Accessor methods – first(), last(): • return the position of the first or last, resp. , element of S • an error occurs if S is empty – before(p), after(p): • return the position of the element of S preceding or following, resp, the one at position p • an error occurs if S is empty, or p is the first or last, resp. , position

List ADT • Update Methods – replace. Element(p, o) • Replace the element at

List ADT • Update Methods – replace. Element(p, o) • Replace the element at position p with o – swap. Elements(p, q) • Swap the elements stored at positions p & q – insert. Before(p, o), insert. After(p, o), • Insert a new element o into S before or after, resp. , position p • Output: position of the newly inserted element – insert. First(o), insert. Last(o) • Insert a new element o into S as the first or last, resp. , element • Output: position of the newly inserted element – remove(p) • Remove the element at position p from S

Exercise: • Describe how to implement the following list ADT operations using a singly-linked

Exercise: • Describe how to implement the following list ADT operations using a singly-linked list – list ADT operations: first(), last(), before(p), after(p) – For each operation, explain how it is implemented and provide the running time next • A singly linked list consists of a sequence of nodes • Each node stores • element • link to the next node elem node head tail Leonard Sheldon Howard Raj

Exercise: • Describe how to implement the following list ADT operations using a doubly-linked

Exercise: • Describe how to implement the following list ADT operations using a doubly-linked list – list ADT operations: first(), last(), before(p), after(p) – For each operation, explain how it is implemented and provide the running time • Doubly-Linked List Nodes implement Position and store: next prev • element • link to previous node • link to next node elem • Special head/tail nodes node tail head Leonard Sheldon Howard Raj

Performance • In the implementation of the List ADT by means of a doubly

Performance • In the implementation of the List ADT by means of a doubly linked list – The space used by a list with n elements is O(n) – The space used by each position of the list is O(1) – All the operations of the List ADT run in O(1) time – Operation element() of the Position ADT runs in O(1) time

STL list class • Functions in the STL list class – – – size()

STL list class • Functions in the STL list class – – – size() - return #elements in list, empty() - boolean front(), back() - return references to first/last elements push_front(e), push_back(e) - insert e at front/end pop_front(), pop_back() - remove first/last element List() - creates an empty list • Similarities & Differences with book’s List ADT – STL front() & back() correspond to first() & last() except the STL functions return the element & not its position – STL push() & pop() are equiv to List ADT insert and remove when applied to the beginning & end of the list – STL also provides functions for inserting & removing from arbitrary positions in the list - these use iterators

List Summary • List Operation Complexity for different implementations List Singly-Linked List Doubly- Linked

List Summary • List Operation Complexity for different implementations List Singly-Linked List Doubly- Linked first(), last(), after(p) insert. After(p, o), replace. Element(p, o), swap. Elements(p, q) O(1) before(p), insert. Before(p, o), remove(p) O(n) O(1) Size(), is. Empty() O(1)

Sequence ADT • The Sequence ADT is the union of the Vector and List

Sequence ADT • The Sequence ADT is the union of the Vector and List ADTs • Elements accessed by – Rank, or – Position • Generic methods: – size(), is. Empty() • Vector-based methods: – elem. At. Rank(r), replace. At. Rank(r, o), insert. At. Rank(r, o), remove. At. Rank(r) • List-based methods: – first(), last(), before(p), after(p), replace. Element(p, o), swap. Elements(p, q), insert. Before(p, o), insert. After(p, o), insert. First(o), insert. Last(o), remove(p) • Bridge methods: – at. Rank(r), rank. Of(p)

Applications of Sequences • The Sequence ADT is a basic, general-purpose, data structure for

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 – small database (e. g. , address book) • Indirect applications: – Building block of more complex data structures

Sequence Implementations Operation size, is. Empty at. Rank, rank. Of, elem. At. Rank first,

Sequence Implementations Operation size, is. Empty at. Rank, rank. Of, elem. At. Rank first, last, before, after replace. Element, swap. Elements replace. At. Rank insert. At. Rank, remove. At. Rank insert. First, insert. Last insert. After, insert. Before remove Array 1 1 1 List 1 n 1 1 1 n n 1 1 1