Download Final

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Quadtree wikipedia , lookup

Array data structure wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
NAME:
EE 322C Fall 2005 (Chase) Final
READ THIS FIRST. Please use the back side of each page for scratch paper. I will not give
credit for illegible answers (and I reserve sole judgment for what is “legible”). The exam is out
of 100 points, not all questions are equally hard or take equally long to complete. Good luck!
1. (10 pts) Inheritance and object oriented programming does not work very well with arrays.
Explain why. For full credit predict what the output is of the following program (and explain).
class B {
public:
int x;
B(void) { x = 1; }
};
class D : public B {
public:
int y;
D(void) : B() { y = 2; }
};
void uhoh(B* p) {
for (int k = 0; k < 10; k += 1) {
cout << p[k].x;
}
}
int main(void) {
D x[10];
uhoh(x);
}
2. (8pts) Consider the following class and indicate the result of each of the following
expressions
class B {
public:
virtual void vf(void) { cout << “B::vf”; }
void f(void) { cout << “B::f”; }
};
class D : public B {
public:
virtual void vf(void) { cout << “D::vf”; }
void f(void) { cout << “D::f”; }
}
int main(void)
B bobj;
D dobj;
B* bptr = &bobj;
D* dptr = &dobj;
B oobj = dobj;
B* ooptr = &dobj;
}
a. bobj.vf();
b. bptr->vf();
c. dptr->f();
d. dptr->vf();
e. ooptr->vf();
f. ooptr->f();
g. oobj.vf();
h. oobj.f();
3. (9pts) I have a red-black tree with n values. All the values are integers. I also have an array
of n integers. The array is sorted.
a. Which is faster, searching for a value in the red-black tree, or searching for a value in the
array. Justify your response. Discuss both time complexity and the raw time required.
b. Assume the values are read from a file, and that they are initially random. Which takes
longer to build, the red-black tree, or the array. Please keep in mind that the array will
have to be sorted one time (after all the values have been read). Discuss both time
complexity and the raw time required.
c. Which is more expensive, adding one new value to the tree or adding one new value to the
array? If I have to insert new values into the data structure on a regular basis, which data
structure should I use? Discuss both time complexity and the raw time required.
4. (4 pts) I have two data structures in my personal library. I have an expandable array called
Array<int> and a binary search tree called Tree<int>. Both data structures have an iterator class
defined for them. However, the iterators have a subtly different API. The return type for
Array<int>::iterator::operator* is int&. However, the return type for
Tree<int>::iterator::operator* is int (not a reference). Why does operator* have a different return
type on these two classes.
5. (15 pts) The class SList is a “null-terminated, singly-linked list of int”. Write an iterator for
this class. You should write operators for *, ++ and = = for your iterator and functions begin()
and end() for creating iterators. You should write the entire class for your iterator type, and
include at least one constructor. You do not need to write any other operators for the iterator.
The SList class has the following properties: If the list is empty, then both head and tail are zero
(i.e., null). If the list has one element, then both head and tail point to the same Link object. If
tail is not zero, then tail->next is zero (i.e., tail points to the last element in the list). head
always points to the first element.
struct Link {
Link* next;
into value;
};
class SList {
Link* head;
Link* tail;
public:
6. (3 pts) On April Fools day, I snuck into Microsoft and removed all the destructors from the
source code to Microsoft Word. What do you think would be the most likely result of my little
prank? Justify your answer.
a. Word would continue to function normally
b. Word would fail to compile
c. Word would compile, but would crash immediately upon starting
d. Word would compile and run, but would eventually run out of stack space
e. Word would compile and run, but would eventually run out of heap space
7. (3 pts) A “rule of thumb” frequently quoted in the C++ text books is, “If you write a
desctructor, you should also write both a copy constructor and an assignment operator”. Why?
(hint: the answer is not because “every class should have these functions”).
8. (6 pts) Consider the following class
class Foo {
private:
int x;
public:
Foo& max(Foo& a, Foo& b) {
if (b.x > a.x) { return b; }
else { return a; }
}
Foo(int x) { this->x = x; }
};
a. Someone told me that max would run faster if it were static. Is this true?
b. Is it even legal to make max static?
9. (8 pts) Write a template function that has two parameters. The parameters are iterators.
Return the number of elements contained in the range described by the two iterators. For
example, if the iterators were the begin and end of a data structure, then your function should
return the number of elements in the data structure. Use only operators *, ++ and = = in your
solution.
10. (8 pts) In all the data structures we’ve talked about this semester, there’s only two functions
that can be written for each data structure and that runs in O(1) time (consider “amortized
O(1) time to be the same thing as O(1) for this question). Which functions are they? Note: it
might be necessary to add a data member or two in order to implement the function in O(1)
time. Draw an X through those functions that cannot be implemented in O(1) time for at
least one data structure. Put an O around the two functions that can be implemented in O(1)
time on all of the data structures.
a. push_back – add an element to the end of the data structure
b. find – search for a specific value in the data structure
c. pop_back – remove the last element from the data structure
d. size – return the number of elements in the data structure
e. begin() – return an iterator that points to the first element in the data structure
f. end() – return an iterator that points after the last element in the data structure
11. (4 pts) Does it make sense to sort the elements in a hash table? Why or why not?
12. (10 pts) In EE312 we implemented a Set data structure. We built our Set on top of an
expandable array (e.g., a vector). We kept the elements in the array sorted at all times.
Some people argue that Set data structures should be built using Red-Black trees. Assuming
the operations that can be performed on a Set are insert, find, remove, intersection (returns a
Set) and union (returns a Set) what’s the right answer? Justify your conclusion by
completing the following table. Assuming that there are n elements in the set, what is the
time complexity of each of the following:
Tree-based Set
Insert
Find
Remove
Intersection
Union
Array-based Set
13. Write a function that computes the median value in a binary search tree. To receive credit,
you have to follow the instructions below. The functions should not be member functions.
Assume the BST is built using the following struct. The tree has no duplicated values.
struct TreeNode {
int value;
TreeNode* left;
TreeNode* right;
};
a. (4 pts) Write a function count that returns the number of elements in a binary search tree.
b. (6 pts) Write a function that returns the kth smallest value in a binary search tree (if k is
zero, return the smallest value).
c. (2 pts) Write a function that computes the median value of a binary search tree.