Question

In: Computer Science

7) Given a binary search tree, insert a new node with specific data such that the...

7) Given a binary search tree, insert a new node with specific data such that the binary tree after the insertion is also a binary search tree.

8) Given a binary search tree, delete a node with a specific data value such that the binary tree after the deletion is also a binary search tree.

reply in c++

Solutions

Expert Solution


#include<iostream>
#include<stdlib.h>
using namespace std;
//structure of Binary Search Tree
struct BST
{
int val;
BST *left, *right;
};

//method to create a new node
BST *newNode(int num)
{
//BST *T = (BST *)malloc(sizeof(BST)); //allocate a new node
BST *T = new BST;
T->val = num;
T->left = T->right = NULL;
return T;
}

//inorder traversal
void inorder(BST *root)
{
if (root != NULL)
{ //move left
inorder(root->left);
cout<<" "<<root->val; //print the value
//move right
inorder(root->right);
}
}

//this function will insert the elements into the tree
BST * insert(BST * node, int x)
{
//condition for Empty tree
if (node == NULL) return newNode(x);
  
//if element is less than the tree value then move left
if (x < node->val)
node->left = insert(node->left, x);
//if element is greater than the tree value then move right
else if (x > node->val)
node->right = insert(node->right, x);
//return the node
return node;
}

// Driver Program
int main()
{
  
BST *root = NULL;
int i,n,no;
//ask user about the number of elements
cout<<endl<<"Enter the number of elements of a tree";
cin>>n;//read the number
//enter the value of root node
cout<<endl<<"Enter the value of root node";
cin>>no;
//insert the root node
root = insert(root,no);
//loop to insert the other nodes
for(i=2;i<=n;i++)
{
   cout<<endl<<"Enter a number";
   cin>>no;
   insert(root, no);
   }
// Inorder traversal of tree
inorder(root);
//ask user to input an extra element into the tree
cout<<endl<<"Enter a number to insert an element with the tree";
cin>>no;
//insert the extra element
insert(root, no);
//print the tree after insertion
cout<<endl<<"After insertion of the element the tree is ";
inorder(root);
return 0;
}

output

8)


#include<iostream>
#include<stdlib.h>
using namespace std;
//structure of Binary Search Tree
struct BST
{
int val;
BST *left, *right;
};

//method to create a new node
BST *newNode(int num)
{
//BST *T = (BST *)malloc(sizeof(BST)); //allocate a new node
BST *T = new BST;
T->val = num;
T->left = T->right = NULL;
return T;
}

//inorder traversal
void inorder(BST *root)
{
if (root != NULL)
{ //move left
inorder(root->left);
cout<<" "<<root->val; //print the value
//move right
inorder(root->right);
}
}

//this function will insert the elements into the tree
BST * insert(BST * node, int x)
{
//condition for Empty tree
if (node == NULL) return newNode(x);
  
//if element is less than the tree value then move left
if (x < node->val)
node->left = insert(node->left, x);
//if element is greater than the tree value then move right
else if (x > node->val)
node->right = insert(node->right, x);
//return the node
return node;
}

BST * min(BST * node)
{
BST * current = node;
  
/* loop down to find the leftmost leaf */
while (current && current->left != NULL)
current = current->left;
  
return current;
}
  
//METHOD TO DELETE THE NODE FROM TREE
BST * Delete(BST * root, int x)
{
//CONDITION FOR NULL TREE
if (root == NULL) return root;
  
//IF THE node to be delete is less than root then move left
if (x < root->val)
root->left = Delete(root->left, x);
  
//IF THE node to be delete is less than root then move right
else if (x > root->val)
root->right = Delete(root->right, x);
//this node will delete
else
{
// node with only one child or no child
if (root->left == NULL)
{
BST *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
BST *temp = root->left;
free(root);
return temp;
}
  
// node with two children
BST * temp = min(root->right);
  
  
root->val = temp->val;

root->right = Delete(root->right, temp->val);
}
return root;
}

// Driver Program
int main()
{
  
BST *root = NULL;
int i,n,no;
//ask user about the number of elements
cout<<endl<<"Enter the number of elements of a tree";
cin>>n;//read the number
//enter the value of root node
cout<<endl<<"Enter the value of root node";
cin>>no;
//insert the root node
root = insert(root,no);
//loop to insert the other nodes
for(i=2;i<=n;i++)
{
   cout<<endl<<"Enter a number";
   cin>>no;
   insert(root, no);
   }
// Inorder traversal of tree
inorder(root);

//ask user to DELETE an element into the tree
cout<<endl<<"Enter a number to delete an element with the tree";
cin>>no;
//delete an element
Delete(root, no);
//print the tree after deletion
cout<<endl<<"After deletion of the element the tree is ";
inorder(root);
return 0;
}

output


Related Solutions

c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
in C++, given a binary search tree of ints, in which each node contains a size...
in C++, given a binary search tree of ints, in which each node contains a size parameter (also an int), explain, in English, not code, how you would find the median element of the tree in theta(log N) time.
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key and then takes it out of the tree. The program can be C++/Java or C-style pseudocode. Do not use function call to either Min or delete done in the class. Write this from scratch. (No need to ensure AVL properties, just show it for a generic BST) Node * extract min(Node * x) { }
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order traversal, preorder traversal, and find. Please separate the code in the four parts and explain in detail what is happening. Also, if you can please basic C language. If not, then I understand. Thank you for your time. The test cases are 'm', 'd', 'g', 'r', 'p', 'b', and 'x'. Output: Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT