The goal of this assignment is to reinforce the tree data st
The goal of this assignment is to reinforce the tree data structure in C++. Specifically, the assignment is to do the following: Binary search trees have their best performance when they are balanced, which means that at each node, n, the height of the left subtree of n is within one of the height of the right subtree of n. Write a function (and a test program) which takes a sorted list of entries and produces a balanced binary search tree.
Solution
#include <iostream>
using namespace std;
/* A Binary Tree node */
 struct TNode
 {
     int data;
     struct TNode* left;
     struct TNode* right;
 };
struct TNode* newNode(int data);
/* A function that constructs Balanced Binary Search Tree from a sorted array */
 struct TNode* sortedArrayToBST(int arr[], int start, int end)
 {
     /* Base Case */
     if (start > end)
       return NULL;
    /* Get the middle element and make it root */
     int mid = (start + end)/2;
     struct TNode *root = newNode(arr[mid]);
    /* Recursively construct the left subtree and make it
        left child of root */
     root->left = sortedArrayToBST(arr, start, mid-1);
    /* Recursively construct the right subtree and make it
        right child of root */
     root->right = sortedArrayToBST(arr, mid+1, end);
    return root;
 }
/* Helper function that allocates a new node with the
    given data and NULL left and right pointers. */
 struct TNode* newNode(int data){
     struct TNode* node = new TNode;
     node->data = data;
     node->left = NULL;
     node->right = NULL;
    return node;
 }
/* A utility function to print preorder traversal of BST */
 void preOrder(struct TNode* node){
     if (node == NULL)
         return;
     cout<<node->data<<\" \";
     preOrder(node->left);
     preOrder(node->right);
 }
/* Driver program to test above functions */
 int main(){
     int arr[] = {1, 2, 3, 4, 5, 6, 7};
     int n = sizeof(arr)/sizeof(arr[0]);
    /* Convert List to BST */
     struct TNode *root = sortedArrayToBST(arr, 0, n-1);
     cout<<\"\  PreOrder Traversal of constructed BST \";
     preOrder(root);
    return 0;
 }


