9/19/00 HW #4

advertisement
CSCE 350 Homework Solutions
9/19/00 HW #4
Problems 4.3, 4.4, 4.5, 4.8 & 4.9
DUE 9/28/00
4.3 10 Points
ANS: 4
Notes: The depth of a tree is equal to the depth of the deepest leaf. Node L and node M are the deepest leaves.
4.4 10 Points
PROOF (Using deduction) we use L( N ) to denote the number of null links of a binary tree of N nodes..
For binary tree, each node will have 2 children. Then each node will have 2 links (left and right) which representing
its children. If one link doesn’t connect any child, it is a null link.
 When N = 1, the tree only has one node-the root A and the tree has 2 null links. (See fig4.4 a) Since L(1) = 2, N
+1 is right for N = 1;
A
A
B
NULL
NULL
NULL
Fig. 4.4 a
NULL
NULL
Fig. 4.4 b

When N = 2, we add a new node B as a child of A, then the null links of A are decreased by 1 but we have 2
new null links which are introduced by B. Thus we have 3 null lins. Since L(2) = L(1) –1 + 2 = 3, L(N) = N+1
is right for N = 2.
 Suppose L(N) = N + 1 is right for all N<=K.
 Then when K = K +1, L(K+1)=L(K) – 1 + 2 = K + 1 –1 + 2 = (K+1) + 1. Thus L(N) = N + 1 is right for N = K
+ 1.
So, it is proved that a binary tree of N nodes has N + 1 null links.
4.5 20 Points
PROOF:
 A complete binary tree has the maximum number of nodes. We use NN (h) denote the number of nodes of
a complete binary tree and use NC (h) to denote the number of node at level h. It is known that the
following relations are right for any complete binary tree.
NC (h)  2 * NC (h  1)
NN (h)  NN (h  1)  NC (h)

(1)
(2)
First, we should prove that
NC(h)  2 h
(3)
and
NN (h)  2 h1  1
(4)
are right for h=1 and h=2.
h  0,
NC (1)  1  2 0 ,
h  1,
NC (2)  2 *1  21
(3) and (4) is right for h=1 and h=2.
NN (h)  1  2 01  1;
NN (2)  NN (1)  NC (2)  1  2  3  211  1.

NC (h)  2 h and NN (h)  2 h1  1 are right for h  k , k is any positive integer.
h
If we can prove that these two relation are right for h=k+1, it is proved that NC (h)  2 and
Secondly, suppose
NN (h)  2 h1  1 are right for every h ( h is an positive integer).
NC (k  1)  2 * NC (k )  2 * 2 k  2 ( k 1)
NN (k  1)  NN (k )  NC (k  1)  2 k 1  1  2 k 1  2 ( k 1)1  1
h
h 1
So NC (h)  2 and NN (h)  2
 1 are right for h  k  1.
Thus, it is proved that the maximum number of nodes in a binary tree of height h is
2 h 1  1 .
4.8 30 Points
1) Prefix Expression (10 points)
(Using preorder traversal (node, left, right))
-**ab+cde
2) Infix Expression (10 Points)
(Using inorder traversal (left, node, right))
a*b*(c+d)-e
3) Postfix Expression (10 Points)
(Using postorder traversal (left, right, node))
ab*cd+*e4.9 20 Points
a. 10 Points
b. 10 Points
3
4
4
1
1
2
6
6
2
9
9
5
5
Fig 4.9 b
Fig 4.9 a
7
7
Download