I have wrote a simple c++ program for the implementation of Binary search tree.
Credits:MyCodeSchool
Code snippet:
Credits:MyCodeSchool
Code snippet:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct BstNode | |
{ | |
int data; | |
BstNode* left; | |
BstNode* right; | |
}; | |
BstNode* CreateNode(int DataArg) | |
{ | |
BstNode* temp = new BstNode; | |
temp->data = DataArg; | |
temp->left = NULL; | |
temp->right = NULL; | |
return temp; | |
} | |
BstNode* Findmin(BstNode* x) | |
{ | |
while(x->left != NULL) x = x->left; | |
return x; | |
} | |
bool searchBst(BstNode* x,int val) | |
{ | |
if(x == NULL) return 0; | |
else if(val == x->data) return 1; | |
else if(val < x->data) searchBst(x->left,val); | |
else searchBst(x->right,val); | |
} | |
BstNode* Findmax(BstNode* x) | |
{ | |
while(x->right != NULL) x = x->right; | |
return x; | |
} | |
void Insert(BstNode*& x, int d) | |
{ | |
if(x == NULL) | |
{ | |
x = CreateNode(d); | |
} | |
else if(d <= x->data) | |
{ | |
Insert(x->left,d); | |
} | |
else | |
{ | |
Insert(x->right,d); | |
} | |
} | |
void printBST(BstNode* temp) | |
{ | |
if(temp == NULL) return; | |
printBST(temp->left); | |
cout<<temp->data<<"\t"; | |
printBST(temp->right); | |
cout<<endl; | |
} | |
void del(BstNode*& arg, int val) | |
{ | |
if(arg == NULL) return; | |
else if(val < arg->data) del(arg->left,val); | |
else if(val > arg->data) del(arg->right,val); | |
else | |
{ | |
if(arg->left == NULL && arg->right == NULL)//No child | |
{ | |
delete arg; | |
arg = NULL; | |
return; | |
} | |
else if(arg->right == NULL)//Only one child | |
{ | |
BstNode* temp = arg->left;//backup | |
delete arg; | |
arg = temp; | |
delete temp; | |
} | |
else if(arg->left == NULL)//Only one child | |
{ | |
BstNode* temp = arg->right;//backup | |
delete arg; | |
arg = temp; | |
delete temp; | |
} | |
else | |
{ | |
BstNode* temp = Findmin(arg->right); | |
arg->data = temp->data; | |
del(arg->right,temp->data); | |
} | |
} | |
} | |
int main() | |
{ | |
BstNode* root =NULL;//Initially tree is empty. | |
int choice,buffer; | |
cout<<"/** Binary Search Tree **/\n"<<"1.Insert\n2.Delete\n3.FindMax\n4.FindMin\n5.Print\n6.Exit\n"; | |
do | |
{ | |
cout<<"Enter your choice:"; | |
cin>>choice; | |
switch(choice) | |
{ | |
case 1 : | |
{ | |
cout<<"Enter the Value to insert:"; | |
cin>>buffer; | |
Insert(root,buffer); | |
break; | |
} | |
case 2 : | |
{ | |
cout<<"Enter value to delete:"; | |
cin>>buffer; | |
del(root,buffer); | |
break; | |
} | |
case 3 : | |
{ | |
BstNode* buff1 = Findmax(root); | |
cout<<"Max value:"<<buff1->data<<endl; | |
break; | |
} | |
case 4 : | |
{ | |
BstNode* buff2 = Findmin(root); | |
cout<<"Mix value:"<<buff2->data<<endl; | |
break; | |
} | |
case 5 : | |
{ | |
printBST(root); | |
break; | |
} | |
case 6 : | |
{ | |
cout<<"Thank you :-)"; | |
break; | |
} | |
default : | |
{ | |
cout<<"Invalid Choice"; | |
} | |
} | |
}while(choice != 6); | |
} |
Comments
Post a Comment