Skip over navigation

Binary Search in Trees

Problems 1

Building a Binary Search Tree

Searching a Binary Search Tree

Problem : Write a function that will perform a binary search on a sorted array of integers.

We will provide two solutions, one iterative and one recursive. The return value from both is the index in the original array. If the element is not present in the array, the sharp-defined value ~NOT_FOUND~ is returned.

int bin_search (int arr[], int n, int val)
{
	/* n indicates the number of cells in the array */

	int low, high, mid;

	low = 0;

	/* Set high to be the highest array index. */ 
	high = n - 1;

	while (high >= low) {

		/* Begin searching in the middle */
		mid = (low + high) / 2;

		/* Check if you have found it or adjust the range accordingly */
		if (arr[mid] == val) {
			return mid;
		} else if (arr[mid] > val) {
			high = mid - 1;
		} else {
			low = mid + 1;
		}
	}

	return NOT_FOUND;
}
Now for the recursive one. The basic idea it that it keeps applying the same algorithm on the reduced range. The tricky part is offsetting the return value.

int bin_search (int arr[], int n, int val)
{
	int mid;

	if (n == 0) return NOT_FOUND;

	if (n == 1) return (arr[0] == val? 0 : NOT_FOUND);

	mid = (n - 1) / 2;

	/* Check if you have found it or adjust the range accordingly */
	if (arr[mid] == val) {
		return mid;
	} else if (arr[mid] > val) {
		return mid + bin_search (&arr[mid + 1], n / 2, val);
	} else {
		return mid + bin_search (&arr[mid - 1], (n - 1) / 2, val);
	}
}

Problem : Assume now that we modify the definition of a binary search tree slightly. All of the data in a left subtree must precede the data in the current node, but all of the data in the right subtree must only be greater than or equal to the data in the root node (as opposed to exclusively greater than). Write a function that will take a new binary search tree and return 1 or 0 for whether it contains any duplicates.

In order to check for duplicates, it suffices to check if the root of the right subtree has the same data element as the parent.

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

	if (t->right == NULL) return 0;

	if (t->data == t->right)
		return 1;
	else
		return duplicates(t->left) || duplicates(t->right);
}

Problem : What is the worst case scenario for a binary search tree in terms of the complexity for searching for a data element? What about for adding a new data element?

The worst case for searching is basically one where the binary search tree provides no help in dividing up the data. In other words, it is the case where the tree branches exclusively in one direction. In this case if there are n nodes in the tree, then there is one leaf and it is at depth n - 1. Thus a search in this tree consists of walking through each data element in order until you find the one you want. Hence, the complexity is O(n) . Similarly, adding a new data element means walking along each data element until you find the place to add it; this is also O(n) .

Follow Us