2019 Final Questions Flashcards
T/F: When I insert a value into an AVL tree, I will perform a maximum of two rotations.
True. After either a zig-zig or zig-zag imbalance is fixed, the tree is balanced, so you can stop.
T/F: a(n) = O(b(n)) if there are positive constants c and n0 such that for all n > n0, ca(n) ≥ b(n).
False. This is backward (it’s the definition of big omega)
Suppose I have a deque with n elements and an iterator to one of its elements. What is the running time
of deleting the element.
O(n). Inserting into the middle of a deque is the same as inserting into the middle of a vector.
Suppose I have an AVL tree with n elements. What is the running time of deleting an element from it.
O(log n) – that’s one reason why we like AVL trees.
Suppose I have a binary search tree with n elements. What is the running time to create a sorted vector
from the binary search tree?
O(n) – it’s an inorder traversal.
What is the big-O expression for 15n^2 + 400n + 5 log(n)
O(n2). In a sum of expressions, it’s the big O of the largest expression, with any constant factors removed. What is the big-O expression for 15n2 + 400n + 5 log(n)
Suppose I have a list with n elements and an iterator to one of its elements. What is the running time of
deleting the element.
O(1) – that’s one reason why we like lists.
Suppose I have a binary search tree. What kind of traversal do we use to create a sorted vector from the
binary search tree.
See part E. It’s an inorder traversal
T/F: If b is a map, and I put the line “a = b[i]” in my method, I can declare the method as const.
False. The associative array feature of a map will insert i if it’s not there. The compiler will exit with an error if you call the method const
T/F: a(n) = O(b(n)) if there are positive constants c and n0 such that for all n > n0, cb(n) ≥ a(n)
True. The very definition of big O.
Suppose I have a binary search tree with n elements. What is the running time of deleting an element
from it.
O(n). The tree doesn’t have to be balanced – if we insert keys from “A” to “Z” in that order, the tree has a depth of 26.
What is the big-O expression for 55n + 4n log(n) + 20 log(n)?
O(n log(n)). In a sum of expressions, it’s the big O of the largest expression, with any constant factors removed.
What is the big-O running time of inserting a value between 0 and n into a multimap with m elements
that are numbers between 0 and n.
O(log m). Inserting into a multimap is O(log x) where x is the number of elements in the multimap. Their values don’t matter.
T/F: If b is a map, and I put the line “a = b.find(i)->second” in my method, I can declare the method
as const.
True, because b is not modified. Now, it’s a different matter as to what happens if i is not in the map, but that’s not what the question is asking.
Suppose I have a binary search tree class. I implement Clear() without declaring any local variables.
What kind of traversal do I have to use?
It has to be a postorder traversal. You’ll have to call a recursive clearing method on the two children, and then you delete the node. Please see Clear() in the lecture notes on binary search trees. It calls a postorder method called recursive_destroy().