Suppose you have some data keys sorted in an array and you want to construct a balanced binary search tree from them. Assume a tree node representation TreeNode that includes instance variables key, left, and right.
a) Write pseudocode (or Java if you wish) for an algorithm that constructs the tree and returns the root node. (We won't worry about making the enclosing BinaryTree class instance.) You will need to use methods for making a new TreeNode, and for setting its left and right children.
Hints: First, identify the array location of the key that would have to be the root of the balanced BST. Now think about how BinarySearch works on the array. Which item does it access first in any given subarray it is called with? Using a similar strategy a simple recursive algorithm is possible.
b) What is the Θ cost to construct the tree? How does the expected runtime of BinarySearch on the array compare to the expected runtime of search in the tree you just constructed?
25 freelancers are bidding on average $31 for this job
I have read and fully understood your requirements. Please open chat to discuss further….. =================+++++++++++++++++++++++++==================
Hello there, I teach programming language courses in a university. Lots of experience with algorithms, data structures, OOP and also Java, C/C++/C#, etc. Let me know if you're interested. Cheers!
I have 5 years of experience in Java. I am a professional about data processing - java core system - proof of concept - fast and easy - have more time let’s to discuss first