Skip over navigation

Contents

Review of Trees

Review Test

Table of Contents

How to Cite This SparkNote

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:


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;
}


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;
}

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

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?

Consider the following definition of tree_t:


typedef struct _tree {
	char *s;
	struct _tree *left, *right;
}
and consider the following freeing function:

void free_tree (tree_t *t)
{
	if (t == NULL)  return;

	free_tree(t->left);
	free_tree(t->right);
	free(t);
}
Which of the following is true:

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);
}

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

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.

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

Figure %: Question 10

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

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?

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?

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;
}

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

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

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?

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?

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));
}

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.

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.

Mystery function:


void g(tree_t *t)
{
	tree_t *t1;
	if (t == NULL) return;

	g(t->left);
	g(t->right);
	t1 = t->left;
	t->left = t->right;
	t->right = t1;
}

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?

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

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

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.

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

Mystery Function:


int g(tree_t *t)
{
	if (t == NULL) return 1;

	return (t->data > t->left->data) && (t->data < t->right->data) &&
		g(t->left) && g(t->right);
}

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.

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?


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:

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

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

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


int g(tree_t *t)
{
	if (t == NULL) return 0;

	return ((t->left == NULL && !( t->right == NULL)) || 
		(!(t->left == NULL) && (t->right == NULL))? 1 : 0) +
		g(t->left) + g(t->right);
}
This function:

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

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

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?

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

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

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

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

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

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

Figure %: Question 44

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

Figure %: Question 45

Figure %: Questions 46-50

Which is a heap?

Which is a binary search tree?

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

Which is complete?

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

Follow Us