vectors
play

Vectors 9/14/2007 2:55 PM Outline and Reading The Vector ADT - PDF document

1 Vectors Vectors 9/14/2007 2:55 PM Outline and Reading The Vector ADT (5.1.1) Array-based implementation (5.1.2) 9/14/2007 2:55 PM Vectors 2 The Vector ADT The Vector ADT stores Main vector operations: objects to which it


  1. 1 Vectors Vectors 9/14/2007 2:55 PM

  2. Outline and Reading The Vector ADT (§5.1.1) Array-based implementation (§5.1.2) 9/14/2007 2:55 PM Vectors 2

  3. The Vector ADT The Vector ADT stores Main vector operations: objects to which it � elemAtRank(int r): returns the provides direct access element at rank r without removing it An element can be � replaceAtRank(int r, Object o): accessed, inserted or replace the element at rank r with removed by specifying o its rank (number of � insertAtRank(int r, Object o): elements preceding it) insert a new element o to have An exception is rank r thrown if an incorrect � removeAtRank(int r): removes the rank is specified (e.g., element at rank r a negative rank) Additional operations size() and isEmpty() 9/14/2007 2:55 PM Vectors 3

  4. Applications of Vectors Direct applications � Sorted collection of objects (elementary database) Indirect applications � Auxiliary data structure for algorithms � Component of other data structures 9/14/2007 2:55 PM Vectors 4

  5. 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 elemAtRank ( r ) is implemented in O (1) time by returning V [ r ] V n 0 1 2 r 9/14/2007 2:55 PM Vectors 5

  6. Insertion In operation insertAtRank ( 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 n 0 1 2 r V n 0 1 2 r V o n 0 1 2 r 9/14/2007 2:55 PM Vectors 6

  7. Deletion In operation removeAtRank ( 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 o n 0 1 2 r V n 0 1 2 r V n 0 1 2 r 9/14/2007 2:55 PM Vectors 7

  8. Performance In the array based implementation of a Vector � The space used by the data structure is O ( n ) � size , isEmpty , elemAtRank and replaceAtRank run in O (1) time � insertAtRank and removeAtRank run in O ( n ) time In an insertAtRank operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one 9/14/2007 2:55 PM Vectors 8

  9. 9 Lists and Sequences Vectors 9/14/2007 2:55 PM

  10. Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly linked list (§ 5.2.3) Sequence ADT (§5.3.1) Implementations of the sequence ADT (§5.3.3) 9/14/2007 2:55 PM Vectors 10

  11. Singly Linked List A singly linked list is a next concrete data structure consisting of a sequence of nodes Each node stores node � element elem � link to the next node ∅ A B C D 9/14/2007 2:55 PM Vectors 11

  12. Stack with a Singly Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O ( n ) and each operation of the Stack ADT takes O (1) time nodes ∅ t elements 9/14/2007 2:55 PM Vectors 12

  13. Queue with a Singly Linked List We can implement a queue with a singly linked list � The front element is stored at the first node � The rear element is stored at the last node The space used is O ( n ) and each operation of the Queue ADT takes O (1) time r nodes f ∅ elements 9/14/2007 2:55 PM Vectors 13

  14. 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 isNull(): returns true if this is a null position 9/14/2007 2:55 PM Vectors 14

  15. List ADT The List ADT models a Accessor methods: sequence of positions � first(), last() storing arbitrary objects � before(p), after(p) It establishes a Update methods: before/after relation � replaceElement(p, o), swapElements(p, q) between positions � insertBefore(p, o), Generic methods: insertAfter(p, o), � size(), isEmpty() � insertFirst(o), Query methods: insertLast(o) � isFirst(p), isLast(p) � remove(p) 9/14/2007 2:55 PM Vectors 15

  16. Doubly Linked List A doubly linked list provides a natural prev next implementation of the List ADT Nodes implement Position and store: element � link to the previous node � elem node link to the next node � Special trailer and header nodes trailer nodes/positions header elements 9/14/2007 2:55 PM Vectors 16

  17. Insertion We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C 9/14/2007 2:55 PM Vectors 17

  18. Deletion We visualize remove(p), where p = last() p A B C D A B C p D A B C 9/14/2007 2:55 PM Vectors 18

  19. 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 9/14/2007 2:55 PM Vectors 19

  20. Sequence ADT The Sequence ADT is the List-based methods: union of the Vector and � first(), last(), List ADTs before(p), after(p), replaceElement(p, o), Elements accessed by swapElements(p, q), � Rank, or insertBefore(p, o), � Position insertAfter(p, o), Generic methods: insertFirst(o), � size(), isEmpty() insertLast(o), Vector-based methods: remove(p) � elemAtRank(r), Bridge methods: replaceAtRank(r, o), � atRank(r), rankOf(p) insertAtRank(r, o), removeAtRank(r) 9/14/2007 2:55 PM Vectors 20

  21. 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 9/14/2007 2:55 PM Vectors 21

  22. Array-based Implementation elements We use a circular array storing positions A position object stores: � Element 0 1 2 3 � Rank positions Indices f and l keep track of first and last S positions f l 9/14/2007 2:55 PM Vectors 22

  23. Sequence Implementations Operation Array List size, isEmpty 1 1 n atRank, rankOf, elemAtRank 1 first, last, before, after 1 1 replaceElement, swapElements 1 1 n replaceAtRank 1 n n insertAtRank, removeAtRank insertFirst, insertLast 1 1 insertAfter, insertBefore n 1 remove n 1 9/14/2007 2:55 PM Vectors 23

Recommend


More recommend