Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) I have a binary search tree class and i need to create a Constructor. Firs

ID: 3820369 • Letter: #

Question

(C++) I have a binary search tree class and i need to create a Constructor. First parameter is an ordered array of elements and the second parameter is the number of elements in the array. The tree should be built in a balanced fashion, with odd elements going to the left when necessary

class BST
{
   struct node
   {
       int val;
       node* left;
       node* right;
   };
private:
   node* root;
   node* curr;
   int size;
public:
   BST();           //Default constructor, creates an empty tree.
   BST(int[], int);
   void insert(int);

}

Explanation / Answer

From you question it seems that you have implemented the insert function and If you have implemented the insert function then simply call insert on each element of the array as below

BST(int arr[], int n);
{

for(int i=0; i<n; i++)
this.insert(arr[i]);

}