DSA 3: ADT Implementation
NBIT209/DIFT204: Data Structures and Algorithms
PAUL OFFEI
1
NBIT209/DIFT204
NBIT209/DIFT204
1
Warm Up
ArrayList
get return data[index]
set data[index] = value
add data[size] = value, if out of space grow data
insert shift values to make hole at index, data[index] = value, if out of space grow data
delete shift following values forward
size return size
state
behavior
data[]
size
ArrayList
uses an Array as underlying storage
0 | 1 | 2 | 3 | 4 |
88.6 | 26.1 | 94.4 | 0 | 0 |
list
free space
LinkedList
get loop until index, return node’s value
set loop until index, update node’s value
add create new node, update next of last node
insert create new node, loop until index, update next fields
delete loop until index, skip node
size return size
state
behavior
Node front
size
LinkedList
uses nodes as underlying storage
88.6
26.1
94.4
Q: Would you use a LinkedList or ArrayList implementation for each of these scenarios?
Situation #1: Choose a data structure that implements the List ADT that will be used to store a list of songs in a playlist.
Situation #2: Choose a data structure that implements the List ADT that will be used to store the history of a bank customer’s transactions.
Situation #3: Choose a data structure that implements the List ADT that will be used to store the order of students waiting to speak to a TA at a tutoring center
List ADT
get(index) return item at index
set(item, index) replace item at index
append(item) add item to end of list
insert(item, index) add item at index
delete(index) delete item at index
size() count of items
state
behavior
Set of ordered items
Count of items
NBIT209/DIFT204
2
Warm Up
Situation: Write a data structure that implements the List ADT that will be used to store a list of songs in a playlist.
Features to consider:
Why ArrayList?
Why LinkedList?
Q: Would you use a LinkedList or ArrayList implementation for this scenario?
Discuss with those around you!
NBIT209/DIFT204
3
Agenda
Design Decisions Review
Stacks
Queues
Dictionaries
Questions
NBIT209/DIFT204
4
Design Decisions Review
Stacks
Queues
Dictionaries
Questions
NBIT209/DIFT204
5
Design Decisions
For every ADT there are lots of different ways to implement them
Based on your situation you should consider:
This class is all about implementing ADTs based on making the right design tradeoffs!
A common topic in interview questions!
NBIT209/DIFT204
6
A quick aside: Types of memory
int[] array = new int[3];
array[0] = 3;
array[1] = 7;
array[2] = 3;
Node front = new Node(3);
front.next = new Node(7);
front.next.next = new Node(3);
Arrays - contiguous memory: when the “new” keyword is used on an array the operating system sets aside a single, right-sized block of computer memory
0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 |
| d672 | 8baf | 020a | | | | 713f | 04e3 | 2e6e |
3
7
3
Nodes- non-contiguous memory: when the “new” keyword is used on a single node the operating system sets aside enough space for that object at the next available memory location
array
0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 |
| 4b44 | | | 052f | d3cd | | | 23d4 | | |
front
3
7
3
More on how memory impacts runtime later in this course…
NBIT209/DIFT204
7
Design Decisions
Situation: Write a data structure that implements the List ADT that will be used to store the history of a bank customer’s transactions.
Features to consider:
Why ArrayList?
Why LinkedList?
Q: Would you use a LinkedList or ArrayList implementation for this scenario?
Discuss with those around you!
NBIT209/DIFT204
8
Real-World Scenarios: Lists
LinkedList
ArrayList
NBIT209/DIFT204
9
Questions?
NBIT209/DIFT204
10
Design Decisions Review
Stacks
Queues
Dictionaries
Questions
NBIT209/DIFT204
11
What is a Stack?
stack: A collection based on the principle of adding
elements and retrieving them in the opposite order.
Last-In, First-Out ("LIFO")
Elements are stored in order of insertion.
| |
top | 3 |
| 2 |
bottom | 1 |
pop, peek
push
Stack ADT
push(item) add item to top
pop() return and remove item at top
peek() look at item at top
size() count of items
isEmpty() count of items is 0?
state
behavior
Set of ordered items
Number of items
supported operations:
NBIT209/DIFT204
12
Implementing a Stack with an Array
0 | 1 | 2 | 3 |
| | | |
push(3)
3
4
5
numberOfItems =
0
1
2
ArrayStack<E>
push data[size] = value, if out of room grow data
pop return data[size - 1], size-1
peek return data[size - 1]
size return size
isEmpty return size == 0
state
behavior
data[]
size
Stack ADT
push(item) add item to top
pop() return and remove item at top
peek() look at item at top
size() count of items
isEmpty() count of items is 0?
state
behavior
Set of ordered items
Number of items
push(4)
pop()
push(5)
Big O Analysis | |
pop() | |
peek() | |
size() | |
isEmpty() | |
push() | |
O(1) Constant |
O(N) Linear if a resize is required O(1) Otherwise |
O(1) Constant |
O(1) Constant |
O(1) Constant |
NBIT209/DIFT204
13
Implementing a Stack with Nodes
push(3)
numberOfItems =
0
1
2
LinkedStack<E>
push add new node at top
pop return and remove node at top
peek return node at top
size return size
isEmpty return size == 0
state
behavior
Node top
size
Stack ADT
push(item) add item to top
pop() return and remove item at top
peek() look at item at top
size() count of items
isEmpty() count of items is 0?
state
behavior
Set of ordered items
Number of items
4
3
front
push(4)
pop()
Big O Analysis | |
pop() | |
peek() | |
size() | |
isEmpty() | |
push() | |
O(1) Constant |
O(1) Constant |
O(1) Constant |
O(1) Constant |
O(1) Constant |
NBIT209/DIFT204
14
Real-World Scenarios - Stacks
NBIT209/DIFT204
15
Design Decisions Review
Stacks
Queues
Dictionaries
Questions
NBIT209/DIFT204
16
What is a Queue?
queue: Retrieves elements in the order they were added
add
remove,
peek
Queue ADT
add(item) add item to back
remove() remove and return item at front
peek() return item at front
size() count of items
isEmpty() count of items is 0?
state
behavior
Set of ordered items
Number of items
supported operations:
front | | back |
1 | 2 | 3 |
NBIT209/DIFT204
17
Implementing a Queue with an Array
0 | 1 | 2 | 3 | 4 |
| | | | |
add(5)
numberOfItems =
0
5
8
9
1
2
3
ArrayQueue<E>
add – data[size] = value, if out of room grow data
remove – return data[size - 1], size-1
peek – return data[size - 1]
size – return size
isEmpty – return size == 0
state
behavior
data[]
Size
front index
back index
Queue ADT
add(item) add item to back
remove() remove and return item at front
peek() return item at front
size() count of items
isEmpty() count of items is 0?
state
behavior
Set of ordered items
Number of items
front = 0
back = 0
1
2
1
add(8)
add(9)
remove()
Big O Analysis | |
remove() | O(1) Constant |
peek() | O(1) Constant |
size() | O(1) Constant |
isEmpty() | O(1) Constant |
add() | O(N) Linear if a resize is required O(1) Otherwise |
NBIT209/DIFT204
18
Implementing a Queue with an Array
(Wrap around)
0 | 1 | 2 | 3 | 4 |
| | | | |
numberOfItems =
3
front
back
5
9
2
7
4
add(7)
4
5
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
5 | 9 | 2 | 7 | 4 | | | | | |
front
back
1
add(4)
add(1)
6
NBIT209/DIFT204
19
Implementing a Queue with Nodes
add(5)
LinkedQueue<E>
add – add node to back
remove – return and remove node at front
peek – return node at front
size – return size
isEmpty – return size == 0
state
behavior
Node front
Node back
size
Queue ADT
add(item) add item to back
remove() remove and return item at front
peek() return item at front
size() count of items
isEmpty() count of items is 0?
state
behavior
Set of ordered items
Number of items
numberOfItems =
0
1
2
8
5
front
back
Big O Analysis | |
remove() | O(1) Constant |
peek() | O(1) Constant |
size() | O(1) Constant |
isEmpty() | O(1) Constant |
add() | O(1) Constant |
add(8)
remove()
NBIT209/DIFT204
20
Real-World Examples
NBIT209/DIFT204
21
Questions?
NBIT209/DIFT204
22
Design Decisions Review
Stacks
Queues
Dictionaries
Questions
NBIT209/DIFT204
23
Dictionaries (aka Maps)
Every Programmer’s Best Friend
You’ll probably use one in almost every programming project.
Because it’s hard to make a big project without needing one sooner or later.
// two types of Map implementations supposedly you have learnt in Java
Map<String, Integer> map1 = new HashMap<>();
Map<String, String> map2 = new TreeMap<>();
NBIT209/DIFT204
24
Maps
map: Holds a set of distinct keys and a
collection of values, where each key is
associated with one value.
key | value |
“you" | 22 |
key | value |
“in" | 37 |
key | value |
“the" | 56 |
key | value |
“at" | 43 |
map.get("the")
56
Dictionary ADT
put(key, item) add item to collection indexed with key
get(key) return item associated with key
containsKey(key) return if key already in use
remove(key) remove item and associated key
size() return count of items
state
behavior
Set of items & keys
Count of items
supported operations:
NBIT209/DIFT204
25
Implementing a Dictionary with an Array
0 | 1 | 2 | 3 | 4 |
| | | | |
containsKey(‘c’)
get(‘d’)
put(‘b’, 97)
(‘a’, 1)
(‘b’, 2)
Dictionary ADT
put(key, item) add item to collection indexed with key
get(key) return item associated with key
containsKey(key) return if key already in use
remove(key) remove item and associated key
size() return count of items
state
behavior
Set of items & keys
Count of items
(‘c’, 3)
97)
(‘d’, 4)
(‘e’, 20)
put(‘e’, 20)
Big O Analysis: if the key is the last one looked at / is not in the dictionary | |
put() | O(N) linear |
get() | O(N) linear |
containsKey() | O(N) linear |
remove() | O(N) linear |
size() | O(1) constant |
Big O Analysis: if the key is the first one looked at | |
put() | O(1) constant |
get() | O(1) constant |
containsKey() | O(1) constant |
remove() | O(1) constant |
size() | O(1) constant |
ArrayDictionary<K, V>
put find key, overwrite value if there. Otherwise create new pair, add to next available spot, grow array if necessary
get scan all pairs looking for given key, return associated item if found
containsKey scan all pairs, return if key is found
remove scan all pairs, replace pair to be removed with last pair in collection
size return count of items in dictionary
state
behavior
Pair<K, V>[] data
NBIT209/DIFT204
26
Implementing a Dictionary with Nodes
LinkedDictionary<K, V>
put if key is unused, create new with pair, add to front of list, else replace with new value
get scan all pairs looking for given key, return associated item if found
containsKey scan all pairs, return if key is found
remove scan all pairs, skip pair to be removed
size return count of items in dictionary
state
behavior
front
size
containsKey(‘c’)
get(‘d’)
put(‘b’, 20)
Dictionary ADT
put(key, item) add item to collection indexed with key
get(key) return item associated with key
containsKey(key) return if key already in use
remove(key) remove item and associated key
size() return count of items
state
behavior
Set of items & keys
Count of items
front
‘c’
9
‘b’
7
‘d’
4
‘a’
1
20
Big O Analysis: if the key is the last one looked at / is not in the dictionary | |
put() | O(N) linear |
get() | O(N) linear |
containsKey() | O(N) linear |
remove() | O(N) linear |
size() | O(1) constant |
Big O Analysis: if the key is the first one looked at | |
put() | O(1) constant |
get() | O(1) constant |
containsKey() | O(1) constant |
remove() | O(1) constant |
size() | O(1) constant |
NBIT209/DIFT204
27
Real-World Examples
NBIT209/DIFT204
28
Design Decisions
Situation: You are writing a program to schedule jobs sent to a laser printer. The laser printer should process these jobs in the order in which the requests were received. There are busy and slow times for requests that can have large differences in the volume of jobs sent to the printer. Which ADT and what implementation would you use to store the jobs sent to the printer?
ADT options:
Implementation options:
LinkedQueue
This will maintain the original order of the print jobs, but allow you to easily cancel jobs stuck in the middle of the queue. This will also keep the space used by the queue at the minimum required levels despite the fact the queue will have very different lengths at different times.
Discuss with your neighbors: For the following scenario select the appropriate ADT and implementation and explain why they are optimal for this situation.
NBIT209/DIFT204
29
Design Decisions Review
Stacks
Queues
Dictionaries
Questions?
NBIT209/DIFT204
30
That’s all!
Have a great weekend
NBIT209/DIFT204
31