Beruflich Dokumente
Kultur Dokumente
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
ECE750 Lecture 4: Trees, Tree Iterators,
Treaps, Unique Representation, Persistence,
Red-Black Trees, Tries
Todd Veldhuizen
tveldhui@acm.org
Electrical & Computer Engineering
University of Waterloo
Canada
Oct. 5, 2007
1 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Trees
...
2 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Binary Trees
Terminology:
C is the parent of B, D;
if (z == data)
return true;
else if ((z < data) && (left != null ))
return left . contains(z);
else if ((z > data) && (right != null))
return right . contains(z);
return false ;
7 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Binary Search Trees
Make the root A[0]; make its right subtree A[1], ...
9 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Binary Search Trees
Example: treaps
red-black trees;
AVL trees;
2-3 trees;
B-trees;
splay trees.
10 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Enumeration of Binary Search Trees
n
3/2
(Sequence A000108 in the Online Encyclopedia of
Integer Sequences.)
if (z == data) return;
else if (z < data)
if ( left == null)
left = new Tree(z);
else
left . insert (z);
else if (z > data)
if ( right == null)
right = new Tree(z);
else
right . insert (z);
15 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Iterator for a binary search tree I
if ( left != null)
left >traverseInorder ();
// do something with this key
if ( right != null)
right >traverseInOrder();
16 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Iterator for a binary search tree II
BSTNode t = (BSTNode)stack.pop();
if (t . right child != null)
18 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Iterator for a binary search tree II
fathom(t. right child );
return t ;
do
stack. push(t );
t = t. left child ;
while (t != null );
19 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Rotations I
d
d
d
d
B
d
d
d
d
E
A C
rotate right at D
rotate left at B
B
d
d
d
d
A D
.~
~
~
~
d
d
d
d
C E
Any two binary search trees on the same set of keys can
be transformed into one another by a sequence of
rotations.
20 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Rotations II
if ( left == null)
throw new RuntimeException(Cannot rotate here);
Tree A = left . left ;
Tree B = left ;
Tree C = left . right ;
Tree D = this;
Tree E = right ;
return new Tree(B.data, A, new Tree(D.data, C, E));
b
b
a c
c
c
d
c
c
e
b
a
a
a
d
c
c
c e
23 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Part I
Balancing strategies for binary search trees
24 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Binary Search Trees
Balance invariants:
1. No red node has a red child.
2. Every path in a subtree contains the same number of
black nodes.
Ordering invariants:
(k
2
, p
2
)
t
t
t
t
t
t
t
t
t
u
u
u
u
u
u
u
u
u
(k
1
, p
1
) (k
3
, p
3
)
k
1
k
2
k
3
Key order
_
p
2
p
p
1
p
2
p
p
3
Priority order
Every node has a higher priority than its descendents.
40 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Balance of treaps
Example: the treap below has just had (e, 19) inserted
as a new leaf. Rotations have not yet been performed.
(d,23)
(b,11) (h,14)
(a,7) (c,1) (f,6) (i,13)
(e,19)
1/2
ways to place n
keys in a binary search tree (Catalan numbers). e.g.
C
20
= 6564120420.
46 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Unique representation II
n)
worst-case time for insert, delete, search [23].
The left subtree of the root will contain all keys < k,
and the right subtree of the root will contain all keys
> k.
Of the keys < k, the one with the highest priority must
occupy the left child of the root. This then splits
constructing the left subtree into two subproblems.
etc.
48 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Unique Representation property of treaps
{(c, 1), (b, 11), (a, 7)} {(i , 13), (h, 14), (f , 6)}
(b, 11)
u
u
u
u
u
u
u
u
{(i , 13), (h, 14), (f , 6)}
(a, 7) (c, 1)
49 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Part II
Persistence
50 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Persistent Data Structures
Literature: [10, 11, 6, 9]
e
e
e
e
e
e
e
e
Y1
Y2
g
g
g
g
g
g
g
g
W
53 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Applications of persistent data structures
Literature: [19]
if (z == data) return;
else if (z < data)
if ( left == null)
left = new Tree(z);
else
left . insert (z);
else if (z > data)
if ( right == null)
right = new Tree(z);
else
right . insert (z);
56 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Contrast: BST insert in a pure functional style
No assignments to elements of the node; changes are made by creating
new nodes. The insert operation returns a new root pointer. The
previous root pointer still points to the original version of the tree.
Tree insert (int z)
if (z == data)
return this ;
else if (z < data)
if ( left == null)
return new Tree(data, new Tree(z), right );
else
return new Tree(data, left . insert (z), right );
else if (z > data)
if ( right == null)
right = new Tree(data, left , new Tree(z));
else
return new Tree(data, left , right . insert (z ));
57 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Fully persistent treaps
We write
If w, v
) together with an
associative binary operator () and an identity element (). For
any strings u, v, w
,
u (v w) = (u v) w
v = v = v
1
Innite strings are very useful also: if we write a real number
x [0, 1] as a binary number e.g. 0.101100101000 , this is a
representation of x by an innite string from
.
65 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Tries
1
X
X
X
X
y
z
= 0, 10, 11
The set P
The set P
is prex-closed: if wv P
, then w P
also. i.e., P
.
2
2
We can dene
as an operator by A
{w : wv A}.
is a
closure operator. A useful fact: every closure operator has as its range a
complete lattice, where meet and join are given by (X Y)
= X
and (X Y)
= (X
or P
or P
, we can
reproduce the tree.
3
i =1
2
d
i
1 (1)
76 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Tries: Krafts inequality I
x
is the iterated logarithm
log
x =
_
0 if x 1
1 + log
(log x) otherwise
79 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Tries: Krafts inequality II
See e.g., [4, 20].
Where does this bound come from? Well, a necessary condition for
X
k=0
2
d
k
1
to hold is that the series
P
k=0
2
d
k
converges. For example, if
d
k
= log k, then 2
d
k
=
1
k
, the Harmonic series. The Harmonic series
diverges, so Krafts inequality cant hold.
We can parlay this into an inequality by remembering the comparison
test for convergence of series: if a
k
, b
k
are two positive series, and
a
k
b
k
for all k, then
P
a
k
P
b
k
. If we stick the Harmonic series in
for a
k
and 2
d
k
for b
k
, we get:
If
1
k
2
d
k
for all k then
P
2
d
k
.
The premiss of this test must be false if
P
2
d
k
does not diverge to
innity. Therefore 2
d
k
must be <
1
k
for at least some k. If 2
d
k
<
1
k
for only some nite number of choices of k, the series would still
diverge. So, a necessary condition for 2
d
k
to converge is that 2
d
k
<
1
k
80 / 1
ECE750 Lecture 4:
Trees, Tree
Iterators, Treaps,
Unique
Representation,
Persistence,
Red-Black Trees,
Tries
Todd Veldhuizen
tveldhui@acm.org
Tries: Krafts inequality III
for innitely many terms. Taking logarithms and multiplying through by
1 we get d
k
> log k for innitely many i .
We can generalize this by saying that if g (1) is any diverging
function, then d
k
> log g