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

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) { /* 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; }

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.

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?

Take a Study Break!