2019 Final Questions Flashcards

1
Q

T/F: When I insert a value into an AVL tree, I will perform a maximum of two rotations.

A

True. After either a zig-zig or zig-zag imbalance is fixed, the tree is balanced, so you can stop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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).

A

False. This is backward (it’s the definition of big omega)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A

O(n). Inserting into the middle of a deque is the same as inserting into the middle of a vector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Suppose I have an AVL tree with n elements. What is the running time of deleting an element from it.

A

O(log n) – that’s one reason why we like AVL trees.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A

O(n) – it’s an inorder traversal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the big-O expression for 15n^2 + 400n + 5 log(n)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

O(1) – that’s one reason why we like lists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Suppose I have a binary search tree. What kind of traversal do we use to create a sorted vector from the
binary search tree.

A

See part E. It’s an inorder traversal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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)

A

True. The very definition of big O.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Suppose I have a binary search tree with n elements. What is the running time of deleting an element
from it.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the big-O expression for 55n + 4n log(n) + 20 log(n)?

A

O(n log(n)). In a sum of expressions, it’s the big O of the largest expression, with any constant factors removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Suppose I have a tree that represents an arithmetic expression. What kind of traversal do we use to
evaluate/calculate that expression.

A

This is also postorder – you have to calculate the expressions of the children, before you can perform an arithmetic operator on them. Please see the “Example Question” in the lecture notes on trees.

17
Q

T/F: When I delete a value from an AVL tree, I will perform a maximum of two rotations

A

False. Although you only need one rotation to fix a zig-zig and two rotations to fix a zig-zag, when they are fixed, you tree may still be unbalanced, so you have to keep traversing up to the root of the try to check imbalances.

18
Q

Suppose I have a vector with n elements and an iterator to one of its elements. What is the running time
of deleting the element

A

This is O(n).