Solution

advertisement
CmSc 250 Fundamentals of Computing III
Homework 06 - SOLUTIONS
1. A complete binary tree is a tree is completely filled with the possible exception of the bottom
level, which is filled from left to right.
Example:
Answer the following questions and explain your answers :
a. What is the height of a complete binary tree with 1250 nodes?
The height of a tree is equal to the length of the longest path from the root to a leaf. The
height is also equal to the number of levels in the tree.
A perfect binary tree, i.e. a tree where all rows are filled with nodes, the relation between
the number of the nodes and the number of levels is as follows:
A tree with M levels has exactly 2(M+1) -1 nodes.
A tree with N nodes has exactly log(N+1) -1 levels
(Draw a perfect tree, count the nodes and the levels and you will see that this is the relation)
If the tree is complete, but not perfect, this means that the last row is not entirely filled , i.e.
N < 2(M+1) -1, and for the levels M we have M = ceiling(log(N+1)) -1 = log(2(M+1) ) -1, where
2(M+1) is the smallest power of 2 such that 2(M+1) > N
In the problem N is 1250, the smallest power of 2 greater than 1250 is 2048 = 211.
Hence M+1 = 11, i.e. M = 10
Another way to solve the problem is to find the largest power of 2 that is smaller than N:
210 = 1024 < 1250, M = 10
1
b. How many nodes can be added to a complete binary tree with 1050 nodes without increasing
its height?
A complete binary tree with 1050 nodes has 10 levels (see the previous problem about how to
compute the levels)
A perfect tree with m levels will have 2(M+1) -1 nodes. In the problem M = 10, so 2(M+1) -1 = 211 -1
= 2048 – 1 = 2047. So we can have 2047 nodes in a tree of height 10, i.e. we can increase the
nodes with 2047 – 1050 = 997 nodes keeping same height.
c. How many nodes would have depth equal to the tree height in a complete binary tree with 1300
nodes?
The depth of a node is equal to its distance to the root. Since the height is defined as the longest
path from the root to a leaf, this means that the nodes at the bottom will have depth equal to the
height of the tree. So we have to compute how many nodes are there at the last row of the tree.
The tree with 1300 nodes has 10 levels (see previous problem about how to determine the levels)
If we consider the tree without its last row, it will be a perfect tree with 9 levels and hence it will
have 210 -1 = 1024 – 1 = 1023 nodes . Therefore there are 1300 – 1023 = 277 nodes at the last
row.
2. Write a piece of code with complexity O(N).
// finding the sum of all integers from 1 to N
sum = 0;
for(k = 1; k < =N; k++)
sum = sum + k;
3. Write a piece of code with complexity O(logN)
// finding the ceiling of log(N)
log = 0;
for(k = N; k >0; k=k/2)
log++;
4. Show how to build a binary heap given the following array: 17, 2, 15, 6, 8, 3 (smaller value
means higher priority). Treat the array as a heap with order property violated, and then do the
necessary operations to fix the order property.
indexes
1
17
2
2
3
15
4
6
5
8
6
3
2
In this problem N = 6
Start with the first node at height 1 : the node at index 3 (this is 15) and compare it with
its child – the node at index 6 (this is 3). Since 15 > 3 we have to swap the values:
indexes
1
17
2
2
3
3
4
6
5
8
6
15
Next node to the left is 2 at position 2. Compare it with the smaller of its children – the
node at position 4 with value 6. Since 2 < 6, no reordering is necessary.
Next node to the left is 17 at position 1. Compare it with the smaller of its children – the
node at position 2 with value 2. Since 2 < 17, the child 2 has to be moved to position 1:
indexes
1
2
2
3
3
4
6
5
8
6
15
Before inserting 17 at position 2, we have to compare it with the smaller of the children
of position 2, i.e. with the smaller of the values at positions 4 and 5. This is 6. Since 17 >
6 we have to move 6 at position 2:
indexes
1
2
2
6
3
3
4
5
8
6
15
5
8
6
15
Position 4 does not have children, so we can put 17 there
indexes
1
2
2
6
3
3
4
17
5. Solve the following recurrence relations, with initial condition T(1) = 1
a. T(N) = T(N/2) + LogN
Telescoping:
T(N) = T(N/2) + LogN
T(N/2) = T(N/4) + Log(N/2)
T(N/4) = T(N/8) + Log(N/4)
……
T(8) = T(4) + Log8
T(4) = T(2) + Log4
T(2) = T(1) + Log2
Adding the left and the right sides and crossing the equal terms:
3
T(N) = T(1) + Log2 + Log4 + Log8 + …. Log N = 1 + 1 + 2 + 3 + …. + LogN
The sum 1 + 2 + 3 + … logN = logN ( logN + 1)/2
Hence T(N) = 1 + logN ( logN + 1)/2
b. T(N) = T(N/2) + N
Telescoping
T(N) = T(N/2) + N
T(N/2) = T(N/4) + N/2
T(N/4) = T(N/8) + N/4
……
T(8) = T(4) + 8
T(4) = T(2) + 4
T(2) = T(1) + 2
Adding the left and the right sides and crossing the equal terms:
T(N) = T(1) + 2 + 4 + 8 + …. N = 1 + 2 + 4 + 8 +…. + N
The sum 1 + 2 + 4 + 8 + …. + 2k = 2 k + 1 - 1
We use the equality N = 2 logN
Therefore, 1 + 2 + 4 + 8 +…. + N = 1 + 2 + 4 + 8 +…. + 2 logN = 2 logN + 1 – 1 = 2* 2 logN – 1 =
2N – 1
Thus the solution is: T(N) = 2N – 1
c. T(N) = 2T(N/2) + N
Before telescoping, we divide both sides by N
T(N) / N = 2T(N/2) / N + 1
We move ‘2’ down in the denominator of the first term on the right:
T(N) / N = T(N/2) / (N /2) + 1
Note, that we use this technique – dividing by N, only when there is a coefficient ‘2’ in front of
T(N/2), so that we can represent it as T(N/2) / (N/2) . If this coefficient is not present (e.g. the
recurrence relation is T(N) = T(N/2) + 1) we would not divide by N.
Telescoping:
T(N) / N
= T(N/2) / (N /2) + 1
T(N/2) / (N/2) = T(N/4) / (N /4) + 1
T(N/4) / (N/4) = T(N/8) / (N /8) + 1
4
……………..
T(8) / 8 = T(4) / 4 + 1
T(4) / 4 = T(2) / 2 + 1
T(2) / 2 = T(1) / 1 + 1
Adding the left and the right sides and crossing equal terms:
T(N)/N = T(1)/1 + 1 + 1 + … + 1
The number of relations is logN, therefore
T(N)/N = T(1)/1 + LogN = 1 + LogN
T(N) = N + NlogN
5
Download