0% found this document useful (0 votes)
105 views4 pages

Data Structures and Algorithms Key To Homework Assignment 6

The document provides solutions to homework problems related to analyzing algorithms and data structures. It includes: 1. Analyzing the time complexity of different for loop nesting structures and finding the simplest function f(n) such that the number of statements executed is O(f(n)). 2. Proving by induction that the number of degree 2 nodes in a binary tree is one less than the number of leaves. 3. Providing a recursive function to search a binary tree for a given value. 4. Proving by induction that for a full binary tree with n internal nodes, the external path length equals the internal path length plus 2n.

Uploaded by

richa2628
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views4 pages

Data Structures and Algorithms Key To Homework Assignment 6

The document provides solutions to homework problems related to analyzing algorithms and data structures. It includes: 1. Analyzing the time complexity of different for loop nesting structures and finding the simplest function f(n) such that the number of statements executed is O(f(n)). 2. Proving by induction that the number of degree 2 nodes in a binary tree is one less than the number of leaves. 3. Providing a recursive function to search a binary tree for a given value. 4. Proving by induction that for a full binary tree with n internal nodes, the external path length equals the internal path length plus 2n.

Uploaded by

richa2628
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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 Θ(n). So f (n) = n2 .


(c) sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n*n; j++)
sum++;

The inner loop is Θ(n2 ). So f (n) = n3 .


(d) sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < i; 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 is Θ(j). So the for j loop is


2 
iX−1
!
i2 (i2 − 1)
Θ j = Θ ,
j=0
2

which is the same as Θ(i4 ). So by the fact that


n
X
tk is O(tk+1 ),
t=1

we can conclude that f (n) = n5 .


(f) sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i*i; j++)
if (j % i == 0)
for (int k = 0; k < j; k++)
sum++;

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

which is Θ(i3 ). So by the result on sums of powers of i, we see that f (n) = n4 .

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:

(a) L has no sibling in T, and


(b) L has a sibling in T.

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

l(T ) = l(T 0 ) + 1 = d(T 0 ) + 1 + 1 = d(T ) + 1.

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.

boolean search(BinNode rt, int K)

private boolean search(BinNode currNode, int key) {


if (currNode == null)
return false;
Elem currElem = (Elem) currNode.element();
if (currElem.key() == key)
return true;
boolean hasKey = search(currNode.left(), key);
if (hasKey)
return true;
else
return search(currNode.right(), key);
} // search

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

E(T ) = E(T 0 ) − d(P ) + 2d(L) and


I(T ) = I(T 0 ) + d(P ).

So we have that

E(T ) = I(T 0 ) + 2n0 − d(P ) + 2d(L)


= I(T ) − d(P ) + 2n0 − d(P ) + 2d(L)
= I(T ) + 2n0 − 2d(P ) + 2d(P ) + 2
= I(T ) + 2(n0 + 1).

You might also like