0% found this document useful (0 votes)
79 views42 pages

04 Lists PDF

The document discusses data structures and algorithms. It covers basic data structures like arrays and linked lists. It also discusses abstract data types (ADTs) like stacks, queues, deques and sequences. The document explains array lists, positional lists and sequence ADTs. It describes how array lists can be implemented using arrays and linked lists, and the time complexities of different operations on each implementation. Positional lists are also introduced with linked lists as their natural implementation.

Uploaded by

Ray Jia
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)
79 views42 pages

04 Lists PDF

The document discusses data structures and algorithms. It covers basic data structures like arrays and linked lists. It also discusses abstract data types (ADTs) like stacks, queues, deques and sequences. The document explains array lists, positional lists and sequence ADTs. It describes how array lists can be implemented using arrays and linked lists, and the time complexities of different operations on each implementation. Positional lists are also introduced with linked lists as their natural implementation.

Uploaded by

Ray Jia
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/ 42

CSI2110

ata Structures and Algorithms

Prof. WonSook Lee

1
We learned…

2
Review:
Basic Data Structures (“concrete” data structures)

Array

Linked Lists
For example:
l

header trailer

3
Abstract Data Types (ADT)

ADT is an abstraction of a data structure.


ADTs specify what can be stored and what
operations can be performed.

Containers
Contains objects

I can INSERT

I can REMOVE

I can …..

4
Abstract Data Types seen so far
Insert = PUSH
Remove = POP Insert = ENQUEUE
Remove = DEQUEUE

STACK
QUEUE

“last in first out”


“first in first out”

DEQUE
Insert: InsertFirst, InsertLast
Remove: RemoveFirst RemoveLast
5
What are we going to see next ...

Generalization…

6
1st
3rd
ARRAY LIST
POSITIONAL LIST
2nd

By “index”
By “position”
(by address)

SEQUENCE

7
Lists

• Array-List ADT
• Positional-List ADT
• Sequence ADT

8
Lists or Sequences
LISTS or SEQUENCES= collection of elements in linear order

1st 2nd

Array List ADT Positional List ADT

To be implemented To be implemented by linked lists


by arrays. Access by Access by “position” (or address)
“index”

SEQUENCE ADT

Combination of both
9
Array-lists
• Can access any element directly, not just first or last.

• Elements are accessed by index (or rank), the number


of elements which precede them (if starting from
index 0).

• Typically implemented by an array

V
0 1 2 i n
10
The Array-List ADT

• A sequence S (with n elements) that supports the following methods:

-get(i): Return the element of S with index i;


an error occurs if i < 0 or i > n -1
-set(i,e): Replace the element at index i with e
and return the old element; an error
condition occurs if i < 0 or i > n - 1
-add(i,e): Insert a new element into S which
will have index i; an error occurs if
i< 0 or i > n
-remove(i): Remove from S the element at index i;
an error occurs if i < 0 or i > n - 1

11
Adapter Pattern
• Two data structures A and B are often similar
• Adapt data structure B to be used as A
• Create a “wrapper class” A holding B
Examples:
– Regular array as an ArrayList, or
– ArrayList can be adapted as a Deque

Deque ArrayList
getFirst(), getLast() get(0), get(size()-1)
addFirst(e), addLast(e) add(0,e), add(size(),e)
removeFirst(), removeLast() remove(0), remove(size()-1)
12
Natural Implementation of Array-List:
with an Array

• Array V 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


V[i]

V
0 1 2 i n

