9/26/13 1

9/26/13 dynamic tables
 
data structure
 
 
 
implementation
 
 
cs140 – algorithms
prof. yi chen
september 24, 2013
 
 
insert(T,x)
delete(T,x)
array
If insert in a full array, double the size of the array, copy over,
and insert
If delete takes array to some reduced size, contract array
cost of insert?
9/24/13
9/24/13
Binary search trees
 
 
Binary search trees
what is it?
what operations?
 
 
 
 
 
 
 
search(T,k)
minimum(T)
maximum(T)
successor(T,x)
predecessor(T,x)
insert(T,x)
delete(T,x)
 
20
15
30
 
what is it?
what operations?
 
 
10
 
18
 
 
15
 
 
17
20
search(T,k)
minimum(T)
maximum(T)
successor(T,x)
predecessor(T,x)
insert(T,x)
delete(T,x)
15
10
30
29
18
15
19
17
9/24/13
9/24/13
1 9/26/13 Balanced binary search trees
 
 
 
 
 
Red-black trees
Red-black tree
Splay tree
2-3 trees
AVL trees
... etc ...
 
each node:
 
 
 
 
tree maintains 5 properties
1. 
2. 
3. 
4. 
5. 
9/24/13
color
key
left, right, parent
every node is red or black
root is black
leaves (null pointers) are black
if node is red, both children are black
for every node, all paths from node to descendant leaves
contain the same number of black nodes.
9/24/13
Red-black trees
1. 
2. 
3. 
4. 
5. 
every node is red or black
root is black
leaves (null pointers) are
black
if node is red, both
children are black
for every node, all paths
from node to descendant
leaves contain the same
number of black nodes.
26
17
41
47
30
28
38
50
9/24/13
2