Question: Suppose you wanted to take a sorted array A and build a balanced Binary Search Tree out of it . How would you do that?
Suppose you wanted to take a sorted array A and build a balanced Binary
Search Tree out of it How would you do that?
First try building a balanced BST from the following sorted array A by
hand:
A
Hint: The root of your balanced tree will be
Suppose we define a TNode as follows:
typedef struct TNode
int data;
TNode pLeft; left child of this node
TNode pRight; right child of this node
TNode;
Now write a recursive algorithm to solve this problem in the general case:
Precondition: A is a sorted array
This function creates and returns a balanced BST
containing the numbers in Astart to Aend
TNode createTreeRec int A int start, int end
Hint: There is an On time algorithm to build a balanced tree from a sorted
array
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
