0% found this document useful (0 votes)
288 views

Sequences in Data Structure

Sequence type is the union of two other Lisp types: lists and arrays. In other words, any list is a sequence, and any array is a sequence. The common property that all sequences have is that each is an ordered collection of elements. An array is a single primitive object that has a slot for each of its elements. All the elements are accessible in constant time, but the length of an existing array cannot be changed. Strings, vectors, char-tables and bool-vectors are the four types of arrays. A

Uploaded by

Lei Lei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
288 views

Sequences in Data Structure

Sequence type is the union of two other Lisp types: lists and arrays. In other words, any list is a sequence, and any array is a sequence. The common property that all sequences have is that each is an ordered collection of elements. An array is a single primitive object that has a slot for each of its elements. All the elements are accessible in constant time, but the length of an existing array cannot be changed. Strings, vectors, char-tables and bool-vectors are the four types of arrays. A

Uploaded by

Lei Lei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

SEQUENCES

IT 21 – Data Structures and Algorithms


Mrs. Sheila E. Dimasuhid
Sequences

 represents a collection of linearly


arranged elements and provides
methods for accessing, inserting, and
removing arbitrary elements.

IT 21 – Data Structures and Algorithms 2


Sequences

 Vectors (Ranks)
 Lists (Positions)
 General Sequences (combination)

IT 21 – Data Structures and Algorithms 3


Sequences

 Vector ADT
– sequence S (with n elements) that
supports the following methods:
• elemAtRank(r)
• replaceAtRank(r, e)
• insertAtRank(r,e)
• removeAtRank(r)
• size()
• isEmpty()

IT 21 – Data Structures and Algorithms 4


Sequences

 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

IT 21 – Data Structures and Algorithms 5


Sequences

 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

IT 21 – Data Structures and Algorithms 6


Sequences

 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

IT 21 – Data Structures and Algorithms 7


Sequences

 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

IT 21 – Data Structures and Algorithms 8


Sequences

 Vector Methods
– size()
Returns the size of the sequence
Input: None
Output: Integer

IT 21 – Data Structures and Algorithms 9


Sequences

 Vector Methods
– isEmpty()
Returns True if sequence is empty, False if
otherwise
Input: None
Output: Boolean

IT 21 – Data Structures and Algorithms 10


Sequence
Vector Operation Output VS
Insert 7 at rank 0 - (7)
Insert 4 at rank 0 - (4,7)

Return the element at 7 (4,7)


rank 1
Insert 2 at rank 2 - (4,7,2)
Return the element at “error” (4,7,2)
rank 3
Remove the element at 7 (4,2)
rank 1
Insert 5 at rank 1 - (4,5,2)

IT 21 – Data Structures and Algorithms 11


Sequence

 Array-based Implementation of the


Vector ADT
S G
0 1 2 3 4 ….. N-4 N-1

Object G has a rank 4 (which is equal to the


number of objects preceding it).
It is also called the 4th object.

IT 21 – Data Structures and Algorithms 12


Sequences

 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

IT 21 – Data Structures and Algorithms 14


Sequences

 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

IT 21 – Data Structures and Algorithms 17


Sequences

 Vector ADT
Algorithm elemAtRank(r):
if r<0 or r>size-1 then
throw Error
else
return S[r]

IT 21 – Data Structures and Algorithms 18


Sequences

 Vector ADT Time Complexity


Method Time
insertAtRank(r,e) O(n)
removeAtRank(r) O(n)
replaceAtRank(r,e) O(1)
elemAtRank(r) O(1)
size() O(1)
isEmpty() O(1)

IT 21 – Data Structures and Algorithms 19


Sequences

 Doubly Linked List Implementation of


the Vector ADT
Header Trailer

CS 10 CS 11 CS 20

IT 21 – Data Structures and Algorithms 20


Sequences

 Vector Node Insertion


Header Trailer

CS 10 CS 20 CS 22

CS 11
IT 21 – Data Structures and Algorithms 21
Sequences

 Vector Node Deletion

Header Trailer

CS 10 CS 11 CS 20

IT 21 – Data Structures and Algorithms 22


Sequences

 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

IT 21 – Data Structures and Algorithms 23


Sequences

 List ADT Methods


– generic methods
• size(), isEmpty()
– query methods
• isFirst(p), isLast(p)
– accessor methods
• first(), last()
• before(p), after(p)

IT 21 – Data Structures and Algorithms 24


Sequences

 List ADT Methods


