Question: PLEASE UPDATE THE CODE ACCORDING TO GIVEN BELOW INSTRUCTION AND SHARE THE SCREENSHOTS. Develop a program to build a BinarySearchTree. The code BinarySearchTree is given
PLEASE UPDATE THE CODE ACCORDING TO GIVEN BELOW INSTRUCTION AND SHARE THE SCREENSHOTS.
Develop a program to build a BinarySearchTree. The code BinarySearchTree is given below the ability to delete a node is missing in the code. In that code, you must write a function that deletes a node. Make a tree with your registration number, for example, if your roll number is F20604001, you must enter 2,0,6,4,1 and create a tree with that. The delete function should remove the last value and the 0 value.
#include
struct TreeNode
{
int value;
TreeNode *left;
TreeNode *right; TreeNode *root=NULL; }; void insertNode(int num)
{
TreeNode *temp= new TreeNode; // Pointer to traverse the tree
TreeNode *nodePtr= new TreeNode; // Create a new node
temp->value = num; temp->left = temp->right = NULL;
if (!root) // Is the tree empty?
root = temp;
else{
nodePtr = root;
while (nodePtr != NULL)
{
else
{
nodePtr->left = temp;
break;
}}
if (num < nodePtr->value)
{
if (nodePtr->left)
nodePtr = nodePtr->left;
Implementation in C++
else if (num > nodePtr->value)
{
if (nodePtr->right)
nodePtr = nodePtr->right;
else
{
nodePtr->right = temp;
break;
}}
else
{
cout << "Duplicate value found in tree. ";
break;
}}}} void displayPreorder(TreeNode *nodePtr)
{
if(nodePtr)
{
cout<
displayPreorder(nodePtr->left);
displayPreorder(nodePtr->right);
}
}
void displayInorder(TreeNode *nodePtr)
{
if(nodePtr)
{
displayInorder(nodePtr->left);
cout<
displayInorder(nodePtr->right);
}
}
void displayPostorder(TreeNode *nodePtr)
{
if(nodePtr)
{
displayPostorder(nodePtr->left);
displayPostorder(nodePtr->right);
cout<
} }
int main(void) {
cout << "Inserting nodes. ";
insertNode(10);
insertNode(6);
insertNode(14);
insertNode(5);
insertNode(8);
insertNode(11);
insertNode(18);
cout << "Done. Inorder Display: "; displayInorder(root);
cout << " Preorder Display: ";
displayPreorder(root);
cout <<" Postorder Display: ";
displayPostorder(root);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
