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();

}

Doubly linked list all operations

#include<iostream>
#include<cstdlib>
using namespace std;
struct dnode
{
int data;
dnode *right;
dnode *left;
};
void display(dnode *s)
{
dnode *p;
p=s;
cout<<"\tThus the no's are as:\n";
while(p!=NULL)
{cout<<"\t"<<p->data<<endl;
p=p->right;
}
}
dnode *add_end(dnode *s,int d)
{
dnode *p,*l;
l=s;
while(l->right!=NULL)
l=l->right;
p=new(dnode);
p->data=d;
p->right=NULL;
p->left=NULL;
l->right=p;
p->left=l;
return s;
}
dnode *add_begin(dnode *s,int d)
{
dnode *p,*l;
p=s;
l=new(dnode);
l->data=d;
l->right=p;
l->left=NULL;
p->left=l;
s=l;
return s;
}
dnode *add_after(dnode *s,int d,int k)
{
dnode *p,*l;
p=s;
while(p->data!=d)
p=p->right;
if(p->right==NULL)
{
l=new(dnode);
l->data=k;
l->right=NULL;
l->left=p;
p->right=l;
return s;
}
else
{
l=new(dnode);
l->data=k;
l->right=NULL;
l->left=NULL;
l->right=p->right;
l->left=p;
p->right=l;
l->right->left=l;
return s;
}
}
dnode *add_before(dnode *s,int d,int k)
{
dnode *p,*l;
p=s;
if(p->data==d)
{
l=new(dnode);
l->data=k;
    l->right=p;
   l->left=NULL;
p->left=l;
s=l;
return s;
}
else
{
while(p->data!=d)
p=p->right;
l=new(dnode);
l->data=k;
l->left=p->left;
l->right=p;
p->left=l;
l->left->right=l;
return s;
}
}
dnode *del_end(dnode *s)
{
dnode *p;
p=s;
while(p->right->right!=NULL)
p=p->right;
p->right=NULL;
return s;
}
dnode *del_begin(dnode *s)
{
dnode *p;
p=s;
p=p->right;
p->left=NULL;
s=p;
return s;
}
dnode *del_mid(dnode *s,int d)
{
dnode *p;
p=s;
while(p->right->data!=d)
p=p->right;
p->right=p->right->right;
p->right->left=p;
return s;
}
int main()
{
dnode *s,*t,*l;
int d,k,n;
l=new(dnode);
s=l;
    cout<<"Start entering the no's.....Enter -1 for aborting...\n";
    cin>>d;
    l->data=d;
    l->left=NULL;
    l->right=NULL;
    cin>>d;
    while(d!=-1)
     {t=new(dnode);
      t->data=d;
      t->right=NULL;
      t->left=NULL;
      l->right=t;
      t->left=l;
      l=t;
      cin>>d;
     }
    display(s);
    while(1)
{
  cout<<"Select any of the options provided below....\n1:Adding at the end\n2:Adding at the beginning\n3:Adding after a specified no\n4:Adding before a specified no\n5:Deleting a no at the end\n6:Deleting a no at the beginning\n7:Deleting a no in between the no's\n8:For quitting\n";
  cin>>n;
       switch(n)
       {case 1 :cout<<"Enter the no:";
                cin>>d;
                s=add_end(s,d);
                display(s);
                break;
        case 2 :cout<<"Enter the no:";
                cin>>d;
                s=add_begin(s,d);
                display(s);
                break;
        case 3 :cout<<"Enter the no after which you want to enter:";
       cin>>d;
       cout<<"Enter the no:";
       cin>>k;
       s=add_after(s,d,k);
       display(s);
break;
case 4 :cout<<"Enter the no before which you want to enter:";
       cin>>d;
       cout<<"Enter the no:";
       cin>>k;
       s=add_before(s,d,k);
       display(s);
       break;
case 5 :s=del_end(s);
       display(s);
       break;
case 6 :s=del_begin(s);
       display(s);
       break;
case 7 :cout<<"Enter the no which you want to delete:";
       cin>>d;
       s=del_mid(s,d);
       display(s);
       break;
case 8 :cout<<"Thanks...";
       exit(1);
        default:cout<<"Wrong choice....Enter again\n";
                break;
       }
    }
}

Adding large numbers using linked list in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
void display(node *l)
{
node *s;
s=l;
while(s!=NULL)
{
cout<<s->data;
s=s->next;
}
}
node *add(node *p,node *q)
{
node *t,*l;
int d;
t=NULL;
while(p!=NULL && q!=NULL)
{
d=p->data+q->data;
p=p->next;
q=q->next;
if(d>9)
{
d-=10;
if(p==NULL)
{
p=new(node);
p->data=0;
p->next=NULL;
}
if(q==NULL)
{
q=new(node);
q->data=0;
q->next=NULL;
}
p->data+=1;
}
if(t==NULL)
{
t=new(node);
t->data=d;
t->next=NULL;
}
else
{
l=new(node);
l->data=d;
l->next=t;
t=l;
}
if(p==NULL && q==NULL)
break;
if(p==NULL)
{
p=new(node);
p->data=0;
p->next=NULL;
}
if(q==NULL)
{
q=new(node);
q->data=0;
q->next=NULL;
}
}
return l;
}
int main()
{
node *s1,*s2,*l,*t;
int d;
cout<<"\nEnter the no's....Press -1 for aboting....:\n";
cin>>d;
t=new(node);
t->data=d;
t->next=NULL;
cin>>d;
while(d!=-1)
{
l=new(node);
l->data=d;
l->next=t;
t=l;
cin>>d;
}
s1=l;
cout<<"\nEnter the no's....Press -1 for aboting....:\n";
cin>>d;
t=new(node);
t->data=d;
t->next=NULL;
cin>>d;
while(d!=-1)
{
l=new(node);
l->data=d;
l->next=t;
t=l;
cin>>d;
}
s2=l;
l=add(s1,s2);
cout<<"Thus the addn is:\n";
display(l);
}

Adding two mathematical function using linked list in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int co;
int po;
node *next;
};
void display(node *s)
{
node *p;
p=s;
while(p!=NULL)
{
cout<<p->co<<"*("<<p->po<<")+";
p=p->next;
}

}
node *add(node *s)
{
node *t,*p,*q,*r,*e,*z,*f;int ctr=0;
r=NULL;
e=r;
p=s;
while(p!=NULL)
{
t=new(node);
t->po=p->po;
t->co=p->co;
t->next=NULL;
q=p;
if(q!=NULL)
q=q->next;
if(q==NULL);
else
{

while(q!=NULL)
{
if(p->po==q->po)
{
t->co+=q->co;
if(p->next->po==q->po)
p->next=p->next->next;
else if(p->next==NULL)
break;
else
{
f=p;
while(f->next->po!=q->po)
f=f->next;
f->next=f->next->next;
break;
}
}
if(q->next==NULL)
break;
else
q=q->next;
}
}
if(r==NULL)
{
r=t;
e=r;
}
else
{
r->next=t;
r=t;
}
p=p->next;
}
return e;
}
node *sub(node *s1,node *s2)
{
node *t,*p,*q,*r,*e,*s,*z,*f,*w;int ctr=0;
r=NULL;
e=r;
s=s1;
w=s2;
while(s2!=NULL)
{
s2->co*=-1;
s2=s2->next;
}
while(s1->next!=NULL)
s1=s1->next;
s1->next=w;
p=s;
while(p!=NULL)
{
t=new(node);
t->po=p->po;
t->co=p->co;
t->next=NULL;
q=p;
if(q!=NULL)
q=q->next;
if(q==NULL);
else
{

while(q!=NULL)
{
if(p->po==q->po)
{
t->co+=q->co;
if(p->next->po==q->po)
p->next=p->next->next;
else if(p->next==NULL)
break;
else
{
f=p;
while(f->next->po!=q->po)
f=f->next;
f->next=f->next->next;
break;
}
}
if(q->next==NULL)
break;
else
q=q->next;
}
}
if(r==NULL)
{
r=t;
e=r;
}
else
{
r->next=t;
r=t;
}
p=p->next;
}
return e;
}
node *mul(node *s1,node *s2)
{
node *l,*t,*s;
t=NULL;
s=s2;
while(s1!=NULL)
{
s2=s;
while(s2!=NULL)
{
if(t==NULL)
{
t=new(node);
t->po=(s1->po)+(s2->po);
t->co=(s1->co)*(s2->co);
t->next=NULL;
}
else
{
l=new(node);
l->po=(s1->po)+(s2->po);
l->co=(s1->co)*(s2->co);
l->next=t;
t=l;
}
s2=s2->next;
}
s1=s1->next;
}
t=add(l);
return t;
}
int main()
{
node *s,*l,*p1,*p2,*t,*s1,*s2;
int i,d,k;
l=new(node);
p1=l;
    cout<<"Start entering the Coeff. & Power.....Enter -1 for aborting...\n";
    cin>>d;
    cin>>k;
    l->co=d;
    l->po=k;
    l->next=NULL;
    cin>>d;
    cin>>k;
    while(d!=-1)
     {t=new(node);
      t->co=d;
      t->po=k;
      t->next=NULL;
      l->next=t;
      l=t;
      cin>>d;
      cin>>k;
     }
    display(p1);
    l=new(node);
    p2=l;
    cout<<"\nStart entering the Coeff. & Power.....Enter -1 for aborting...\n";
    cin>>d;
    cin>>k;
    l->co=d;
    l->po=k;
    l->next=NULL;
    cin>>d;
    cin>>k;
    while(d!=-1)
     {t=new(node);
      t->co=d;
      t->po=k;
      t->next=NULL;
      l->next=t;
      l=t;
      cin>>d;
      cin>>k;
     }
    display(p2);
    s2=p2;
    s1=p1;
    l=p1;
    t=p1;
    while(l->next!=NULL)
    l=l->next;
    k=l->co;
    d=l->po;
    while(s1->next!=NULL)
    s1=s1->next;
    s1->next=s2;
    cout<<"\nPress...\n1 for Addition\n2 for Subtraction\n3 for Multiplication\n";
    cin>>k;
    switch(k)
    {
    case 1:s=add(p1);
      display(s);
      break;
    case 2:while(t->co!=k && t->po!=d)
          t=t->next;
          t->next=NULL;
  s=sub(p1,p2);
      display(s);
      break;
    case 3:while(t->co!=k && t->po!=d)
          t=t->next;
          t->next=NULL;
  s=mul(p1,p2);
      display(s);
      break;
    }
}

