Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MODIFY THE struct TNode* sortedArrayToBST(int arr[], int start, int end) function to correctly keep track of the number of nodes in each subtree. Its current

MODIFY THE "struct TNode* sortedArrayToBST(int arr[], int start, int end)" function to correctly keep track of the number of nodes in each subtree. Its current implementation does not keep track of the members of the left and right subtree.

/* A Binary Tree node */

struct TNode

{

int data;

struct TNode* left;

struct TNode* right;

int leftSubtreeCount;

int rightSubtreeCount;

};

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 = (struct TNode*)

malloc(sizeof(struct TNode));

node->data = data;

node->left = NULL;

node->right = NULL;

return node;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions