This page requires JavaScript, which you
don't seem to have. Please try a different browser.

Scroll through the page to review your answers. The correct answer is
highlighted in
green.
Your incorrect answers (if any) are highlighted in
red.
If you'd like to take the test over again, click the reset button at the end of the test.

For the following question, consider the following mystery function f:

int f (tree_t *t)
{
int n1, n2;
if (t == NULL) return 0;
n1 = f(t->left);
n2 = f(t->right);
if (n1 > n2) {
return (n1 > t->data? n1 : t->data);
} else {
return (n1 > t->data? n1 : t->data);
}
}

Which of the following is true:

(A)
This function determines whether the node with the greatest integer is in
the left subtree or the right subtree and returns 0 or 1 respectively.

(B)
This function returns the maximum data element only if the tree passed is a
binary search tree.

(C)
This function returns the maximum data element from any tree.

(D)
This function returns the maximum element from any tree assuming that all
elements in the tree are >= 0.

int g (tree_t *t)
{
int n1, n2;
if (t == NULL) return 0;
n1 = f(t->left);
n2 = f(t->right);
return n1 + n2 + 1;
}

(A)
This function determines whether the node with the greatest integer is in the
left subtree or the right subtree and returns 0 or 1 respectively.

(B)
This function returns the sum of data elements only if the tree passed is a
binary search tree.

(C)
This function returns the sum of data elements in any tree.

(D)
This function counts the number of nodes in a tree.

int h (tree_t *t)
{
int n1, n2;
if (t == NULL) return 0;
n1 = f(t->left);
n2 = f(t->right);
return n1 + n2 + t->data;
}

(A)
This function returns the sum of the data elements in any tree where all of
the data elements must be >=0.

(B)
This function returns the sum of data elements only if the tree passed is a
binary search tree.

(C)
This function returns the sum of data elements in any tree.

(D)
This function counts the number of nodes in a tree.

How many nodes will there be in a perfect tree of depth 6, where the root is at depth 0?

(A)
63

(B)
128

(C)
127

(D)
255

(E)
511

What is the range of possible numbers of nodes in a complete tree where
the greatest depth of the tree is 5 where the root node is at depth zero?

(A)
You may not have freed all of the memory associated with the tree.

(B)
You could dereference a null pointer in the case of a leaf.

(C)
This is a pre-order traversal of the tree.

Recall that a heap is a sort of tree where the data element in a
node is greater than the data elements in all of its descendants.
What does the following function do when passed a heap as an argument?

void i (tree_t *t)
{
if (t == NULL) return;
printf(" %d ", t->data);
i (t->left);
i (t->right);
}

(A)
It prints out all of the data in sorted order.

(B)
It prints out all of the data in the tree.

(C)
It prints out all of the data in the tree except the leaves.

Consider the following algorithm:
Step 1: Stop if the root is larger than both children
Step 2: Switch the value in the root with the value of the larger child
Step 3: Perform this algorithm on the child that is now rooted by the
original root

(A)
This algorithm will take any tree and convert it to a heap

(B)
This algorithm will stop if the original tree is not a heap

(C)
If the root node is less than all non-leaves, then when the algorithm
finishes, it will be in a leaf position.

(D)
This function will never finish.

What does the following algorithm do when applied to a heap?
Step 1: Remove the largest element of the tree and print it
Step 2: Replace it with the last leaf on the tree (rightmost leaf at the
greatest depth).
Step 3: Perform the algorithm from the previous problem
Step 4: Go back to step one if there are still nodes in the tree.

(A)
This algorithm will print the data in sorted order.

(B)
This algorithm will leave the heap unchanged.

(C)
This algorithm prints the data in a random order.

Which binary search tree is more effective in an average binary search?

Figure %: Question 10

(A)
Picture 1

(B)
Picture 2

(C)
They are equally as effective.

(D)
Impossible to determine.

Which binary search tree requires fewer steps on average to add a new
element to the tree over the set of integers by adding a new
element into a leaf position?

Figure %: Question 11

(A)
Picture 1

(B)
Picture 2

(C)
They are equally as effective.

(D)
Impossible to determine.

In a tree where all of the nodes have a null left subtree pointer and there
are n nodes in the tree, what is the complexity of a search?

(A)
O(1) (constant time)

(B)
O(log n)

(C)
O(n)

(D)
O(
n^{2}
)

In a tree where all of the nodes have a null left subtree pointer and there
are n nodes in the tree, what is the complexity of an insertion into a leaf
position, assuming the insertion function is passed only a pointer to the
root node?

(A)
O(1) (constant time)

(B)
O(log n)

(C)
O(n)

(D)
O(
n^{2}
)

Which statement is true about the following function:

tree_t* b (tree_t *t, int *arr, int n)
{
int i;
tree_t *n_t, *t;
if (n == 0) return NULL;
t = new_tree (arr[0]);
for (i = 1; i < n; i++) {
n_t = new_tree(arr[i]);
if (arr[i] > t->data)
n_t ->left = t;
else
n_t->right = t;
t = n_t;
}
return t;
}

(A)
This function creates a binary search tree from the data in the array
arr.

(B)
This function could dereference a null pointer.

(C)
This function does a binary search in the passed array.

What is the average number of operations required to find the maximum
element from a tree with n nodes?

(A)
log n

(B)
n

(C)
n^2

(D)
2^n

What is the average number of operations required to find the maximum
in a binary search tree?

(A)
log n

(B)
n

(C)
n^2

(D)
2^n

About how many words would you predict that you would need to check in the
dictionary applying a binary search for a word assuming that there are 12,000
words in the English language?

(A)
5

(B)
10

(C)
100

(D)
6,000

If there are exactly 27 elements to be searched using a binary search, and
the elements are ordered, which of the following would require the fewest
number of steps to find?

(A)
1st

(B)
7th

(C)
15th

(D)
26th

Mystery function:

int g (tree_t *t1, tree_t *t2)
{
if (t1 == NULL || t2 == NULL) return (t1 == t2);
return (g(t1->left, t2->left) && g(t1->right, t2->right));
}

(A)
The above function checks if two trees contain the same data, although not
necessarily in the same location in the tree.

(B)
The above function checks if two trees contain the same data in the same
location in the tree.

(C)
The above function returns true only if the two trees exist at the same
memory addresses.

(D)
The above function returns true only if the two trees are of the same shape.

True/False

int g (tree_t *t1, tree_t *t2)
{
if (t1 == NULL || t2 == NULL) return (t1 == t2);
return (g(t1->left, t2->left) && g(t1->right, t2->right) &&
(t1->data == t2->data));
}

The above function will return true when passed two binary search trees
that contain all of the same data elements.

(A)
True

(B)
False

True/False
A binary search performed on a sorted array will take n/log n times
longer than one performed on a binary search tree.

(A)
This function fails to change the tree at all because it only modifies the
local copies of the variable, but not the allocated memory.

(B)
This function cannot change the tree because its return type is void.

(C)
This function results in the tree being its mirror image when finished.

(D)
This function reduce the number of nodes in the tree by 1 after all of the
recursive calls are done.

Recall that a Huffman tree encodes characters. All of its leaves have a
single character associated with them and the encoding for each is the
sequence of 1's and 0's that corresponds to following left and right
branches to walk from the root node to a particular leaf. If ASCII
characters all require 8 bits, then how many bits are saved encoding the word
"Mississippi" if m has depth 12, i has depth 7, s has depth 7, and p has
depth 8 in a Huffman tree where the root is at depth 0?

(A)
-4

(B)
0

(C)
4

(D)
8

What is the complexity of removing an element from a binary search tree?

(A)
1

(B)
log n

(C)
(log n)^2

(D)
n

True/False
A binary search tree can be searched in constant time if it is
implemented using an array.

(A)
True

(B)
False

True/False
By using the opposite methodology used to implement a tree in an array, you
can implement an array using a classic implementation of a tree without
compromising random access speed into the array.

(A)
True

(B)
False

True/False
Comparing two binary search trees, each with <= n nodes to see if they
contain the same elements is O(n^2).

(A)
This function could dereference a null pointer.

(B)
This function returns true if the tree is a binary search tree.

(C)
This function returns true if for each node with a child, the left node is
less than its data and the right node is greater than its data and will not
ever dereference a null pointer.

One starts at the node of a tree and travels left or right with equal
probability if 2 branches exist. Otherwise one simply goes along the
branch that exists. The tree has all leaves at a depth of n.

(A)
There is equal probability that you will end up in any one of the leaves.

