Skip to content

Commit

Permalink
Merge pull request #75 from krishna14kant/main
Browse files Browse the repository at this point in the history
added bst program
  • Loading branch information
aman-raza authored Oct 9, 2020
2 parents 78f5de6 + 55f8660 commit 92614d3
Showing 1 changed file with 359 additions and 0 deletions.
359 changes: 359 additions & 0 deletions CPP/bst.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,359 @@
# include <iostream>
# include <cstdlib>
using namespace std;
struct nod//node declaration
{
int info;
struct nod *l;
struct nod *r;
}*r;
class BST
{
public://functions declaration
void search(nod *, int);
void find(int, nod **, nod **);
void insert(nod *, nod *);
void del(int);
void casea(nod *,nod *);
void caseb(nod *,nod *);
void casec(nod *,nod *);
void preorder(nod *);
void inorder(nod *);
void postorder(nod *);
void show(nod *, int);
BST()
{
r = NULL;
}
};
void BST::find(int i, nod **par, nod **loc)//find the position of the item
{
nod *ptr, *ptrsave;
if (r == NULL)
{
*loc = NULL;
*par = NULL;
return;
}
if (i == rinfo)
{
*loc = r;
*par = NULL;
return;
}
if (i < rinfo)
ptr = rl;
else
ptr = rr;
ptrsave = r;
while (ptr != NULL)
{
if (i == ptrinfo)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (i < ptrinfo)
ptr = ptrl;
else
ptr = ptrr;
}
*loc = NULL;
*par = ptrsave;
}
void BST::search(nod *root, int data) //searching
{
int depth = 0;
nod *temp = new nod;
temp = root;
while(temp != NULL)
{
depth++;
if(tempinfo == data)
{
cout<<"\nData found at depth: "<<depth<<endl;
return;
}
else if(tempinfo > data)
temp = templ;
else
temp = tempr;
}
cout<<"\n Data not found"<<endl;
return;
}
void BST::insert(nod *tree, nod *newnode)
{
if (r == NULL)
{
r = new nod;
rinfo = newnodeinfo;
rl= NULL;
rr= NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (treeinfo == newnodeinfo)
{
cout<<"Element already in the tree"<<endl;
return;
}
if (treeinfo > newnodeinfo)
{
if (treel != NULL)
{
insert(treel, newnode);
}
else
{
treel= newnode;
(treel)→l = NULL;
(treel)→r= NULL;
cout<<"Node Added To Left"<<endl;
return;
}
}
else
{
if (treer != NULL)
{
insert(treer, newnode);
}
else
{
treer = newnode;
(treer)→l= NULL;
(treer)→r = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
}
void BST::del(int i)
{
nod *par, *loc;
if (r == NULL)
{
cout<<"Tree empty"<<endl;
return;
}
find(i, &par, &loc);
if (loc == NULL)
{
cout<<"Item not present in tree"<<endl;
return;
}
if (locl == NULL && locr == NULL)
{
casea(par, loc);
cout<<"item deleted"<<endl;
}
if (locl!= NULL && locr == NULL)
{
caseb(par, loc);
cout<<"item deleted"<<endl;
}
if (locl== NULL && locr != NULL)
{
caseb(par, loc);
cout<<"item deleted"<<endl;
}
if (locl != NULL && locr != NULL)
{
casec(par, loc);
cout<<"item deleted"<<endl;
}
free(loc);
}
void BST::casea(nod *par, nod *loc )
{
if (par == NULL)
{
r= NULL;
}
else
{
if (loc == parl)
parl = NULL;
else
parr = NULL;
}
}
void BST::caseb(nod *par, nod *loc)
{
nod *child;
if (locl!= NULL)
child = locl;
else
child = locr;
if (par == NULL)
{
r = child;
}
else
{
if (loc == parl)
parl = child;
else
parr = child;
}
}
void BST::casec(nod *par, nod *loc)
{
nod *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = locr;
while (ptrl!= NULL)
{
ptrsave = ptr;
ptr = ptrl;
}
suc = ptr;
parsuc = ptrsave;
if (sucl == NULL && sucr == NULL)
casea(parsuc, suc);
else
caseb(parsuc, suc);
if (par == NULL)
{
r = suc;
}
else
{
if (loc == parl)
parl = suc;
else
parr= suc;
}
sucl = locl;
sucr= locr;
}
void BST::preorder(nod *ptr)
{
if (r == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptrinfo<<" ";
preorder(ptrl);
preorder(ptrr);
}
}
void BST::inorder(nod *ptr)//inorder traversal
{
if (r == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptrl);
cout<<ptrinfo<<" ";
inorder(ptrr);
}
}
void BST::postorder(nod *ptr)//postorder traversal
{
if (r == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
postorder(ptrl);
postorder(ptrr);
cout<<ptrinfo<<" ";
}
}
void BST::show(nod *ptr, int level)//print the tree
{
int i;
if (ptr != NULL)
{
show(ptrr, level+1);
cout<<endl;
if (ptr == r)
cout<<"Root→: ";
else
{
for (i = 0;i < level;i++)
cout<<" ";
}
cout<<ptrinfo;
show(ptrl, level+1);
}
}
int main()
{
int c, n,item;
BST bst;
nod *t;
while (1)
{
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Search Element"<<endl;
cout<<"4.Inorder Traversal"<<endl;
cout<<"5.Preorder Traversal"<<endl;
cout<<"6.Postorder Traversal"<<endl;
cout<<"7.Display the tree"<<endl;
cout<<"8.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>c;
switch(c)
{
case 1:
t = new nod;
cout<<"Enter the number to be inserted : ";
cin>>tinfo;
bst.insert(r, t);
break;
case 2:
if (r == NULL)
{
cout<<"Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted : ";
cin>>n;
bst.del(n);
break;
case 3:
cout<<"Search:"<<endl;
cin>>item;
bst.search(r,item);
break;
case 4:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(r);
cout<<endl;
break;
case 5:
cout<<"Preorder Traversal of BST:"<<endl;
bst.preorder(r);
cout<<endl;
break;
case 6:
cout<<"Postorder Traversal of BST:"<<endl;
bst.postorder(r);
cout<<endl;
break;
case 7:
cout<<"Display BST:"<<endl;
bst.show(r,1);
cout<<endl;
break;
case 8:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
}

0 comments on commit 92614d3

Please sign in to comment.