This is my current code and im having trouble inserting the findMin, findMax, and Search functions in main to output them.
#include<iostream>
using namespace std;
/*Define the class for BST*/
class BST_TREE
{
/*Structure of the tree*/
struct tree
{
int value;
tree* left;
tree* right;
};
tree* root;
/*Method to clear the tree*/
tree* Empty(tree* t)
{
if (t == NULL)
return
NULL;
{
Empty(t->left);
Empty(t->right);
delete t;
}
return NULL;
}
/*Method to Insert Node*/
tree* Insert(int x, tree* t)
{
if (t == NULL)
{
t = new tree;
t->value = x;
t->left = t->right = NULL;
}
else if (x < t->value)
t->left = Insert(x, t->left);
else if (x > t->value)
t->right = Insert(x, t->right);
return t;
}
/*Method to find the Minimum Value*/
tree* findMin(tree* t)
{
if (t == NULL)
return NULL;
else if (t->left == NULL)
return t;
else
return findMin(t->left);
}
/*Method to find the maximum Value*/
tree* findMax(tree* t)
{
if (t == NULL)
return NULL;
else if (t->right == NULL)
return t;
else
return findMax(t->right);
}
/*Method to remove the Node*/
tree* Delete(int x, tree* t)
{
tree* temp;
if (t == NULL)
return NULL;
else if (x < t->value)
t->left = Delete(x, t->left);
else if (x > t->value)
t->right = Delete(x, t->right);
else if (t->left && t->right)
{
temp = findMin(t->right);
t->value = temp->value;
t->right = Delete(t->value, t->right);
}
else
{
temp = t;
if (t->left == NULL)
t = t->right;
else if (t->right == NULL)
t = t->left;
delete temp;
}
return t;
}
/*Method to display the Tree in INorder*/
void Print_BST(tree* t)
{
if (t == NULL)
return;
Print_BST(t->left);
cout << t->value << " ";
Print_BST(t->right);
}
/*method to search the given Value*/
tree* Search(tree* t, int x)
{
if (t == NULL)
return NULL;
else if (x < t->value)
return Search(t->left, x);
else if (x > t->value)
return Search(t->right, x);
else
return t;
}
public:
/*Constructor to initialize the tree*/
BST_TREE()
{
root = NULL;
}
/*Destructor to empty the tree*/
~BST_TREE()
{
root = Empty(root);
}
void insert(int x)
{
root = Insert(x, root);
}
void Delete(int x)
{
root = Delete(x, root);
}
void FindMin(tree *t)
{
root = findMin(root);
}
void Print_BST()
{
Print_BST(root);
cout << endl;
}
void Search(int x)
{
root = Search(root, x);
}
};
int main()
{
BST_TREE t;
t.insert(5);
t.insert(8);
t.insert(3);
t.insert(1);
t.insert(4);
t.insert(9);
t.insert(18);
t.insert(20);
t.insert(19);
t.insert(2);
t.Print_BST();
//t.FindMin(*t);
t.Delete(19);
t.Print_BST();
t.Delete(2);
t.Print_BST();
t.Delete(8);
t.Print_BST();
t.Delete(3);
t.Print_BST();
t.Delete(5);
t.Print_BST();
#include<iostream>
using namespace std;
/*Define the class for BST*/
class BST_TREE
{
/*Structure of the tree*/
struct tree
{
int value;
tree* left;
tree* right;
};
tree* root;
/*Method to clear the tree*/
tree* Empty(tree* t)
{
if (t == NULL)
return NULL;
{
Empty(t->left);
Empty(t->right);
delete t;
}
return NULL;
}
/*Method to Insert Node*/
tree* Insert(int x, tree* t)
{
if (t == NULL)
{
t = new tree;
t->value = x;
t->left = t->right = NULL;
}
else if (x < t->value)
t->left = Insert(x, t->left);
else if (x > t->value)
t->right = Insert(x, t->right);
return t;
}
/*Method to find the Minimum Value*/
tree* findMin(tree* t)
{
if (t == NULL)
return NULL;
else if (t->left == NULL)
return t;
else
return findMin(t->left);
}
/*Method to find the maximum Value*/
tree* findMax(tree* t)
{
if (t == NULL)
return NULL;
else if (t->right == NULL)
return t;
else
return findMax(t->right);
}
/*Method to remove the Node*/
tree* Delete(int x, tree* t)
{
tree* temp;
if (t == NULL)
return NULL;
else if (x < t->value)
t->left = Delete(x, t->left);
else if (x > t->value)
t->right = Delete(x, t->right);
else if (t->left && t->right)
{
temp = findMin(t->right);
t->value = temp->value;
t->right = Delete(t->value, t->right);
}
else
{
temp = t;
if (t->left == NULL)
t = t->right;
else if (t->right == NULL)
t = t->left;
delete temp;
}
return t;
}
/*Method to display the Tree in INorder*/
void Print_BST(tree* t)
{
if (t == NULL)
return;
Print_BST(t->left);
cout << t->value << " ";
Print_BST(t->right);
}
/*method to search the given Value*/
tree* Search(tree* t, int x)
{
if (t == NULL)
return NULL;
else if (x < t->value)
return Search(t->left, x);
else if (x > t->value)
return Search(t->right, x);
else
return t;
}
public:
/*Constructor to initialize the tree*/
BST_TREE()
{
root = NULL;
}
/*Destructor to empty the tree*/
~BST_TREE()
{
root = Empty(root);
}
void insert(int x)
{
root = Insert(x, root);
}
void Delete(int x)
{
root = Delete(x, root);
}
void FindMin()
{
tree *min = findMin(root);
cout<<"Min value is: "<<min->value<<endl;
}
void Print_BST()
{
Print_BST(root);
cout << endl;
}
void Search(int x)
{
tree *s = Search(root, x);
if(s==NULL)
cout<< "value is not found"<<endl;
else
cout<< "Value is found"<<endl;
}
void FindMax()
{
tree *max = findMax(root);
cout<<"Min value is: "<<max->value<<endl;
}
};
int main()
{
BST_TREE t;
t.insert(5);
t.insert(8);
t.insert(3);
t.insert(1);
t.insert(4);
t.insert(9);
t.insert(18);
t.insert(20);
t.insert(19);
t.insert(2);
t.Print_BST();
t.FindMin();
t.FindMax();
t.Search(20);
t.Delete(19);
t.Print_BST();
t.Delete(2);
t.Print_BST();
t.Delete(8);
t.Print_BST();
t.Delete(3);
t.Print_BST();
t.Delete(5);
t.Print_BST();
}
=============================================
SEE OUTPUT
========================================
Thanks, let me now if there is any concern.
Get Answers For Free
Most questions answered within 1 hours.