(B)
There is a greater likelihood that you will end up at the centermost leaf.

(C)
There is not enough information to predict whether all leaves have an equal
likelihood.

You follow the same procedure as in question the last question, except that
the tree is complete with leaves at depth n and n - 1. There twice as many
leaves at depth n as at depth n - 1. Which of the following is true?

(A)
There is equal likelihood of ending up at depth n as there is at depth n - 1.

(B)
There is twice the likelihood of ending up at depth n as there is at depth
n - 1.

(C)
There is half the likelihood of ending up at depth n as there is of ending
up at depth n - 1.

(D)
There is insufficient information to determine any of the above.

void g(tree_t *t1, tree_t *t2)
{
if (t2 == NULL) return;
t3 = t1;
while (TRUE) {
t4 = t3;
if (t2->data > t3-data) {
if (t3->right == NULL) {
t3->right = new_tree(t2->data);
break;
} else {
t3 = t3->right;
}
} else {
if (t3->left == NULL) {
t3->left = new_tree(t2->data);
break;
} else {
t3 = t3->left;
}
}
}
g(t1, t2->left);
g(t1, t2->data);
}

When passed two non-empty binary search trees, this function will:

(A)
Have no lasting effect on the trees once the function is done.

(B)
Add all of the elements from the first tree to the second tree.

(C)
Add all of the elements from the second tree to the first tree.

(D)
Destroy the second tree.

True/False:
The function from the previous problem will have the same effect if the
first tree were any non-empty tree.

(A)
True

(B)
False

True/False
The function from the previous problem will have the same effect if the
second tree were any non-empty tree.

(A)
True

(B)
False

True/False
A balanced tree where all the leaves at the greatest depth are leftmost
in the tree is always complete.

(C)
Counts the number of nodes that branch exactly one way.

(D)
Counts the number of branching nodes.

True/False
The maximum depth and the number of leaves are sufficient information to
calculate the number of nodes in the tree.

(A)
True

(B)
False

If a change is defined to be switching the left and the right pointer in
a given node, how many changes would it require to make this tree complete.

Figure %: Question 37

(A)
0

(B)
1

(C)
2

(D)
3

If a binary search tree branches only to the right and has depth n, how much
worse is an average search in this tree than in a complete binary search
tree?

(A)
O(n)

(B)
O(log (n))

(C)
O(log (n - log (n)))

(D)
O(n - log (n))

We can use a tree to track the possible moves that pieces can make in
various games. Assume we have a tree where the nodes contain a location
on a chess board and the children represent the possible locations that
can be moved into from the parent location by a knight. If the root node
has a corner location, how many nodes are there at depth 2 (where the root
is at depth 0)?

(A)
1

(B)
6

(C)
11

(D)
12

How many distinct locations could a knight be in after two moves when it
begins in the corner of a chess board?

(A)
3

(B)
10

(C)
11

(D)
12

Let us define the "evaluation of a tree" such that a tree evaluates to a
number according to the rule that the operands for any operation are the
numbers that the two subtrees resolve to. If a tree consists of one node,
that node will contain only the number it resolves to.
What does the following tree resolve to:

Figure %: Question 41

(A)
10/3

(B)
26/3

(C)
16/3

(D)
48

If the following is the result of a pre-order traversal of such a tree, what
does that tree evaluate to?
*/102*102

(A)
1

(B)
10

(C)
20

(D)
100

If the following is the result of a post-order traversal of such a tree,
what does that tree resolve to?
1234* + /5*

(A)
5/14

(B)
33

(C)
80

(D)
140

Would the infix traversal of the following tree require parentheses to be
evaluated correctly?

Figure %: Question 44

(A)
Yes

(B)
No

Would the infix traversal of the following tree require parentheses to be
evaluated correctly?

Figure %: Question 45

(A)
Yes

(B)
No

Figure %: Questions 46-50

Which is a heap?

(A)
A

(B)
B

(C)
C

(D)
D

Which is a binary search tree?

(A)
A

(B)
B

(C)
C

(D)
D

If you switch the left and the right pointers at each node in the trees,
which would result in a binary search tree?

(A)
A

(B)
B

(C)
C

(D)
D

Which is complete?

(A)
A

(B)
B

(C)
C

(D)
D

To which could you add one node and have a balanced tree?