Notes on Red-black Trees
Geoffrey Matthews
May 25, 2016
1
Red-black trees
A variation of binary search trees.
Balanced: height is O(lg n), where n is number of nodes.
Operations will take O(lg n) in worst case.
Red-black trees
A red-black tree is a bi
Notes on Amortized Analysis
Geoffrey Matthews
May 24, 2016
1
Amortized analysis
Analyze a sequence of operations on a data structure.
Goal: Show that although some operations may be expensive, on average
the cost per operation is small.
Average is not
Notes on Heapsort
Geoffrey Matthews
April 26, 2016
1
Heapsort
O(n lg n) worst case like merge sort
Sorts in place like insertion sort
Combines best of both algorithms
Heaps
A nearly complete binary tree.
Height: number of edges on longest path from n
Divide and Conquer Matrix Multiply
T (1) = c
T (n) = 8T (n/2) + cn2
+ c(n)2
T (n) =
8T
(n/2)
= 82 T
2 ) + 8c(n/2)2
8T
(n/2)
(n/2
2
2
3
3
8
T
(n/2
)
=
8
T
(n/2
) + 82c(n/22)2
3
3
4
4
8
T
(n/2
)
=
8
T
(n/2
) + 83c(n/23)2
.
(
lg n1(lg n1
lg n
lg n1
lg
Mergesort Recurrence
T (1) = c
f (n) = 2T (n/2) + cn
(if n > 1)
Lets make this easier by assuming n is a power of two:
n = 2j
j = log2(n)
Now we can rewrite our equations:
T (20) = c
f (2j ) = 2T (2j1) + cn
(if j > 0)
Now we can introduce a new function,
Notes on Binary Search Trees
Geoffrey Matthews
May 18, 2016
1
Search Trees
Data structures that support many dynamic-set operations.
Dictionaries and priority queues.
Basic operations take time proportional to height of the tree.
Best case: (lg n)
Wo
Notes on Hash Tables
Geoffrey Matthews
May 16, 2016
1
Dictionary Operations
Insert
Search
Delete
Hash table implementation of Dictionary
Expected search time: O(1)
Worst case search: O(n)
2
Hash table is generalization of an ordinary array
With arra
Master Theorem
Let a 1 and b > 1 be constants, let f (n) be a function, and let
T (n) be defined on nonnegative integers by
n
T (n) = aT
+ f (n)
b
Then T (n) has the following asymptotic bounds:
f (n)
O(n
)
(nlogb a )
(nlogb a+ )
logb a
T (n)
(nlogb a )
(
Recurrences
f (1) = 5
f (n) = f (n 1) + 3
(if n > 1)
Find some simple cases:
f (1) = 5
f (2) = f (1) + 3 = 8
f (3) = f (2) + 3 = 11
f (4) = f (3) + 3 = 14
Guess a solution: f (n) = 3n + 2. This is called a closed
form since there is no recursion in the de