#include <bits/stdc++.h>
using namespace std;
class tnode {
public:
int data;
tnode *left,*right;
};
class bst {
public:
tnode* root=NULL;
tnode* insert(tnode *temp, int item) {
if (temp==NULL) {
temp=new tnode;
temp->data = item;
temp->left=NULL;
temp->right=NULL;
}
else {
if (item<=temp->data) {
temp->left=insert(temp->left,item);
}
else {
temp->right = insert(temp->right,item);
}
}
return temp;
}
bool search(tnode *s,int key) {
if (s==NULL) {
return false;
}
else if (key==s->data) {
return true;
}
else if (key<s->data) {
return search(s->left,key);
}
else {
return search (s->right,key);
}
}
void preorder(tnode*node) {
if (node==NULL) {
return;
}
cout<<node->data<<" ";
preorder(node->left);
preorder(node->right);
}
void postorder(tnode*node) {
if (node==NULL) {
return;
}
postorder(node->left);
postorder(node->right);
cout<<node->data<<" ";
}
void inorder(tnode*node) {
if (node==NULL) {
return;
}
inorder(node->left);
cout<<node->data<<" ";
inorder(node->right);
}
tnode* min(tnode* temp) {
if (temp==NULL) {
return NULL;
}
else if (temp->left==NULL){
return temp;
}
else {
return min(temp->left);
}
}
tnode* max(tnode* temp) {
if (temp==NULL) {
return NULL;
}
else if (temp->right==NULL){
return temp;
}
else {
return max(temp->right);
}
}
};
int main() {
bst b;
b.root=b.insert(b.root,45);
b.root=b.insert(b.root,15);
b.root=b.insert(b.root,79);
b.root=b.insert(b.root,90);
b.root=b.insert(b.root,10);
b.root=b.insert(b.root,55);
b.root=b.insert(b.root,12);
b.root=b.insert(b.root,20);
b.root=b.insert(b.root,50);
cout << "Display the Tree Content\n";
b.preorder(b.root);
cout << "\n";
b.inorder(b.root);
cout <<"\n";
b.postorder(b.root);
cout <<"\n";
int key=5;
cout << "Enter item to research for 5 \n";
if (b.search(b.root,key))
cout << "item found \n";
else {
cout << "sorry, item not found \n";
}
int k=15;
cout << "Enter item to research for 15 \n";
if (b.search(b.root,k))
cout << "item found \n";
else {
cout << "sorry, item not found \n";
}
cout << "Find Minimum \n";
tnode* minimum = b.min (b.root);
if (minimum == NULL)
cout << "No Item Exist \n";
else
cout << "Minimum is " << minimum ->data << "\n";
cout << "Find Maximum \n";
tnode* maximum = b.max (b.root);
if (maximum == NULL)
cout << "No Item Exist \n";
else
cout << "Maximum is " << maximum ->data << "\n";
}