Question: You are given a pointer to the root of a binary search tree and a value to be inserted into the tree. Insert this value

You are given a pointer to the root of a binary search tree and a value to be inserted into the tree. Insert this value into its appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function.

Input Format

You are given a function,

node * insert (node * root ,int value) { } 

node is defined as :

struct node { int data; node * left; node * right; }node; 

Output Format

Return the root of the binary search tree after inserting the value into the tree.

I cannot see what is wrong with my function:

node * insert(node * root, int value) { if (root = NULL) { node* newNode = new node(); newNode->data = value; newNode->left = newNode->right = NULL; root = newNode; } else if(value <= root->data) { root->left = insert(root->left, value); } else { root->right = insert(root->right, value); } return root; }

Please explain where my logic is wrong and any corrections.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!