Sequences in Data Structure
Sequences in Data Structure
Vectors (Ranks)
Lists (Positions)
General Sequences (combination)
Vector ADT
– sequence S (with n elements) that
supports the following methods:
• elemAtRank(r)
• replaceAtRank(r, e)
• insertAtRank(r,e)
• removeAtRank(r)
• size()
• isEmpty()
Vector Methods
– elemAtRank(r)
Returns the element of S with rank r; an error
condition occurs if r < 0 or r > size-1, where
size is the current number of elements
Input: Integer
Output: Object
Vector Methods
– replaceAtRank(r,e)
Replaces with e the element at rank r and return
it; an error condition occurs if r < 0 or r > size-1
Input: Integer and Object
Output: Object
Vector Methods
– insertAtRank(r,e)
Inserts a new element e into S to have rank r;
an error condition occurs if r < 0 or r > size
Input: Integer and Object
Output: None
Vector Methods
– removeAtRank(r)
Removes from S the element at rank r; an error
condition occurs if r < 0 or r > size-1
Input: Integer
Output: Object
Vector Methods
– size()
Returns the size of the sequence
Input: None
Output: Integer
Vector Methods
– isEmpty()
Returns True if sequence is empty, False if
otherwise
Input: None
Output: Boolean
Vector ADT
Operation: Insert “P” at Rank 3
S C O M U T E R
0 1 2 3 4 5 6 ….. N-4 N-1
S C O M U T E R
0 1 2 3 4 5 6 ….. N-4 N-1
S C O M P U T E R
0 1 2 3 4 5 6 7 ….. N-4 N-1
IT 21 – Data Structures and Algorithms 13
Sequences
Vector ADT
Algorithm insertAtRank(r,e):
if r<0 or r>size-1 then
throw Error
else
for x ← size-1 down to r
S[x+1] ← S[x]
S[r] ← e
size ← size + 1
Vector ADT
Operation: Delete Element at Rank 3
S C O M P U T E R
0 1 2 3 4 5 6 7 ….. N-4 N-1
S C O M U T E R
0 1 2 3 4 5 6 ….. N-4 N-1
S C O M U T E R
0 1 2 3 4 5 6 7 ….. N-4 N-1
IT 21 – Data Structures and Algorithms 15
Sequences
Vector ADT
Algorithm removeAtRank(r):
if r<0 or r>size-1 then
throw Error
else
j ← S[r]
for x ← r to size-2
S[x] ← S[x+1]
size ← size - 1
return j
IT 21 – Data Structures and Algorithms 16
Sequences
Vector ADT
Algorithm replaceAtRank(r,e):
if r<0 or r>size-1 then
throw Error
else
temp ← S[r]
S[r] ← e
return temp
Vector ADT
Algorithm elemAtRank(r):
if r<0 or r>size-1 then
throw Error
else
return S[r]
CS 10 CS 11 CS 20
CS 10 CS 20 CS 22
CS 11
IT 21 – Data Structures and Algorithms 21
Sequences
Header Trailer
CS 10 CS 11 CS 20
List ADT
– sequence S (with n elements) with
position-based methods
• Positions have only one method: element():
Return the element at this position
• Positions are defined relatively to other
positions (before/after relation)
• Positions are not tied to an element or rank
remove(p4) 9 (8,3,5)
swapElements(p1,p2) - (5,3,8)
replaceElement(p3,7) 3 (5,7,8)
0 1 2
0 1 2 3 4 … … N -1
Comparison of Sequence
Implementations
Operations Array List
size, isEmpty O(1) O(1)
atRank, rankOf, elemAtRank O(1) O(n)
first, last O(1) O(1)
before, after O(1) O(1)
replaceElement, swapElements O(1) O(1)
Comparison of Sequence
Implementations
Operations Array List
replaceAtRank O(1) O(n)
insertAtRank, removeAtRank O(n) O(n)
insertFirst, insertLast O(1) O(1)
insertAfter, insertBefore O(n) O(1)
remove O(n) O(1)