You are on page 1of 2

The program demonstrates the algorithm for a balanced BST created from a sorted array using 3

different arrays. The creation of the balanced BST relies on a recursive balanced insert function that
takes the start and end elements of the array, determines the median, inserts the median, and then calls
itself twice. First on the range of the starting element to the median – 1 and then on the range of the
median + 1 to the end element. It repeats this process until the distance between the start and end
elements is less than 2, at which point it will insert the remaining elements.
The program also demonstrates the find level algorithm for finding the level of a node in the BST. This
algorithm is reminiscent of the recursive search function. The public function calls the private version of
the function with the root node as the parameter. The private function determines if the target is less
than or greater than the current node’s data. If the data is either greater or lesser than the target, the
function returns 1 + the result of a recursive call of itself against either the left or right child node. If the
target does equal the current node’s data, then the function returns 0. If the node is null, the function
was not able to find the target in tree and throws an error.

You might also like