– update methods
• swapElement(p,q), replaceElement(p,q)
• insertFirst(e), insertLast(e)
• insertBefore(p,e), insertAfter(p,e)
• remove(p)

IT 21 – Data Structures and Algorithms 25


Sequences

 List ADT Methods


– first()
Returns the position of the first element of S; an
error occurs if S is empty
Input: None
Output: Position

IT 21 – Data Structures and Algorithms 26


Sequences

 List ADT Methods


– last()
Returns the position of the last element of S; an
error occurs if S is empty
Input: None
Output: Position

IT 21 – Data Structures and Algorithms 27


Sequences

 List ADT Methods


– isFirst()
Returns a boolean value indicating whether the
given position is the first one in the list
Input: Position p
Output: Boolean

IT 21 – Data Structures and Algorithms 28


Sequences

 List ADT Methods


– isLast()
Returns a boolean value indicating whether the
given position is the last one in the list
Input: Position p
Output: Boolean

IT 21 – Data Structures and Algorithms 29


Sequences

 List ADT Methods


– before(p)
Returns the position of the element of S
preceding the one at position p; an error occurs
if p is the first position
Input: Position p
Output: Position

IT 21 – Data Structures and Algorithms 30


Sequences

 List ADT Methods


– after(p)
Returns the position of the element of S
following the one at position p; an error occurs if
p is the last position
Input: Position p
Output: Position

IT 21 – Data Structures and Algorithms 31


Sequences

 List ADT Methods


– replaceElement(p,e)
Replaces the element at position p with e,
returning the element formerly at position p
Input: Position p
Output: Object

IT 21 – Data Structures and Algorithms 32


Sequences

 List ADT Methods


– swapElements(p,q)
Swaps the elements stored at positions p and q,
so that the element that is at position p moves to
position q and the element that is at position q
moves to position q
Input: Positions q and q
Output: None

IT 21 – Data Structures and Algorithms 33


Sequences

 List ADT Methods


– insertFirst(e)
Inserts a new element e into S as the first
element
Input: Object e
Output: Position of the newly inserted
element e

IT 21 – Data Structures and Algorithms 34


Sequences

 List ADT Methods


– insertLast(e)
Inserts a new element e into S as the last
element
Input: Object e
Output: Position of the newly inserted
element e

IT 21 – Data Structures and Algorithms 35


Sequences

 List ADT Methods


– insertBefore(p, e)
Inserts a new element e into S before position p
in S; an error occurs if p is the first position
Input: Position p and Object e
Output: Position of the newly inserted
element e

IT 21 – Data Structures and Algorithms 36


Sequences

 List ADT Methods


– insertAfter(p, e)
Inserts a new element e into S after position p in
S; an error occurs if p is the last position
Input: Position p and Object e
Output: Position of the newly inserted
element e

IT 21 – Data Structures and Algorithms 37


Sequences

 List ADT Methods


– remove(p)
Removes from S the element at position p and
returns it; an error occurs if size=0
Input: Position p
Output: Object

IT 21 – Data Structures and Algorithms 38


Sequences

 List ADT Methods


– Reasons for a position p to be invalid:
• p = null O(n)
• p was previously deleted from the list O(1)
• p is a position of a different list O(n)

IT 21 – Data Structures and Algorithms 39


Sequences
List Operation Output L
insertFirst(8) p1(8) (8)

insertAfter(p1, 5) p2(5) (8,5)

insertBefore(p2, 3) p3(3) (8,3,5)

insertFirst(9) p4(9) (9,8,3,5)

before(p3) p1(8) (9,8,3,5)

last() p2(5) (9,8,3,5)

remove(p4) 9 (8,3,5)

IT 21 – Data Structures and Algorithms 40


Sequences
List Operation Output L
(8,3,5)

swapElements(p1,p2) - (5,3,8)

replaceElement(p3,7) 3 (5,7,8)

insertAfter(first(), 2) p5(2) (5,2,7,8)

IT 21 – Data Structures and Algorithms 41


Sequences

 General Sequence ADT


– combines the Vector and List ADT
– adds methods that bridge between ranks
and positions
• atRank(r) returns a position
• rankOf(p) returns integer rank
– an array-based implementation needs to
use objects to represent the positions

IT 21 – Data Structures and Algorithms 42


Sequences

 General Sequence ADT


CS 10 CS 11 CS 20

0 1 2

0 1 2 3 4 … … N -1

IT 21 – Data Structures and Algorithms 43


Sequences

 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)

IT 21 – Data Structures and Algorithms 44


Sequences

 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)

IT 21 – Data Structures and Algorithms 45

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy