Showing posts with label bd tree. Show all posts
Showing posts with label bd tree. Show all posts

Saturday, February 7, 2015

Deleting a node in btree in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct bstnode
{
bstnode *lchild;
int data;
bstnode *rchild;
};
bstnode *insert(bstnode * &t,int k)
{
if(t==NULL)
{
t=new(bstnode);
t->lchild=NULL;
t->rchild=NULL;
t->data=k;
}
else if(t->data>k)
insert(t->lchild,k);
else
insert(t->rchild,k);
}
int search(bstnode *t,int k)
{
if(t==NULL)
return 0;
else if(t->data>k)
return(search(t->lchild,k));
else if(t->data<k)
return(search(t->rchild,k));
else
return 1;
}
void printsort(bstnode * t)
{
if(t!=NULL)
{
printsort(t->lchild);
cout<<t->data<<endl;
printsort(t->rchild);
}
}
int max(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->rchild==NULL)
return t->data;
else
return max(t->rchild);
}
int max2(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->lchild==NULL)
return t->data;
else
return max2(t->lchild);
}
void del(bstnode * &t,int k)
{
if(t!=NULL)
{
if(k==t->data && (t->rchild!=NULL || t->lchild!=NULL))
{
int a=max(t->lchild);
if(a){
del(t,a);
t->data=a;
}
else{
int b=max2(t->rchild);
if(b)
del(t,b);
t->data=b;
}
}
else if(k==t->data && (t->rchild==NULL && t->lchild==NULL))
{
t=NULL;
}
else if(k>t->data)
del(t->rchild,k);
else
del(t->lchild,k);
}
}
void begin()
{
bstnode *s,*t;
int k,i,choice;
t=NULL;
while(1)
{
  cout<<"Select any of the options provided below....\n1:Enter the bstree\n2:Search for a no\n3:Max no\n4:Min no\n5:Printing terminal node\n6:Printing Sorted no's\n";
  cin>>choice;
  switch(choice)
  {
  case 1 :cout<<"Press -1 for aborting....\n";
         cin>>k;
         while(k!=-1)
         {
          insert(t,k);
          cin>>k;
         }
         break;
case 2 :cout<<"Enter the no:";
cin>>k;
del(t,k);
break;
case 3 :cout<<"The sorted nos's are\n";
printsort(t);
break;
case 10:cout<<"Thanks\n";
       exit(1);
default:cout<<"Wrong Choice Entered....Please enter again\n";
break;
  }
    }
}
int main()
{
begin();

}

B tree creation basic in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct bstnode
{
bstnode *lchild;
int data;
bstnode *rchild;
};
bstnode *insert(bstnode * &t,int k)
{
if(t==NULL)
{
t=new(bstnode);
t->lchild=NULL;
t->rchild=NULL;
t->data=k;
}
else if(t->data>k)
insert(t->lchild,k);
else
insert(t->rchild,k);
}
int search(bstnode *t,int k)
{
if(t==NULL)
return 0;
else if(t->data>k)
return(search(t->lchild,k));
else if(t->data<k)
return(search(t->rchild,k));
else
return 1;
}
int max(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->rchild==NULL)
return t->data;
else
return max(t->rchild);
}
int min(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->lchild==NULL)
return t->data;
else
return min(t->lchild);
}
void ter_print(bstnode *t)
{
if(t!=NULL)
{
if(t->lchild==NULL && t->rchild==NULL)
cout<<t->data<<endl;
ter_print(t->lchild);
ter_print(t->rchild);
}
}
void printsort(bstnode *t)
{
if(t!=NULL)
{
printsort(t->lchild);
cout<<t->data<<endl;
printsort(t->rchild);
}
}
void begin()
{
bstnode *s,*t;
int k,i,choice;
t=NULL;
while(1)
{
  cout<<"Select any of the options provided below....\n1:Enter the bstree\n2:Search for a no\n3:Max no\n4:Min no\n5:Printing terminal node\n6:Printing Sorted no's\n";
  cin>>choice;
  switch(choice)
  {
  case 1 :cout<<"Press -1 for aborting....\n";
         cin>>k;
         while(k!=-1)
         {
          insert(t,k);
          cin>>k;
         }
         break;
  case 2 :cout<<"Enter the element you are looking for:\n";
     cin>>k;
     s=t;
   i=search(s,k);
   if(i==1)
cout<<"Found\n";
   else
    cout<<"Not found\n";
   break;
case 3 :cout<<"Thus the maximum no is:";
s=t;
i=max(s);
if(i==0)
cout<<"No maximum value as the BStree is Null\n";
else
cout<<i<<endl;
break;
case 4 :cout<<"Thus the minimum no is:";
s=t;
i=min(s);
if(i==0)
cout<<"No minimum value as the BStree is Null\n";
else
cout<<i<<endl;
break;
case 5 :cout<<"Thus the Terminal Nodes are\n";
ter_print(t);
break;
case 6 :cout<<"The sorted nos's are\n";
printsort(t);
break;
case 10:cout<<"Thanks\n";
       exit(1);
default:cout<<"Wrong Choice Entered....Please enter again\n";
break;
  }
    }
}
int main()
{
begin();

}

Friday, March 14, 2014

COMPARE 2 TREES in C++

Comparing two tree means check each and every node of tree on each level that it is equal or not.
for two similar trees parent as well as both child node should be equal.
the code for comparing two trees is given below.


first tree           and second tree
we copare node 'a' with 'x' and its child subtrees with 'others child subtree.







#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct btree                                     //defined the node structure
{
btree *lchild;
char data;
btree *rchild;
};
btree* bcreate(btree *t,char k);
void printsort(btree *t,char b[15]);
int l=0,m=0;
int main()
{
btree *p=NULL,*q=NULL;
char d,a[15],b[15],c[15];
int n,i;
cout<<"tree data\n";
cin>>d;
p=bcreate(p,d);
printsort(p,a);
a[l]='\0';
n=l;
l=0;
cout<<"tree data\n";
cin>>d;
q=bcreate(q,d);
printsort(q,b);
b[l]='\0';
for(i=0;i<l; i++)
{
c[i]=b[l-1-i];
}
c[l]='\0';
if(!strcmp(a,b))
cout<<"similar";
else if(!strcmp(a,c))
cout<<"mirror";
else
cout<<"nonsimilar";
return 0;
}
btree* bcreate(btree* t,char k)                     //to get input from user
{

                                             btree *s=NULL,*r=NULL;char c1,c2;
if(t==NULL && k!='.')                        //k='.' used  here for stop making nodes in tree
{
t=new(btree);
t->data=k;
t->rchild=NULL; t->lchild=NULL;
cout<<"enter left and right child of "<<k<<endl;
cin>>c1>>c2;
t->lchild=bcreate(s,c1);                     //recursive call for create child
t->rchild=bcreate(r,c2);
}
return t;
}
void printsort(btree *t,char b[15])                       // f\print the tree content in sorted order  nodes                                                                                              // (left child,parent ,right child) and store in character array
{                                                                          
if(t!=NULL)
if(t->lchild==NULL && t->rchild==NULL)
b[l++]=t->data;
else
{
printsort(t->lchild,b);
b[l++]=t->data;
printsort(t->rchild,b);
}
}

Contributors

Translate