Data Structures and Algorithms Key To Homework Assignment 6
Data Structures and Algorithms Key To Homework Assignment 6
1. For each of the following code segments, find a function f (n) with the property that the
number of statements executed by the segment is O(f (n)). Your functions f (n) should be as
simple as possible.
(a) sum = 0;
for (int i = 0; i < n; i++)
sum++;
f (n) = n.
(b) sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum++;
The inner loop is Θ(i). So the entire code segment has runtime proportional to
n−1
X n(n − 1)
i= ,
i=0
2
and f (n) = n2 .
(e) sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < i*i; j++)
for (int k = 0; k < j; k++)
sum++;
1
Data Structures and Algorithms, Key to Homework 6 2
The innermost loop will only be executed when j is is a multiple of i, which will occur
precisely i times. The remaining passes through the for j loop will simply execute the
test and increment j. So we can rewrite the code so that it has the same runtime (up to
big-Θ) as follows.
sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++) {
j_prime = i*j;
for (int k = 0; k < j_prime; k++)
sum++;
Code that has runtime i-1;
}
The runtime of the body of the for j loop is Θ(i + j prime) = Θ(i + ij). So the runtime
of the for j loop is
i
!
X i2 (i + 1)
Θ (i + ij) = Θ i2 + ,
j=1
2
2. Problem 5.2, page 161. Define the degree of a node as the number of its nonempty children.
Prove by induction that the number of degree 2 nodes in any binary tree is one less than the
number of leaves. (You can assume that the tree is nonempty.)
We use induction on n, the number of nodes in the binary tree. If T is any binary tree,
we use the notation l(T ) and d(T ) to denote the number of leaves of T and the number of
degree 2 nodes of T, respectively. So we wish to show that for any nonempty binary tree T
l(T ) = d(T ) + 1.
Data Structures and Algorithms, Key to Homework 6 3
Base case. If T is a binary tree with 1 node, then T consists of a single leaf, and no other
nodes. In particular, it has no degree 2 nodes. So in this case l(T ) = d(T ) + 1.
Induction hypothesis. Suppose then that n0 is a positive integer with the property that if T
is any binary tree with n = n0 (≥ 1) nodes, then l(T ) = d(T ) + 1.
Induction step. Suppose now that T is a binary tree with n = n0 + 1 ≥ 2 nodes. Then (as
we saw in class) T has a leaf L. By deleting L from T, we get a new tree T 0 with n = n0 ≥ 1
nodes. So by the induction hypothesis, l(T 0 ) = d(T 0 ) + 1. We consider two cases:
Note that since T has at least two nodes, L is not the root of T, and hence L has a parent
P in T. In case (a), P has degree 1 in T, and hence it’s a leaf of T 0 . Thus, l(T ) = l(T 0 ) and
d(T ) = d(T 0 ). So in this case, we have l(T ) = d(T ) + 1.
In case (b), P has degree 2 in T, and hence degree 1 in T 0 . Thus l(T ) = l(T 0 ) + 1 and
d(T ) = d(T 0 ) + 1. So by the induction hypothesis, we have that
3. Problem 5.5, page 162. Write a function named search that takes as input a binary tree (not
a BST!) and a value K, and returns true if value K appears in the tree and false otherwise.
Your function should have the following prototype.
4. Problem 5.7, page 162. Define the internal path length for a tree as the sum of the depths of
all internal nodes, while the external path length is the sum of the depths of all the leaves.
Prove by induction that if T is a full binary tree with n internal nodes, I is T ’s internal path
length, and E is T ’s external path length, then E = I + 2n for all n ≥ 0.
We use induction on n, the number of internal nodes in the binary tree. If T is any binary
tree, we use the notation I(T ) and E(T ) to denote the internal and external path lengths of
T, respectively.
Data Structures and Algorithms, Key to Homework 6 4
Base case. If a binary tree T has no internal nodes, it can either be empty, or it can have
a single node, the root. In either case, E(T ) = I(T ) = 0, and we see that for this case
E(T ) = I(T ) + 2n.
Induction hypothesis. Suppose then that n0 is a nonegative integer with the property that any
full binary tree T with n = n0 (≥ 0) internal nodes satisfies the formula E(T ) = I(T ) + 2n.
Induction step. Suppose now that T is a full binary tree with n = n0 + 1 ≥ 1 internal nodes.
As discussed in class, T contains at least one internal node P, both of whose children, L and
L0 , are leaves. Consider the tree T 0 obtained from T by deleting L and L0 from T. Then, in
T 0 the node P is a leaf. So T 0 contains n = n0 internal nodes. Furthermore, if Q is any node
of T 0 different from P, the children of Q in T 0 are the same as the children of Q in T. Hence
T 0 is full, and we can apply the induction hypothesis to conclude that E(T 0 ) = I(T 0 ) + 2n0 .
If d(Q) denotes the depth of a node of T, we know that d(L) = d(L0 ) = d(P ) + 1. Thus, we
have that
So we have that