CSE 331 Spring 2008, Section 1
Homework 4 Solutions
1.
Let
h
= 0.
This gives us a binary “tree” with a single node.
For this case, 2
h
+1
– 1 = 2
1
– 1 = 2 – 1
= 1.
Thus, the maximum number of nodes in a binary tree of height
h
is 2
h
+1
– 1 when
h
= 0.
Assume that for some
k
≥ 0, the maximum number of nodes in a binary tree of height
k
is known
to be 2
k
+1
– 1.
Consider a binary tree of height
k
+ 1.
The root node can have at most two
subtrees.
Each subtree can have a height of at most
k
.
By our inductive hypothesis, these subtrees
can contain a maximum of 2
k
+1
– 1 nodes.
Considering these two subtrees and the root, the
maximum number of nodes in a binary tree of height
k
+ 1 is therefore 2 * (2
k
+1
– 1) + 1.
Distributing terms, 2 * (2
k
+1
– 1) + 1 = 2 * 2
k
+1
– 2 + 1 = 2
(
k
+1) + 1
– 1.
By induction, the maximum number of nodes in a binary tree of height
h
is 2
h
+1
– 1 for any
h
≥ 0.
2.
(a)
At depth
d
of a perfect binary tree, there will be 2
d
nodes.
The depths of the nodes will range
from 0 to
h
, the height of the tree.
In #1, we established that there are 2
h
+1
– 1 nodes in a perfect
binary tree of height
h
.
Therefore, the average depth of the nodes will be
∑
d
=
0
h
d
2
d
/
2
h
1
−
1
.
To figure out what sort of values we can expect, it would be very nice to get rid of the summation.
If we write out a few terms of the series starting from
h
= 1, we get 2, 10, 34, 98, 258, 642.
..
If we
subtract 2 from each of these terms, we get the much nicer-looking series 0, 8, 32, 96, 256, 640.
..
Factoring each of these terms gets us the still-nicer-looking series 0, 2
3
, 2
5
, 3 * 2
5
, 2
8
, 5 * 2
7
...
We
can then rearrange the terms slightly to get 0 * 2
2
, 1 * 2
3
, 2 * 2
4
, 3 * 2
5
, 4 * 2
6
, 5 * 2
7
.
From this, it
seems reasonable to guess that
∑
d
=
0
h
d
2
d
=
h
−
1
2
h
1
2 .
We can prove this by induction.
●
Let