13
Insertion
• In operation add(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

add(r,o):
for i = n - 1, n - 2, ... , r do V
S[i+1] ← s[i] 0 1 2 r n
S[r] ← o
n←n+1 V
0 1 2 r n
V o
0 1 2 r n
14
Deletion

• In operation remove(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
remove(r): 0 1 2 r n
e ← S[r]
for i = r, r + 1, ... , n - 2 do V
S[i] ← S[i + 1] 0 1 2 r n
n←n-1
return V
0 1 2 r n
15
Performance

• In the array based implementation of an array-list


– The space used by the data structure is O(n)
– size, isEmpty, get and replace run in O(1) time
– insert and remove run in O(n) time
• In an insert operation, when the array is full, instead
of having an ERROR, we can replace the array with a
larger one: extendable arrays seen earlier

16
Performance (contd.)

• Time time complexity of the various methods:

size O(1)
isEmpty O(1)
get O(1)
replace O(1)

insert O(n)
remove O(n)

17
Class java.util.ArrayList<E>

- Inherits from
• java.util.AbstractCollection<E>
• java.util.AbstractList<E>
– Implements
• Iterable<E> Implementation with
• Collection<E> extendable arrays
• List<E>
• RandomAccess
• The methods
– size(), isEmpty(), get(int) and set(int,E) in time O(1)
– add(int,E) and remove(int) in time O(n)

18
If we were to implement an array-list with a
doubly linked list it would be quite inefficient !

header trailer

get(rank) ?

19
Finding an element at a certain rank

Algorithm get(rank)
if (rank <= size()/2) { //scan forward from head
node ← header.next
for (int i=0; i < rank; i++)
node ← node.next
}else { // scan backward from the tail
node ← trailer.prev
for (int i=0; i < size()-rank-1 ; i++)
node ← node.prev
}
return node;

20
Performance with linked list …

size O(1)
isEmpty O(1)

get O(n)
replace O(n)
insert O(n)
remove O(n)

21
Positional Lists

Container of elements that store each element at a


position and that keeps these positions arranged in
a linear order

• Cannot access any element directly, can access just


first or last.
(node) (address)
• Elements are accessed by position. (place)
Positions are defined relatively to other positions
(before/after relation)

22
first

me

previous next

There is no notion of rank - I don’t know my rank.


I only know who is next and who is before

23
The Positional-List ADT

ADT with position-based methods

• generic methods size(), isEmpty()


• accessor methods first(), last()
before(p), after(p)
• update methods
addFirst(e), addLast(e)
addBefore(p,e), addAfter(p,e)
set(p,e), remove(p)

24
Natural Implementation: with a Linked List
• A doubly linked list provides a natural
implementation of the Positional-List ADT
• Nodes implement Position and store: prev next
– element
– link to the previous node
– link to the next node
• Special trailer and header nodes
elem node

header nodes/positions trailer

elements

25
Insertion
• We visualize operation addAfter(p, X), which returns position q
p

q
X

p q

26
Insertion
• We visualize operation addAfter(p, e), which returns position v

e
v
addAfter(p,e)
Create a new node v
Correct order??
v.setNext(p.getNext())
v.setElement(e)
(p.getNext()).setPrev(v)
v.setPrev(p)
p.setNext(v)
27
Insertion
• We visualize operation addAfter(p, e), which returns position v

e
v
addAfter(p,e)
Create a new node v
v.setElement(e)
v.setPrev(p)
v.setNext(p.getNext())
(p.getNext()).setPrev(v)
p.setNext(v)
28
Deletion

• We visualize remove(p), where p = last()


p

29
p
t

remove(p)
t ← p.element
(p.getPrev()).setNext(p.getNext())
(p.getNext()).setPrev(p.getPrev())
p.setPrev(null)
p.setNext(null)
return t
30
Performance

• In the implementation of the Positional-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 Positional-List ADT run
in O(1) time

31
A more general ADT:
Sequence ADT

• Combines the Array-List and Positional-List ADT

• Adds methods that bridge between index and positions


-atIndex(i) returns a position
-indexOf(p) returns an integer index

32
An array-based Implementation

• Circular array storing positions


• A position object stores:
– Element
– index
• f and l keep
track of first 0 1 2 3
and last
positions
S
f l
33
0 YOW 1 YYZ 2 YVR 3 YUL

2 YYC

f l

add(2,YYC)

34
0 YOW 1 YYZ 2 YVR 3 YUL

2 YYC

f l

Rank 2 at index f+2 add(2,YYC)

35
2 YYC

0 YOW 1 YYZ 3 YVR 4 YUL

f l
Change all other ranks

36
2 YYC

0 YOW 1 YYZ 3 YVR 4 YUL

f l

atIndex(i) Direct access to the position at index f+i

3 YVR
Immediate access to
indexOf(position):
the corresponding index
position

37
Array-based Implementation

addFirst, addBefore, addAfter, remove

O(n)

Also: add, remove based on the index


O(n)

Other methods
O(1)

38
Implementation
with Doubly Linked List

All methods are inherited ….

Bridges:
atIndex(i), indexOf(p): O(n)

Must traverse the list

39
Summary: Array-based implementation of
Sequences
Need to move elements

addFirst,addBefore,addAfter,add(i,e) ---- O(n)

remove(position) remove(index) ---- O(n)

Bridges: atIndex(i), indexOf(p): ---- O(1)

get(i), set(i,e) ----- O(1) Because the position contains


also the index

40
Summary: Implementation of Sequences by
Doubly-linked lists

addFirst,addBefore,addAfter, remove(position) ----


O(1)
add(i,e)
remove(index) ---- O(n)

Bridges: atIndex(i), indexOf(p): ---- O(n)

Need to traverse to find an index

get(i), set(i,e) ----- O(n)


41
Analysis of Algorithms 42

You might also like