300 150 40 80 450 600 550 200 800 20
------nancy----------------
/ \
-fred- ---susan-------
/ \ / \
bonnie george peter zachary
/ \ \ /
charles nick sarah yifan
/ /
rebecca xavier
/
ralph
-300-
/ \
175 500
/ / \
100 400 600
/ / \
50 350 450
--100---
/ \
40 200
\ / \
60 150 400
/ \
125 175
we end up with the following binary search tree that is not a valid AVL
tree and hence needs to be re-balanced:
--100---
/ \
40 200
/ \
150 400
/ \
125 175
int sum(int numbers[], int start, int end) {
1) int middle = (start+end)/2;
2) return sum(numbers, start, middle) + sum(numbers, middle + 1, end);
}
Answer the following questions about this function:
Euclid's algorithm is a recursive algorithm for finding the gcd of two integers a and b that can be written as the following C++ function:
int gcd(int a, int b) {
1) if (a == b) return a;
2) else if (a < b) return gcd(a, b-a);
3) else return gcd(a-b, b);
}
Suppose I have the following main function:
int main() {
1) int x = 48;
2) int y = 20;
3) cout << gcd(x, y) << endl;
}
In the stack diagram shown below, complete the stack frames
that exist when
the series of recursive calls finally arrives at the base case for
gcd.
|----------------------------------------| | main: | | line: 3 | | x: 48 | | y: 20 | |----------------------------------------| | | | | | | | | | | |----------------------------------------| | | | | | | | | | | |----------------------------------------| | | | | | | | | | | |----------------------------------------| | | | | | | | | | | |----------------------------------------| | | | | | | | | | | |----------------------------------------| | | | | | | | | |----------------------------------------|
int i, j;
int sum = 0;
for (i = 0; i < n*n; i++)
sum += i;
for (j = 0; j < n/2; j++)
sum *= j;
|
for (year = 0; year < 2000; year++) {
for (day = 0; day < 365; day++) {
if (n == year % day) {
printf("year = %d and day = %d\n");
}
}
}
|
|
In the following code, assume that the function f is O(n2)
int i, j;
int sum_f = 0, sum_loops = 0;
for (i = 0; i < n; i++) {
sum += f(i);
}
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
sum += i * j;
}
}
if (sum_f > sum_loops)
cout << sum_f << endl;
else
cout << sum_loops << endl;
|
For each fragment of code, please circle its Big-O running time:
a O(1) O(log n) O(n) O(n log n) O(n2) O(n3) O(2n) b O(1) O(log n) O(n) O(n log n) O(n2) O(n3) O(2n) c O(1) O(log n) O(n) O(n log n) O(n2) O(n3) O(2n) d O(1) O(log n) O(n) O(n log n) O(n2) O(n3) O(2n)
a. array
b. vector
c. stack
d. deque
e. hash table
f. list
g. binary search tree
h. AVL tree
For each of the following questions choose the best answer from the above list. Assume that the size of an array is fixed once it is created, and that its size cannot be changed thereafter. Sometimes it may seem as though two or more choices would be equally good. In those cases think about the operations that the data structures support and choose the data structure whose operations are best suited for the problem. You may have to use the same answer for more than one question: