Question: Hello there! ,I have this data structure below of Binary search Tree.. I want the 4 exercises bottom, solved using the data structure i have
Hello there! ,I have this data structure below of Binary search Tree.. I want the 4 exercises bottom, solved using the data structure i have supported along with the methods already built-in.. thank you :D
#include
using namespace std;
const int n=11;
template
struct node
{
T data;
node
node
node(){left=right=0;}
node(T item){data = item;left=right=0;}
};
template
class btree
{
node
void insert(T key, node
{
if(key < leaf->data)
{
if(leaf->left!=NULL)
insert(key, leaf->left);
else
{
leaf->left=new node
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else if(key>=leaf->data)
{
if(leaf->right!=NULL)
insert(key, leaf->right);
else
{
leaf->right=new node
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
}
void destroy(node
if(leaf!=NULL)
{
destroy(leaf->left);
destroy(leaf->right);
delete leaf;
}
}
node
{
if(leaf!=NULL)
{
if(key==leaf->data)
return leaf;
if(key
return search(key, leaf->left);
else
return search(key, leaf->right);
}
else return NULL;
}
void inorder_print(node
{
if(leaf != NULL)
{
inorder_print(leaf->left);
cout << leaf->data << " , ";
inorder_print(leaf->right);
}
}
node
{
node
/* loop down to find the leftmost leaf */
while (current && current->left != NULL)
current = current->left;
return current;
}
node
{
// base case
if (root == NULL) return root;
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if (key < root->data)
root->left = deleteNode(root->left, key);
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if (key > root->data)
root->right = deleteNode(root->right, key);
// if key is same as root's key, then This is the node
// to be deleted
else
{
// node with only one child or no child
if (root->left == NULL)
{
node
delete root;
return temp;
}
else if (root->right == NULL)
{
node
delete root;
return temp;
}
// node with two children: Get the inorder successor (smallest
// in the right subtree)
node
// Copy the inorder successor's content to this node
root->data = temp->data;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->data);
}
return root;
}
public:
btree(){ root=NULL;}
~btree(){ destroy(root);}
void inorder()
{
inorder_print(root);
}
void insert(T key)
{
if(root!=NULL)
insert(key, root);
else
{
root=new node
}
}
int search(T key)
{
node
if(q==0)
return 0;
return 1;
}
void destroy()
{
destroy(root);
}
int deleteNode(T key)
{
if(deleteNode(root,key)==0)
return 0;
return 1;
}
bool empty()
{
return(root == NULL);
}
};
int main() {
btree
bt.insert(50);
bt.insert(30);
bt.insert(20);
bt.insert(40);
bt.insert(70);
bt.insert(60);
bt.inorder();
bt.destroy();
bt.insert(80);
bt.insert(45);
bt.deleteNode(70);
cout< bt.inorder(); if(bt.search(80)) cout<<" the item is found"; else cout<<" the item is not Exist"; } Exercise1: write an operator overload for = operation. Exercise 2: write a code to check if the two binary trees are the same or not Exercise 3: write a code to flip the right subtree to be the left and the left to be the right Exercise 4: write a code to check if the tree is a BST or not
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
