Professional Documents
Culture Documents
Midterm Review
3/10/2015
Daniel Bauer
Midterm
60 minutes. Be on time!
Topics - Overview
Series, Proofs.
Applications.
Types of Proofs
Remember some examples?
Proof by Induction
Proof by Contradiction
Proof by Counterexample
Time / Space
and
f(N) = N2 + 2
e.g. c = 1, n0 = 16.1
and
f(N) = N2 + 2
T(N) is in the
order of f(N)
e.g. c = 1, n0 = 16.1
and
f(N) = N2 + 2
T(N) is in the
order of f(N)
f(N) is an
upper bound
on T(N)
e.g. c = 1, n0 = 16.1
Lower Bound:
such that
and
and
there is some
.
exponential
quadratic
linear
log-squared
logarithmic
constant
Stack (LIFO)
Map
Ordered Set
Balanced Search Tree
Ordered Map
Balanced Search Tree
Hash Table
Linked List entries
Probing Hash Tables
A0 A1 A2 A3 A4 A5 A6
A0 A1 A2 A3 A4 A5 A6
A0 A1 A2 A3 A4 A5 A6
A0 A1 A2 A3 A4 A5 A6
Array List
1
N=7
6 7 8 9
Worst Case Running Times
printList
O(N)
find(x)
O(N)
findKth(k)
O(1)
insert(x,k)
remove(x)
Array List
1
insert(5,7): O(1)
N=7
6 7 8 9
Worst Case Running Times
printList
O(N)
find(x)
O(N)
findKth(k)
O(1)
insert(x,k)
remove(x)
Array List
1
insert(5,7): O(1)
remove(7): O(1)
N=7
6 7 8 9
Worst Case Running Times
printList
O(N)
find(x)
O(N)
findKth(k)
O(1)
insert(x,k)
remove(x)
Array List
7 moves
5
insert(5,7): O(1)
remove(7): O(1)
insert(5,0): O(N)
N=7
6 7 8 9
Worst Case Running Times
printList
O(N)
find(x)
O(N)
findKth(k)
O(1)
insert(x,k)
remove(x)
Array List
7 moves
5
insert(5,7): O(1)
remove(7): O(1)
insert(5,0): O(N)
remove(0): O(N)
N=7
6 7 8 9
Worst Case Running Times
printList
O(N)
find(x)
O(N)
findKth(k)
O(1)
insert(x,k)
O(N)
remove(x)
O(N)
42
23
null
42
23
null
42
23
null
42
23
null
42
23
O(N)
null
42
23
null
42
23
null
A0
A1
A2
A3
tail
push(x)
O(1)
pop()
O(1)
peek()
O(1)
empty()
O(1)
Top
Implementations discussed:
Using an Array List, Using a LinkedList
Hardware Stacks (memory abstraction, stack machine)
push(x)
O(1)
pop()
O(1)
peek()
O(1)
empty()
O(1)
Top
42
5
Implementations discussed:
Using an Array List, Using a LinkedList
Hardware Stacks (memory abstraction, stack machine)
O(1)
3
23
peek()
O(1)
42
empty()
O(1)
Implementations discussed:
Using an Array List, Using a LinkedList
Hardware Stacks (memory abstraction, stack machine)
O(1)
O(1)
Top
23
peek()
O(1)
42
empty()
O(1)
Implementations discussed:
Using an Array List, Using a LinkedList
Hardware Stacks (memory abstraction, stack machine)
Stack Applications
Stack Applications
Stack Applications
Stack Applications
Stack Applications
Stack Applications
Stack Applications
Implementing Queue.
Stack Applications
Implementing Queue.
enqueue(x)
O(1)
dequeue()
O(1)
empty()
O(1)
front back
Implementations discussed:
Using a linked list
Using a circular array
O(1)
dequeue()
O(1)
empty()
Implementations discussed:
Using a linked list
Using a circular array
O(1)
front
back
17
23
O(1)
dequeue()
O(1)
empty()
Implementations discussed:
Using a linked list
Using a circular array
O(1)
front
back
17
23
O(1)
dequeue()
O(1)
empty()
O(1)
front
Implementations discussed:
Using a linked list
Using a circular array
17
back
23
Circular Array
Implementation of Queue
17
front
23
back
Circular Array
Implementation of Queue
17
back
front
23
Tree ADT
A tree T consists of
T
A root node r.
Tree ADT
r
A tree T consists of
T1
T2
A root node r.
Tn
Representing Trees
1st child
n1
1st child
n0
next sibling
n2
n3
next sibling
Representing Trees
n1
n2
n3
M-ary Trees
Binary Trees
(a + b * c) + (d * e + f) * g
+
*
b
*
d
g
f
*
b
*
d
g
f
1. Process root
2. Process left child
3. Process right child
*
b
*
d
g
f
BST property:
For all nodes s in Tl, sitem < ritem.
contains(x)
insert(x)
findMin()
findMax()
remove()
Tl
O(height(T))
O(height(T))
O(height(T))
O(height(T))
O(height(T))
Tr
Best case:
height(T)=log N
4
2
2
1
3
4
5
3
7
2
1
3
4
1
Maintaining Balance in an
AVL Tree
Single Rotation
k2
k1
z
y
Single Rotation
k1
k2
Double Rotation
k3
k1
z
k2
x
yl
yr
Double Rotation
k2
k3
k1
yl
yr
B-Trees
values (usually
16
25
33
27
38
34
36
41
46
48
OrderedSet ADT
findMin()
findMax()
A
1
AB
2
5
6
8
B
9
AB
Set ADT
Supported operations:
insert(x)
remove(x)
contains(x)
isEmpty()
size()
Set ADT
Supported operations:
insert(x)
addAll(s) / union(s)
remove(x)
removeAll(s)
contains(x)
retainAll(s) / intersection(s)
isEmpty()
size()
A
1
AB
2
5
6
8
B
9
AB
Map ADT
Two operations:
get(key)
put(key, value)
key1
key2
key3
key4
value1
value2
value3
Hash Tables
Assuming hash(key) takes constant time, get and put run in O(1).
0
Alice 555-341-1231
hash(key)
Alice 555-341-1231
TableSize - 1
Separate Chaining
Anna 555-521-2973
0
hash(key)
Alice 555-341-1231
1
2
TableSize - 1
Bob 555-341-1231
Separate Chaining
Anna 555-521-2973
0
hash(key)
Anna 555-521-2973
1
2
TableSize - 1
Bob 555-341-1231
Alice
555-341-1231
40
hash(key)
x % 11
0
1
2
3
4
5
6
7
8
9
10
40
Linear Probing
17
hash(key)
x % 11
0
1
2
3
4
5
6
7
8
9
10
39
40
51
18
17
Quadratic Probing
No primary clustering.
If table size is not prime or table is more than half full it is
possible that no empty cell can be found for a key, even if
there is still space in the table.
3
47
hash(key)
x % 11
f(3) = 9
0
1
2
3
4
5
6
7
8
9
10
47
25
3
14
Double Hashing
Compute a second hash function to
determine a linear offset for this key.
f(1) = 1 hash2(x) =3
62
hash(key)
x % 11
hash2(key)
5-x%5
0
1
2
3
4
5
6
7
8
9
10
40
84
62