How to handin.
Points : 40
Weight : 3%
Due : Friday June 11, 2004 @ 11:00 PM
Note : Late assignments will be accepted only with the instructor's pre-approval.
vector<int> vec;and that we have filled the vector vec with the following values:
12, 5, 1, 23, 16, 4, 3, 11, 41, 19Given the following recursive function:
int mystery(const vector<int>& x, int n) {
int temp; // local variable
if ( n == 1 ) return x[0];
// n is > 1;
temp = mystery(x, n-1);
if (x[n-1] > temp)
return x[n-1];
else
return temp;
}
Trace the mystery recursive function if the call
cout << mystery(vec, vec.size()) << endl;is used. What is the output? What does the mystery function do?
| f(x) = 5 | if x = 1; | |
| f(x) = x + f(x-3) | if x is odd and x >= 3; | |
| f(x) = x2 + f(x-2) | if x is even and x >= 4; |
Explain why this function is not well defined and add something to it that will make it well-defined.
| log2(n) = 0 | if n = 1; | |
| log2(n) = 1 + log2(n/2) | if n > 1; |
sumsq(2) = 0 + 1 + 4 = 5 sumsq(4) = 0 + 1 + 4 + 9 + 16 = 30
1 3 5 7 9 11 12 15 17What happens when the elements inserted into a binary search tree are already in order? Now create a binary search tree inserting the following elements:
17 15 12 11 9 7 5 3 1What happens when the elements inserted into a binary search tree are in reverse order? How do these orders affect the efficiency of the search and insert algorithms?
F
/ \
/ \
R C
/ \ \
/ \ \
A E O
/ / \ / \
/ / \ / \
B T G L D
F
/ \
/ \
C R
/ / \
/ / \
O E A
/ \ / \ \
/ \ / \ \
D L G T B
[4] Modify the doPrints function in the test program to do the following :
University of Lethbridge Home Page