5.05.balanced Trees
5.05.balanced Trees
Balanced Trees
ece.uwaterloo.ca
[email protected]
Outline
In this topic, we will:
– Introduce the idea of balance
– We will introduce a few examples
Balanced trees
3
Background
We want to ensure that the run times never fall into w(ln(n))
Requirement:
– We must maintain a height which is Q(ln(n))
For a perfect tree, all nodes have the same number of descendants
on each side
Perfect binary trees are balanced while linked lists are not
Balanced trees
6
4.9.1 Examples
Requirements:
– The root must be black
– All children of a red node
must be black
– Any path from the root
to an empty node must
have the same number
of black nodes
Balanced trees
11
4.9.2.1 Red-Black Trees
The ratios of the empty nodes at the root node are 5/10 and 5/10
Balanced trees
14
4.9.2.2 Weight-Balanced Trees
The ratios of the empty nodes at this node are 2/5 and 3/5
Balanced trees
15
4.9.2.2 Weight-Balanced Trees
The ratios of the empty nodes at this node, however, are 4/5 and 1/5
Balanced trees
16
4.9.2.2 Weight-Balanced Trees
If a is bounded by
1 2
0.25 1 0.2929
4 2
Summary
References
Usage Notes
• These slides are made publicly available on the web for anyone to
use
• If you choose to use them, or a part thereof, for a course at another
institution, I ask only three things:
– that you inform me that you are using the slides,
– that you acknowledge my work, and
– that you alert me of any mistakes which I made or changes which you
make, and allow me the option of incorporating such changes (with an
acknowledgment) in my set of slides
Sincerely,
Douglas Wilhelm Harder,
MMath