Lists 2010 Goodrich Tamassia Lists 1 Position ADT

  • Slides: 19
Download presentation
Lists © 2010 Goodrich, Tamassia Lists 1

Lists © 2010 Goodrich, Tamassia Lists 1

Position ADT q q The Position ADT models the notion of place within a

Position ADT q q The Position ADT models the notion of place within a data structure where a single object is stored It gives a unified view of diverse ways of storing data, such as n n q a cell of an array a node of a linked list Just one method: n n object p. element(): returns the element at position In C++ it is convenient to implement this as *p © 2010 Goodrich, Tamassia Lists 2

The Array List ADT q q q The Array List ADT extends the notion

The Array List ADT q q q The Array List ADT extends the notion of array by storing a sequence of arbitrary objects q An element can be accessed, inserted or removed by specifying its index (number of elements preceding it) An exception is thrown if an incorrect index is given (e. g. , a negative index) © 2010 Goodrich, Tamassia q Main methods: n get(integer i): returns the element at index i without removing it n set(integer i, object o): replace the element at index i with o and return the old element n add(integer i, object o): insert a new element o to have index i n remove(integer i): removes and returns the element at index i Additional methods: n size() n is. Empty() Array Lists 3

Applications of Array Lists q Direct applications n q Sorted collection of objects (elementary

Applications of Array Lists q Direct applications n q Sorted collection of objects (elementary database) Indirect applications n n Auxiliary data structure for algorithms Component of other data structures © 2010 Goodrich, Tamassia Array Lists 4

Array-based Implementation q q Use an array A of size N A variable n

Array-based Implementation q q Use an array A of size N A variable n keeps track of the size of the array list (number of elements stored) Operation get(i) is implemented in O(1) time by returning A[i] Operation set(i, o) is implemented in O(1) time by performing t = A[i], A[i] = o, and returning t. A 0 1 2 © 2010 Goodrich, Tamassia n i Array Lists 5

Insertion q q In operation add(i, o), we need to make room for the

Insertion q q In operation add(i, o), we need to make room for the new element by shifting forward the n i elements A[i], …, A[n 1] In the worst case (i = 0), this takes O(n) time A 0 1 2 i n 0 1 2 o i A A © 2010 Goodrich, Tamassia Array Lists n 6

Element Removal q q In operation remove(i), we need to fill the hole left

Element Removal q q In operation remove(i), we need to fill the hole left by the removed element by shifting backward the n i 1 elements A[i + 1], …, A[n 1] In the worst case (i = 0), this takes O(n) time A 0 1 2 o i n 0 1 2 i A A © 2010 Goodrich, Tamassia Array Lists n 7

Performance q In the array based implementation of an array list: n n n

Performance q In the array based implementation of an array list: n n n q q The space used by the data structure is O(n) size, is. Empty, get and set run in O(1) time add and remove run in O(n) time in worst case If we use the array in a circular fashion, operations add(0, x) and remove(0, x) run in O(1) time In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one © 2010 Goodrich, Tamassia Array Lists 8

Growable Array-based Array List q q q In an add(o) operation (without an index),

Growable Array-based Array List q q q In an add(o) operation (without an index), we always add at the end When the array is full, we replace the array with a larger one How large should the new array be? n n Algorithm add(o) if t = S. length 1 then A new array of size … for i 0 to n-1 do A[i] S[i] S A n n+1 S[n-1] o Incremental strategy: increase the size by a constant c Doubling strategy: double the size © 2010 Goodrich, Tamassia Array Lists 9

Comparison of the Strategies q q q We compare the incremental strategy and the

Comparison of the Strategies q q q We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n add(o) operations We assume that we start with an empty stack represented by an array of size 1 We call amortized time of an add operation the average time taken by an add over the series of operations, i. e. , T(n)/n © 2010 Goodrich, Tamassia Array Lists 10

Incremental Strategy Analysis q q We replace the array k = n/c times The

Incremental Strategy Analysis q q We replace the array k = n/c times The total time T(n) of a series of n add operations is proportional to n + c + 2 c + 3 c + 4 c + … + kc = n + c(1 + 2 + 3 + … + k) = n + ck(k + 1)/2 Since c is a constant, T(n) is O(n + k 2), i. e. , O(n 2) The amortized time of an add operation is O(n) © 2010 Goodrich, Tamassia Array Lists 11

Doubling Strategy Analysis q q We replace the array k = log 2 n

Doubling Strategy Analysis q q We replace the array k = log 2 n times geometric series The total time T(n) of a series of n add operations is proportional to 2 4 n + 1 + 2 + 4 + 8 + …+ 2 k = 1 1 k + 1 n+2 1 = 3 n 1 8 T(n) is O(n) The amortized time of an add operation is O(1) © 2010 Goodrich, Tamassia Array Lists 12

Node List ADT q q q The Node List ADT models a sequence of

Node List ADT q q q The Node List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: n q n q Lists begin(), end() Update methods: n size(), empty() © 2010 Goodrich, Tamassia Iterators: insert. Front(e), insert. Back(e) remove. Front(), remove. Back() Iterator-based update: n insert(p, e) n remove(p) 13

Doubly Linked List q q A doubly linked list provides a natural implementation of

Doubly Linked List q q A doubly linked list provides a natural implementation of the Node List ADT Nodes implement Position and store: n n n q element link to the previous node link to the next node prev next elem node Special trailer and header nodes/positions header trailer elements © 2010 Goodrich, Tamassia Lists 14

Insertion q We visualize operation insert(p, x), which inserts x before p a a

Insertion q We visualize operation insert(p, x), which inserts x before p a a p c b x a © 2010 Goodrich, Tamassia q x b Lists q p c 15

Insertion Algorithm insert(p, e): {insert e before p} Create a new node v v

Insertion Algorithm insert(p, e): {insert e before p} Create a new node v v element = e u = p prev v next = p; p prev = v {link in v before p} v prev = u; u next = v {link in v after u} © 2010 Goodrich, Tamassia Lists 16

Deletion q We visualize remove(p) a b c p d a © 2010 Goodrich,

Deletion q We visualize remove(p) a b c p d a © 2010 Goodrich, Tamassia b c Lists 17

Deletion Algorithm remove(p): u = p prev w = p next u next =

Deletion Algorithm remove(p): u = p prev w = p next u next = w {linking out p} w prev = u © 2010 Goodrich, Tamassia Lists 18

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

Performance q In the implementation of the List ADT by means of a doubly linked list n n 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 © 2010 Goodrich, Tamassia Lists 19