Radix sort in C++

#include<iostream>
#include<math.h>
using namespace std;
struct node
{
int data;
node *next;
};
void display(node *s)
{
node *p;
p=s;
cout<<"\tThus the no's are as:\n";
while(p!=NULL)
{cout<<"\t"<<p->data<<endl;
p=p->next;
}
}
node *add_end(node *q,int n)
{
node *p,*l;
l=q;
if(l==NULL)
{
l=new(node);
    l->data=n;
    l->next=NULL;
    return l;
}
else
{
while(q->next!=NULL)
    q=q->next;
      p=new(node);
    p->data=n;
        p->next=NULL;
        q->next=p;
        return l;
}

}
int main()
{
node *r[10],*l,*s,*t;
int i,n,d,k,z,j,max=-1,ctr=0;
l=new(node);
s=l;
    cout<<"Start entering the no's.....Enter -1 for aborting...\n";
    cin>>d;
    l->data=d;
    l->next=NULL;
    cin>>d;
    while(d!=-1)
     {t=new(node);
      t->data=d;
      if(max<d)
      max=d;
      t->next=NULL;
      l->next=t;
      l=t;
      cin>>d;
     }
    while(max!=0)
    {
    max=max/10;
    ctr++;
    }
    for(j=1;j<ctr+1;j++)
    {
    for(i=0;i<10;i++)
          r[i]=NULL;
    while(s!=NULL)
        {
       n=s->data;
       k=pow(10,j);
       z=n%k;
       d=pow(10,j-1);
       i=z/d;
        r[i]=add_end(r[i],n);
          s=s->next;
        }
        s=NULL;
        l=s;
        for(i=0;i<10;i++)
        {
        if(r[i]!=NULL)
        {
        t=r[i];
        if(s==NULL)
        {
        s=t;
        l=s;
        }
        else
        {
        while(s->next!=NULL)
        s=s->next;
        s->next=t;
        }
        }
        }
        s=l;
    }
    display(s);
}

Bin sort in C++

#include<iostream>
using namespace std;
struct nodep
{
int data;
nodep *next1;
};
struct node
{
char ch;
nodep *p;
nodep *q;
node *next;
};
int search(char c,node *K)
{
while(K->next!=NULL)
{
if(K->ch==c)
{
cout<<"welcome:\n";
return 1;
}
else
if(K->next!=NULL)
K=K->next;
}
return 0;
}
void print(node *KK)
{
while(KK!=NULL)
{
cout<<KK->ch<<"    ";
while(KK->q!=NULL)
{
cout<<KK->q->data<<"      ";
KK->q=KK->q->next1;
}
cout<<"\n";
KK=KK->next;
}
}
int main()
{
node *L,*S;char ch1,ch2,choice;int n,y;
L=new(node);S=L;
cout<<"enter the character:\n";
cin>>ch1;
ch2=ch1;
L->ch=ch1;
cout<<"enter the data value:\n";
cin>>n;
L->p=new(nodep);
L->q=L->p;
L->p->data=n;
L->p->next1=NULL;
L->next=NULL;
cout<<"enter y to continue:\n";
cin>>choice;
while(choice=='y'||choice=='Y')
{
cout<<"enter the character:\n";
cin>>ch1;
if(ch1==ch2)
y=1;
else
y=search(ch1,S);
ch2=ch1;
if(y==1)
{
L=S;
while(L->ch!=ch1)
L=L->next;
nodep *temp;
temp=new(nodep);
cout<<"enter the data value:\n";
cin>>n;
temp->data=n;
temp->next1=NULL;
while(L->p->next1!=NULL)
L->p=L->p->next1;
L->p->next1=temp;
L->p=temp;
}
else
{
L=S;
while(L->next!=NULL)
  L=L->next;
  node *temp;
  temp=new(node);
  temp->ch=ch1;
  temp->p=new(nodep);
  temp->q=temp->p;
  cout<<"enter the data value:\n";
  cin>>n;
  temp->p->data=n;
  temp->p->next1=NULL;
  temp->next=NULL;
  L->next=temp;
  L=temp;
}
cout<<"enter y to continue:\n";
cin>>choice;
}
print(S);
return 0;
}

Contributors

Translate