Friday, August 31, 2012

C Program for Quick Sort

#include<iostream.h>
#include<conio.h>
#include<alloc.h>
int *a,key,l,r,n;
void quick(int ,int );
void main()
{
 clrscr();
 int i;
 cout<<"Enter size";
 cin>>n;
 a=(int*)malloc(n*sizeof(int));
 cout<<"\nEnter elements";
 for (i=0;i<n;i++)
 cin>>a[i];
 cout<<"\nAfter quick sorting\n";
 quick(0,n-1);
 for(i=0;i<n;i++)
 cout<<" "<<a[i];
 getch();
}
void quick(int lb,int ub)
{
 int t;
 key=a[lb];
 l=lb+1;
 r=ub;
 while(l<=r)
 {
  while(key>a[l])
  l++;
  while(key<a[r])
  r--;
  if(l<=r)
  {
   t=a[l];
   a[l]=a[r];
   a[r]=t;
  // l++;
   //r--;
  }
 }
 t=a[lb];
 a[lb]=a[r];
 a[r]=t;
 cout<<endl;
 if(r>lb)
 quick(lb,r-1);
 if(l<ub)
 quick(r+1,ub);

}

C Program for Radix Sort

/**************************************************************************
////////////////// -*-  Program on Radix Sort  -*- \\\\\\\\\\\\\\\\\\\\\\\\
//////////////// --*-- By.:Vikrantsingh.M.Bisen --*-- \\\\\\\\\\\\\\\\\\\\\
***************************************************************************/
#include<iostream.h>
#include<math.h>
#include<conio.h>
int bucket[10][10];
int top[10];
int array[10],temp[10];
int i,j,k,size,length;
void initialize()
{
    for(i=0;i<10;i++)
    top[i]=0;
    for(i=0;i<10;i++)
    for(int r=0;r<10;r++)
    bucket[i][r]=0;
}
void copy_to_bucket()
{
    for(i=0;i<size;i++)
    {
     int lsb=temp[i]%10;
     bucket[lsb][top[lsb]]=array[i];
     top[lsb]++;
    }
    //---------Printing
    cout<<"\nBucket Contaion.:\n  ";
    for(i=0;i<10;i++)
    {for(int h=0;h<10;h++){ cout<<" "<<bucket[i][h];}cout<<endl;}
    cout<<"\n TOP Contain.:\n ";
    for(i=0;i<10;i++)
    cout<<" "<<top[i];

}
void copy_to_array()
{
    int k=0;
    for(i=0;i<10;i++)
    {
     int j=0;
     while(j<top[i])
     {
      array[k]=bucket[i][j];
      k++;j++;
     }
    }
    cout<<endl<<"Array Contain.:\n";
    for(i=0;i<size;i++)
    cout<<" "<<array[i];
}
void find_max_length()
{
  int y=1;
  length=0;
  for(i=0;i<size;i++)
  {
   int x=array[i];
   y=0;
   while(x!=0)
   {y++;x=x/10;}
   if(y>length)length=y;
  }

}
void main()
{
  clrscr();
  cout<<"Enter Size";
  cin>>size;
  for(int i=0;i<size;i++)
   cin>>array[i];
//  -------------
  for(i=0;i<size;i++)
  temp[i]=array[i];
  //-------
  find_max_length();
  //--------
  int p=1;
  for(int m=0;m<length;m++)
  {
    initialize();
    //-------assign from array to bucket
    copy_to_bucket();
    //-------assign from bucket to array
    copy_to_array();
    //---
   getch();
    for(i=0;i<size;i++)
    temp[i]=array[i]/pow(10,p);p++;
  }
 cout<<"\nSorted Array.:";
 for(i=0;i<size;i++)
 cout<<"  "<<array[i];
 getch();
}

C Program for Selection Sort

/*Selection Sort*/
#include<iostream.h>
#include<conio.h>
class sort
{
  private:
  int *a,size,i;
  public:
  sort()
  {
   cout<<"Enter size";
   cin>>size;
   a=new int[size];
  }
  void read()
  {
   cout<<"\nEnter Elements\n";
   for(i=0;i<size;i++)
   {
    cin>>a[i];
   }
  }
  void print()
  {
   cout<<"\nSorted Elements\n";
   for(i=0;i<size;i++)
   {
    cout<<" "<<a[i];
   }
  }
  void sorting()
  {
   for(i=0;i<size;i++)
   {
     for(int j=0;j<size;j++)
     {
       if(a[i]<a[j])
       {
    int t=a[i];
    a[i]=a[j];
    a[j]=t;
       }
     }
   }
  }
};
void main()
{
 sort a;
 a.read();
 a.sorting();
 a.print();
 getch();
}

C Program for Stack Push POP Operations

/*Push and Pop Operation on Stack*/
#include<iostream.h>
#include<process.h>
#include<conio.h>
class stack
{
  char ch;
  int x[10],i,TOP;
  public:
  stack()
  { TOP=0;}
  void push(int);
  int pop();
  void process();
};
void stack::push(int y)
{
  if (TOP>=10)
  {
  cout<<"SORRY! OVERFLOW ERROR......";
  exit(0);
  }
 x[++TOP]=y;
}
int stack::pop()
{
  if (TOP<1)
  {
  cout<<"SORRY! UNDERFLOW ERROR......";
  exit(0);
  }
  return(x[TOP--]);
}
void stack::process()
{
 while(1)
 {
//   clrscr();
   cout<<"WHAT WOULD YOU LIKE TO DO?\n";
   cout<<"1.PUSH \t 2.POP \t3.EXIT";
   ch=getche();
   switch(ch)
   {
    case '1':
       cout<<"\nEnter value.:";
       cin>>i;
       push(i);
      cout<<"\nVALUE IN STACK.:";
      for(i=1;i<=TOP;i++)
      cout<<"  "<<x[i];
       break;
    case '2':
      i=pop();
      cout<<"\nVALUE POPED.:"<<i;
      cout<<"\nVALUE IN STACK.:";
      for(i=1;i<=TOP;i++)
      cout<<"  "<<x[i];
      break;
    case '3':
      exit(0);
    default:
      cout<<"SORRY! Invalid Input";
   }
   getch();
 }
}
void main()
{
  clrscr();
  stack s;
  s.process();
  getch();
}

C Program for Stack using linked list

/* STACK OPERATION USING LINK LIST */
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdio.h>
#include<process.h>
struct node
{
  int item;
  struct node *add;
}*h;
void push(struct node *a,int x)
{
  if(a==NULL)
  {
   a=(node *)malloc(sizeof(node));
   h=a;
   a->item=x;
   a->add=NULL;
   return;
  }
  while(a->add!=NULL)
  {
   a=a->add;
  }
  a->add=(node *)malloc(sizeof(node));
  if(a->add==NULL){cout<<"\n\aStack Overflow.";return;}
  a=a->add;
  a->item=x;
  a->add=NULL;
}
int pop(struct node * a)
{
 struct node *temp;
 if(a->add==NULL)h=NULL;
 if(a==NULL){cout<<"\n\aStack Underflow.";return 0;}
 while(a->add!=NULL)
  {
    temp=a;
    a=a->add;
  }
  temp->add=NULL;
  int x=a->item;
  a=NULL;
  return x;
}
void show(struct node *a)
{
   cout<<"List.:";
   while(a!=NULL)
   {
     cout<<" "<<a->item;
     a=a->add;
   }
}
void main()
{
  clrscr();
  cout<<"\nSTACK OPERATION USING LINK LIST\n";
  h=NULL;
  while(1)
  {
   cout<<"\n1.PUSH\t2.POP\t3.EXIT\n";
   char ch;
   int n;
   ch=getch();
   switch(ch)
   {
     case '1':
      cout<<"Enter no.:";
      cin>>n;
      push(h,n);
      show(h);
      break;
     case '2':
      cout<<"Poped.:"<<pop(h);
      show(h);
      break;
     case '3':
       exit(0);
   }
  }
}

C Program to calculate depth of Tree

/* Height/Depth of a Tree */
#include<iostream.h>
#include<alloc.h>
#include<conio.h>
struct tree
{
       int item,level;
       struct tree *right,*left;
};
void create(struct tree *p)
{
     int choice;
     cout<<"\nEnter value";
     cin>>p->item;
     cout<<"\nDo you Want to Enter left Child of "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->left=(tree*)malloc(sizeof(tree));
          p->left->level=p->level+1;
          create(p->left);
     }
     else
     {
          p->left=NULL;
     }
     cout<<"Do you Want to Enter Right Child of  "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->right=(tree*)malloc(sizeof(tree));
          p->right->level=p->level+1;
          create(p->right);
     }
     else
     {
          p->right=NULL;
     }
}
static int max=0;
void findmax(tree *p)
{
  if(p==NULL)return;
  if(p->level>max)
  max=p->level;
  findmax(p->left);
  findmax(p->right);
}
int main()
{
     clrscr();
     struct tree *a;
     a=(tree*)malloc(sizeof(tree));
     a->level=0;
     cout<<"Enter Tree\n";
     create(a);
     findmax(a);
     cout<<"\nHeight of a Binary Tree is"<<max;
     getch();
     return 0;
}

c Program for Checking tree for mirror image

/* Mirror Image */
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
struct tree
{
       int item;
       struct tree *right,*left;
};
void create(struct tree *p)
{
     int choice;
     cout<<"Enter value";
     cin>>p->item;
     cout<<"Do you Want to Enter left Child of "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->left=(tree*)malloc(sizeof(tree));
          create(p->left);
     }
     else
     {
          p->left=NULL;
     }
     cout<<"Do you Want to Enter Right Child of  "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->right=(tree*)malloc(sizeof(tree));
          create(p->right);
     }
     else
     {
          p->right=NULL;
     }
}
void print_inorder(struct tree *p)
{
     if(p==NULL)return;
     print_inorder(p->left);
     cout<<endl<<p->item;
     print_inorder(p->right);
}
int compare(tree *p,tree *q)
{
   if(p==NULL&&q==NULL)
   return 1;
   else if(p==NULL||q==NULL)
   return 0;
   else if(p->item==q->item)
   {
    int s1=compare(p->left,q->right);
    int s2=compare(p->right,q->left);
    return s1*s2;
   }
   else
   return 0;
}
void main()
{
     clrscr();
     struct tree *a;
     cout<<"\nProgram To Compare Two Trees\n";
     cout<<"Enter First Tree\n";
     a=(tree*)malloc(sizeof(tree));
     create(a);
     struct tree *b;
     cout<<"\nEnter Second Tree\n";
     b=(tree*)malloc(sizeof(tree));
     create(b);
     if(compare(a,b)==1)
     cout<<"**Trees are MIRROR IMAGE of each other**";
     else
     cout<<"**Trees are not MIRROR IMAGE of each other**";
     getch();
}

Pattern Matching

#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
  char *t,*p;
  int i,j,k,m,n;
  clrscr();
  cout<<"\nEnter Text\n";
  cin>>t;
  cout<<"\nEnter Pattern\n";
  cin>>p;
  m=strlen(t);
  n=strlen(p);
  int c=0;
  for(i=0;i<m;i++)
  {
    for(j=i,k=0;t[j]==p[k]&&k<n;j++,k++)
    {
     if(k==n-1)
     c++;
    }
  }
  cout<<"Repeat for="<<c;
  getch();
}

C Program for Pattern Matching

#include<iostream.h>
#include<string.h>
#include<conio.h>
char x[20],y[20];
int m,n;
int counter(int i=0,int j=0,int k=0)
{
  int s=0;
  if(i>=m) return s;
  if(k>=n) s=1;
  if(x[j]==y[k]&&s!=1)
  s+=counter(i,j+1,k+1);
  else
  s+=counter(i+1,i+1,0);
  return s;
}
void main()
{
  clrscr();
  cout<<"\nEnter Text\n";
  cin>>x;
  cout<<"\nEnter Pattern\n";
  cin>>y;
  m=strlen(x);
  n=strlen(y);
  cout<<"\nPattern repeated for.:"<<counter();
  getch();
}

C Program for Postfix Expression Evaluation

#include<iostream.h>
#include<process.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
class stack
{
   double x[10],TOP,i,j,count,t,a,b,r;
   char exp[10][10],ch;
   public:
   void read();
   double pop();
   void push(double);
   double convert(double);
   double check(double);
   double process();
};
void stack::read()
{
  puts("Enter Expression in Postfix Form,Press E at End\n");
  count=0;
  do
  {
   count++;
   cin>>exp[count];
  }while(exp[count][0]!='E');
  puts("\n-------------------------------------------------");
}
double stack::pop()
{
  if(TOP<1)
  {
    cout<<"\nSORRY!Stack Underflow....";
    exit(0);
  }
  return(x[TOP--]);
}
void stack::push(double y)
{
  if(TOP==10)
  {
    cout<<"\nSORRY!Stack Overflow....";
    exit(0);
  }
  x[++TOP]=y;
  cout<<"\nElement in Stack.:\t";
  for(int t=1;t<=TOP;t++)
  cout<<"  "<<x[t];
}
double stack::check(double temp)
{
  if(strlen(exp[temp])>1)
  return 1;
  else
  {
    switch(exp[temp][0])
     {
      case '+':
      case '-':
      case '*':
      case '/':
      return 0;
      default:
      return 1;
     }
  }
}
double stack::convert(double temp)
{
   char *endptr;
   double l;
   l=strtod(exp[temp],&endptr);
   return l;
}
double stack::process()
{
  double t;
  TOP=0;
  int i=1;
  do
  {
   t=check(i);
   if(t==1)
   {
     t=convert(i);
     push(t);
   }
   else
   {
    a=pop();
    b=pop();
    switch(exp[i][0])
     {
      case '+':
     cout<<"\nOperator Detected.: +";
     push(a+b);
     break;
      case '-':
     cout<<"\nOperator Detected.: -";
     push(b-a);
     break;
      case '*':
     cout<<"\nOperator Detected.: *";
     push(a*b);
     break;
      case '/':
     cout<<"\nOperator Detected.: /";
     push(b/a);
     break;
      default:
     cout<<"\nSORRY!Invalid Input....";
     exit(0);
     }
   }
   i++;
  }while(exp[i][0]!='E');
  return(x[TOP]);
}
void main()
{

  char ch;
  double i,j;
  clrscr();
  stack x;
  puts("===================================================");
  cout<<"Enter Expression in POSTFIX FORM.\n";
  cout<<"Enter E at the End of Expression.\n";
  puts("===================================================");
  x.read();
  i=x.process();
  puts("\n-------------------------------------------------");
  cout<<"\t-*- Result="<<i<<"  -*-";
  puts("\n-------------------------------------------------");
  getch();
}

C++ Program for Linear Search

#include<iostream.h>
#include<conio.h>
class Array
{
     private:
     int x[10],key,pos,n,i;
     public:
     void read();
     void process();
};
void Array::read()
     {
       cout<<"\n--------------------------------------------------------------";
       cout<<"\nEnter The Size Of Array\n";
       cin>>n;
       cout<<"\n--------------------------------------------------------------";
       cout<<"\nEnter Elements of Array\n";
       for(i=1;i<=n;i++)
       cin>>x[i];
       cout<<"\n--------------------------------------------------------------";
     }
void Array::process()
     {
       cout<<"\nEnter Element to Search\n";
       cin>>key;
       cout<<"\n--------------------------------------------------------------";
       x[n+1]=key;
       for(i=1;i<=n;i++)
       {
     if(x[i]==key)
     {
      pos=i;
      break;
     }
       }
       if(pos<=n)
       cout<<"\nSearch Successfull The Element is located at position no.:"<<pos<<".";
       else
       cout<<"\nSORRY!Search Unuccessfull The Element is NOT FOUND..";
     cout<<"\n--------------------------------------------------------------";
     }
void main()
{
  clrscr();
  Array a;
  cout<<"\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
  cout<<"\n       -*-   Program For Linear Search Method   -*-";
  cout<<"\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
  a.read();
  a.process();
  getch();
}

C Program for Simple Linked List

#include<iostream.h>
#include<conio.h>
struct node
{
       int item;
       struct node *add;
};
void create(struct node *p)
{
     int choice;
     cout<<"Enter value";
     cin>>p->item;
     cout<<"Do you Want to Continue 1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
                  p->add=(node*)malloc(sizeof(node));
                  create(p->add);            
     }
     else
     {
                  p->add=NULL;
     }
                  return;         
}
void print(struct node *p)
{
     if(p==NULL)return;
     cout<<p->item;
     print(p->add);         
}
int main()
{
     struct node *a;
     a=(node*)malloc(sizeof(node));
     create(a);
     print(a);
     getch();
     return 0;
}

C Program for Merge Sorting

#include<iostream.h>
#include<conio.h>
int x[10],z[10];
void mergesort(int lb,int m,int ub)
{
int k=0;int j=lb;int i=m+1;
while(j<=m)
{
 while(i<=ub)
 {
  if(x[j]>x[i])
  {
   z[k]=x[i];
   i++;k++;
  }
  else
  {
   z[k]=x[j];
   j++;k++;
   break;
  }
  if(j>m)
  break;
 }
  if(i>ub)
  break;
}
while(i<=ub)
{
 z[k]=x[i];
 i++;
 k++;
}
while(j<=m)
{
 z[k]=x[j];
 j++;
 k++;
}
cout<<"\nAfter Merging\n";
for(j=lb,i=0;j<=ub;i++,j++)
{
 x[j]=z[i];
 cout<<" "<<x[j];
}
}void merge(int lb,int ub)
{
  int m=(lb+ub)/2;
  if(lb<ub)
  {
   merge(lb,m);
   merge(m+1,ub);
   mergesort(lb,m,ub);
  }
}
void main()
{
cout<<"Program on Merge Sorting\n";
cout<<"Enter Size";
int s;
cin>>s;
cout<<"Enter ";
for(int i=0;i<s;i++)
{
 cin>>x[i];
}
merge(0,s-1);
cout<<"\nFinally Sorted Array\n";
for(i=0;i<s;i++)
{
 cout<<" "<<x[i];
}
getch();
}

C Program for Linear Queue

/*Insertion And Deletion in Linear Queue*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
class queue
{
  int x[10],rear,front;
  public:
  queue()
  {
   front=1;
   rear= 0;
  }
  void insert(int y)
  {
   if(rear>10)cout<<"Overflow";
   x[++rear]=y;
  }
  int del()
  {
   if(front<=1||front>=10){cout<<"underflow";exit(0);}
   return x[front++];
  }
  void process()
  {
    while(1)
    {
//      clrscr();
      cout<<"\nEnter Choice\n 1.Insert  2.Delete  3.Exit";
      char ch;
      int t;
      ch=getch();
      switch(ch)
      {
    case '1':
    cout<<"\nEnter no.:";
    cin>>t;
    insert(t);
    cout<<"Elements in Queue.:";
    for(int i=front;i<=rear;i++)
    cout<<" "<<x[i];
    break;
    case '2':
    t=del();
    cout<<"\nElement POPED.:"<<t;
    cout<<"\nElements in Queue.:";
    for(i=front;i<=rear;i++)
    cout<<" "<<x[i];
    break;
    default:
    exit(0);
     }
     getch();
    }
  }
};
void main()
{
  clrscr();
  queue q;
  q.process();
}

C Program for Double Linked List

#include<iostream.h>
#include<alloc.h>
#include<conio.h>
struct node
{
  int item;
  struct node *n,*p;
}*a;
void create(struct node *q)
{
     int choice;
     cout<<"Enter value";
     cin>>q->item;
     cout<<"Do you Want to Continue 1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {

          q->n=(node*)malloc(sizeof(node));
          q->n->p=q;
          create(q->n);
     }
     else
     {
          q->n=NULL;
     }
}
void print(struct node *q)
{
     if(q==NULL)return;
     cout<<"       "<<q<<"\t"<<q->p<<"\t"<<q->item<<"\t"<<q->n<<endl;
     print(q->n);
}
void del(struct node *q,int x)
{
    while(q->item!=x)
    q=q->n;
    if(q->p==NULL)       //First node.
    {
     q->n->p=NULL;
     a=q->n;
     return;
    }
    if(q->n==NULL)       //Last node.
    {
     q->p->n=NULL;
     return;
    }                     //Intermediate value.
    q->p->n=q->n;
    q->n->p=q->p;
     free (q);
}
void ins(struct node *q,int loc,int value)
{
    struct node *t;
    t=(node*)malloc(sizeof(node));
    t->item=value;
    if(loc==1)             //first
    {
    t->n=q;
    t->p=NULL;
    q->p=t;
    a=t;
    return;
    }
    for(int i=1;i<=loc;i++)
    q=q->n;
    if(q->n==NULL)         //Last
    {
     q->n=t;
     t->p=q;
     t->n=NULL;
    }
    else                   //Intermediate
    {
    t->n=q->n;
    t->p=q;
    q->n->p=t;
    t->n=t;
    }
}
void main()
{
 clrscr();
 cout<<"\n****************************************************************n";
 cout<<"\n Program on Double Link List\n";
 cout<<"\n****************************************************************n";
 a=(node*)malloc(sizeof(node));
 a->p=NULL;
 cout<<"\n===========================================================\n";
 create(a);
 cout<<"\n===========================================================\n";
 cout<<"\n      Address of node    Pre node     Value     Next node\n";
 cout<<"\n===========================================================\n";
 print(a);
 cout<<"\n===========================================================\n";
 cout<<"Enter value to delete";
 int x;
 cin>>x;
 del(a,x);
 cout<<"\n===========================================================\n";
 cout<<"\n      Address of node    Pre node     Value     Next node\n";
 cout<<"\n===========================================================\n";
 print(a);
 cout<<"\n===========================================================\n";
 cout<<"Enter location and value";
 int y;
 cin>>x>>y;
 ins(a,x,y);
 cout<<"\n===========================================================\n";
 cout<<"\n      Address of node    Pre node     Value     Next node\n";
 cout<<"\n===========================================================\n";
 print(a);
 cout<<"\n===========================================================\n";
 getch();
}

c program for Expression Tree

#include<iostream.h>
#include<string.h>
#include<alloc.h>
#include<conio.h>
union info
{
  int x;
  char ch;
};
struct tree
{
  char data;
  union info data2;
  struct tree *left,*right;
}h;
char exp[20];
int n;

int check_precedency(char ch)
{

  switch(ch)
  {
    case '+':
    case '-':
    return 1;
    case '*':
    case '/':
    return 2;
    default:
    return 3;
  }
}
int detect(char ch)
{
  switch(ch)
  {
    case '+':
    case '-':
    case '*':
    case '/':
    return 1;
    default:
    return 0;
  }
}
int convert(char ch)
{
  switch(ch)
  {
    case '1':
    return 1;
    case '2':
    return 2;
    case '3':
    return 3;
    case '4':
    return 4;
    case '5':
    return 5;
    case '6':
    return 6;
    case '7':
    return 7;
    case '8':
    return 8;
    case '9':
    return 9;
    default:
    return 0;
  }
}
int convertCharToInt(char exp[],int l,int u)
{
 char s[10];
 int k=0;
 for(int i=l;i<u;i++,k++)
 {
  s[k]=exp[i];
 }
 s[k]='\0';
 float x=strtod(s,)
}
void Exp_tree(tree*q,int lb,int ub)
{
 if(lb>ub)return;
 if(lb>=ub)
 {
 q->data=exp[lb];
 q->data2.x=convert(exp[lb]);
 q->right=NULL;
 q->left=NULL;
 return;
 }
 int previous=4,precedency,position;
 for(int i=ub;i>=lb;i-- )
 {
   precedency=check_precedency(exp[i]);
   if(previous>precedency)
   {position=i;previous=precedency;}
 }
  i=position;
  q->data2.ch=exp[i];
  q->data=exp[i];
  q->left=(tree*)malloc(sizeof(tree));
  Exp_tree(q->left,lb,i-1);
  q->right=(tree*)malloc(sizeof(tree));
  Exp_tree(q->right,i+1,ub);
}

int evaluate(tree* q)
{
  switch(q->data2.ch)
  {
    case '+':
      return(q->left->data2.x+q->right->data2.x);
    case '-':
      return(q->left->data2.x-q->right->data2.x);
    case '*':
      return(q->left->data2.x*q->right->data2.x);
    case '/':
      return(q->left->data2.x/q->right->data2.x);
  }
}
void find(tree *q)
{
  if(q->left==NULL||q->right==NULL)return;
  find(q->left);
  find(q->right);
  if(q->left->right==NULL&&q->left->left==NULL&&q->right->right==NULL&&q->right->left==NULL)
  {
   cout<<"\nLeaf Node.: "<<q->left->data2.x<<" "<<q->right->data2.x;
   q->data2.x=evaluate(q);
   cout<<"\tOperator.: "<<q->data<<"Result.: "<<q->data2.x;
   q->left=NULL;
   q->right=NULL;
  }
}
void print_inorder(struct tree *p)
{
     if(p==NULL)return;
     print_inorder(p->left);
     cout<<" "<<p->data;
     print_inorder(p->right);
}
void print_preorder(struct tree *p)
{
     if(p==NULL)return;
     cout<<p->data;
     print_preorder(p->left);
     print_preorder(p->right);
}
void print_postorder(struct tree *p)
{
     if(p==NULL)return;
     print_postorder(p->left);
     print_postorder(p->right);
     cout<<p->data;
}

void main()
{
 clrscr();
 cout<<"\nEnter Infix Expression\n";
 cin>>exp;
 n=strlen(exp);
 tree *q;
 q=(tree*)malloc(sizeof(tree));
 Exp_tree(q,0,n-1);
 cout<<"\nInorder.: ";
 print_inorder(q);
 cout<<"\nPreorder.: ";
 print_preorder(q);
 cout<<"\nPostorder.: ";
 print_postorder(q);
 find(q);
 cout<<"\nFinal Solution.: "<<q->data2.x;
 getch();
}

c program for Fibonacci series

#include<iostream.h>
#include<conio.h>
int fibo(int,int=1,int=0,int=1);
int fibo(int m,int n,int f1,int f2)
{
  int s=0;
  if(n>=m)
  return 1;
  s=f1+f2;
  cout<<"\t"<<s;
  s+=fibo(m,++n,f2,s);
  return s;
}
void main()
{
  int i;
  clrscr();
  cout<<"Sum of Fibo..Series..";
  cout<<"\nEnter no.";
  cin>>i;
  cout<<"\n1";
  i=fibo(i);
  cout<<"\nSum of First Fibo..no..is\n";
  cout<<i;
  getch();
}

C Program on Graph traversing BFS and DFS

/*Link List Representation of Graph*/
#include<iostream.h>
#include<alloc.h>
#include<conio.h>
struct link
{
 struct node *points;
 struct link *next;
};
struct node
{
 int item,visit;
 struct link *adjacent;
 struct node *next;
}*head;
//---------------------------------------------------
//                Create Graph
//---------------------------------------------------

void create(struct node *h)
{
  cout<<"Enter Value of Vertex";
  cin>>h->item;
  h->visit=0;
  cout<<"Continue(Y/N)";
  if(getche()=='y')
  {
   h->next=(node*)malloc(sizeof(node));
   create(h->next);
  }
  else
  {
   h->next=NULL;
  }
}
node *search(node *h,int p)
{
 while(h!=NULL)
 {
  if(h->item==p)
  {return h;}
  else
  { h=h->next;}
 }
 return NULL;
}
void getAdjacent(node *h,link *l)
{
  if(h==NULL)return;
  if(l==NULL)
  {
   l=(link*)malloc(sizeof(link));
   h->adjacent=l;
  }
  cout<<"\nDoes the Vertex "<<h->item<<" has an adjacent (Y/N)?";
  if(getche()=='y')
  {

    int t;
    cin>>t;
    l->points=search(head,t);
    l->next=(link*)malloc(sizeof(link));
    getAdjacent(h,l->next);
   }
  else
  {
    l->next=NULL;
    getAdjacent(h->next,l->next);
  }
}
//---------------------------------------------------
//      Print Complete Graph With Adjacent
//---------------------------------------------------

void print (node *p)
{
  if(p==NULL)return;
  link *l=p->adjacent;
  cout<<"\nVertex.: "<<p->item;
  cout<<"\t Adjacent.: ";
  while(l->next!=NULL&&l!=NULL)
  {
   cout<<" "<<l->points->item;
   l=l->next;
  }
  print(p->next);
}

//---------------------------------------------------
//                D.F.S
//---------------------------------------------------
void DFS(node *q)
{
 if(q->visit==1)return;
 else
 {
  q->visit=1;
  cout<<" "<<q->item;
 }
 link *l;
 l=q->adjacent;
 while(l->next!=NULL)
 {
  DFS(l->points);
  l=l->next;
 }
}
//---------------------------------------------------
//                B.F.S
//---------------------------------------------------
struct queue
{
  struct node *element;
  struct queue *next;
}*Fq,*Rq;
void addQueue(node *n)
{
  if(Fq==NULL)
  {
   Rq=Fq=(queue*)malloc(sizeof(queue));
   Rq->element=n;
   return;
  }
  Rq->next=(queue*)malloc(sizeof(queue));
  Rq=Rq->next;
  Rq->element=n;
  Rq->next=NULL;
}
node *delQueue()
{
  if(Fq==NULL) return NULL;
  node *t=Fq->element;
  if(Rq==Fq)
  {
   Rq=Fq=NULL;
   return t;
  }
  queue *q=Fq;
  Fq=Fq->next;
  free(q);
  return t;
}
void BFS(node *h)
{
 node *t=h;
 while(t!=NULL)
 {
  t->visit=0;
  t=t->next;
 }
 addQueue(h);
 while(Fq!=NULL||Rq!=NULL)
 {
   t=delQueue();
   if(t->visit==0)
   {
     cout<<" "<<t->item;
     t->visit=1;
     link *l;
     l=t->adjacent;
     while(l->next!=NULL)
     {
      addQueue(l->points);
      l=l->next;
     }
   }
 }
}
//---------------------------------------------------
//                Main Function
//---------------------------------------------------
void main()
{
 clrscr();
 node *a;
 a=(node*)malloc(sizeof(node));
 head=a;
 create(a);
 link *l;
 l=NULL;
 getAdjacent(a,l);
 print(a);
 cout<<"\nD.F.S\n";
 DFS(a);
 Fq=NULL;
 Rq=NULL;
 cout<<"\nB.F.S\n";
 BFS(a);
 getch();
}

c++ program for Infix to Postfix conversion

#include<iostream.h>
#include<process.h>
#include<string.h>
//#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
class stack
{
   double x[10],TOP_p,TOP_e,TOP_s,i,j,t;
   char exp[10][10],p[10][10],stack[10][10];
   public:
   stack();
   void pop();
   void push();
   double check();
   void intopost();
   void show();
};
stack::stack()
{
  TOP_p=TOP_s=TOP_e=0;
}
void stack::pop()
{
  if(TOP_s<1)
  {
    cout<<"\nSORRY!Stack Underflow....";
    exit(0);
  }
  TOP_p++;
  strcpy(p[TOP_p],stack[TOP_s]);
  TOP_s--;
  cout<<"\nElement in Polish.:\t";
  for(int t=1;t<=TOP_p;t++)
  cout<<"  "<<p[t];
}
void stack::push()
{
  if(TOP_s==10)
  {
    cout<<"\nSORRY!Stack Overflow....";
    exit(0);
  }
  TOP_s++;
  strcpy(stack[TOP_s],exp[TOP_e]);
  TOP_e++;
  cout<<"\nElement in Stack.:\t";
  for(int t=1;t<=TOP_s;t++)
  cout<<"  "<<stack[t];
}
double stack::check()
{
   double s,e,t;
   //stack[st]   Vs    exp[et]
   if(TOP_s<1)return 1;
  switch(stack[TOP_s][0])
  {
    case '^':
    s=3;
    break;
    case '*':
    case '/':
    s=2;
    break;
    case '+':
    case '-':
    s=1;
    break;
    default:
    s=4;
  }
  switch(exp[TOP_e][0])
  {
    case '^':
    e=3;
    break;
    case '*':
    case '/':
    e=2;
    break;
    case '+':
    case '-':
    e=1;
    break;
    default:
    e=4;
  }
  if(e>s) return 1;       //push
  else return 0;          //pop
}
void stack::intopost()
{
  cout<<"Enter exp";
  int i=0;
  do
  {
   i++;
   cin>>exp[i];
  }while(exp[i][0]!='#');
  puts("\n-------------------------------------------------");
  TOP_e=1;
  int j=1;
  push();
  while(exp[++j][0]!='#')
  {

    i=check();
    if(i==1)
    push();
    else
    {
      while(!i)
      {
       pop();
       i=check();
      }
      push();
     }
  }
  while(TOP_s>=1)
  {
  TOP_p++;
  strcpy(p[TOP_p],stack[TOP_s]);
  TOP_s--;
  }
  cout<<"\nElement in pol.:\t";
  for(i=1;i<=TOP_p;i++)
  cout<<"  "<<p[i];
}
void stack::show()
{
  cout<<"\nExpression in Postfix Form.:\n\n";
  for(i=1;i<=TOP_p;i++)
  cout<<"  "<<p[i];
}
void main()
{

  char ch;
  double i,j;
  clrscr();
  stack x;
  puts("===================================================");
  cout<<"Enter Expression in INFIX FORM.\n";
  cout<<"Enter '#' at the End of Expression.\n";
  puts("===================================================");
  x.intopost();
  puts("\n-------------------------------------------------");
  x.show();
  puts("\n-------------------------------------------------");
  getch();
}

c++ Program for Infix to Prefix Conversion

#include<iostream.h>
#include<process.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
class stack
{
   double x[10],TOP_p,TOP_e,TOP_s,i,j,t;
   char exp[10][10],p[10][10],stack[10][10];
   public:
   stack();
   void pop();
   void push();
   double check();
   void intopre();
   void show();
};
stack::stack()
{
  TOP_p=TOP_s=TOP_e=0;
}
void stack::pop()
{
  if(TOP_s<1)
  {
    cout<<"\nSORRY!Stack Underflow....";
    exit(0);
  }
  TOP_p++;
  strcpy(p[TOP_p],stack[TOP_s]);
  TOP_s--;
  cout<<"\nElement in Polish.:\t";
  for(int t=TOP_p;t>=1;t--)
  cout<<"  "<<p[t];
}
void stack::push()
{
  if(TOP_s==10)
  {
    cout<<"\nSORRY!Stack Overflow....";
    exit(0);
  }
  TOP_s++;
  strcpy(stack[TOP_s],exp[TOP_e]);
  TOP_e--;
  cout<<"\nElement in Stack.:\t";
  for(int t=1;t<=TOP_s;t++)
  cout<<"  "<<stack[t];
}
double stack::check()
{
   double s,e,t;
   //stack[st]   Vs    exp[et]
   if(TOP_s<1)return 1;
  switch(stack[TOP_s][0])
  {
    case '^':
    s=3;
    break;
    case '*':
    case '/':
    s=2;
    break;
    case '+':
    case '-':
    s=1;
    break;
    default:
    s=4;
  }
  switch(exp[TOP_e][0])
  {
    case '^':
    e=3;
    break;
    case '*':
    case '/':
    e=2;
    break;
    case '+':
    case '-':
    e=1;
    break;
    default:
    e=4;
  }
  if(e>=s) return 1;       //push
  else return 0;          //pop
}
void stack::intopre()
{
  cout<<"Enter exp";
  int i=0;
  do
  {
   i++;
   cin>>exp[i];
  }while(exp[i][0]!='#');
  puts("\n-------------------------------------------------");
  TOP_e=i-1;
  push();
  while(TOP_e>=0)
  {

    i=check();
    if(i==1)
    push();
    else
    {
      while(!i)
      {
       pop();
       i=check();
      }
      push();
     }
  }
  while(TOP_s>=1)
  {
  TOP_p++;
  strcpy(p[TOP_p],stack[TOP_s]);
  TOP_s--;
  }
  cout<<"\nElement in pol.:\t";
  for(i=TOP_p;i>=1;i--)
  cout<<"  "<<p[i];
}
void stack::show()
{
  cout<<"\nExpression in Prefix Form.:\n\n";
  for(i=TOP_p;i>=1;i--)
  cout<<"  "<<p[i];
}
void main()
{
  clrscr();
  stack x;
  puts("===================================================");
  cout<<"Enter Expression in INFIX FORM.\n";
  cout<<"Enter '#' at the End of Expression.\n";
  puts("===================================================");
  x.intopre();
  puts("\n-------------------------------------------------");
  x.show();
  puts("\n-------------------------------------------------");
  getch();
}

C Program for Insertion Sort

/*Insertion Sort*/
#include<iostream.h>
#include<conio.h>
class sort
{
  private:
  int *a,size,i;
  public:
  sort()
  {
   cout<<"Enter size";
   cin>>size;
   a=new int[size];
  }
  void read()
  {
   cout<<"\nEnter Elements\n";
   for(i=0;i<size;i++)
   {
    cin>>a[i];
   }
  }
  void print()
  {
   cout<<"\nSorted Elements\n";
   for(i=0;i<size;i++)
   {
    cout<<" "<<a[i];
   }
  }
  void sorting()
  {
   for(i=1;i<size;i++)
   {
     for(int j=0;j<i;j++)
     {
       if(a[i]<a[j])
       {
    int t=a[i];
    for(int k=i;k>j;k--)
    a[k]=a[k-1];
    a[j]=t;
       }
     }
   }
  }
};
void main()
{
 sort a;
 a.read();
 a.sorting();
 a.print();
 getch();
}

C Program for Bubble Sort

#include<iostream.h>
#include<conio.h>
#include<alloc.h>
class array
{
  int **x,r,c,i,j;
  public:
  void sort(int**,int,int);
  void process();
};
void array::sort(int **x,int r,int c)
{
  int i,j,k,l,t;
  for(k=r-1;k>=0;k--)
  {
   for(l=c-1;l>=0;l--)
   {
    for(i=0;i<r;i++)
    {
     for(j=0;j<c;j++)
     {
       if(j==(c-1)&&i==(r-1))
       break;
       if(j==l&&i==k)
       break;
       if(j==(c-1)&&i<(r-1))
    {
     if(x[i][j]>x[i+1][0])
     {
        t=x[i][j];
        x[i][j]=x[i+1][0];
        x[i+1][0]=t;
      }
     }
       else
    {
     if(x[i][j]>x[i][j+1])
     {
        t=x[i][j];
        x[i][j]=x[i][j+1];
        x[i][j+1]=t;
     }
      }
    }
   }
  }
 }
}
void array::process()
{
cout<<"Enter Size of 2'D Array\n";
cin>>r>>c;
x=(int**)malloc((r*c)*2);
cout<<"--------------------------------------\n";
cout<<"Enter Elements of 2'D Array\n";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>x[i][j];
sort(x,r,c);
cout<<"--------------------------------------\n";
cout<<"After Sorting \n";
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 cout<<"\t"<<x[i][j];
 cout<<endl;
}
cout<<"--------------------------------------\n";


}
void main()
{
clrscr();
array a;
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
cout<<"   -*- Program for Bubble Sorting For 2'D Array -*-\n";
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
a.process();
cout<<"--------------------------------------\n";
getch();
}

C Program for Character to Value Converter

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
  char *ch;
  double x=0,i=0,k=0,l=10;
  cout<<"Enter\n";
  cin>>ch;
  while(ch[i]!='\0')
  {
    if(k==0)
    {
     switch(ch[i])
     {
    case '0':
         x=x*10+0;
         break;
    case '1':
         x=x*10+1;
         break;
    case '2':
         x=x*10+2;
         break;
    case '3':
         x=x*10+3;
         break;
    case '4':
         x=x*10+4;
         break;
    case '5':
         x=x*10+5;
         break;
    case '6':
         x=x*10+6;
         break;
    case '7':
         x=x*10+7;
         break;
    case '8':
         x=x*10+8;
         break;
    case '9':
         x=x*10+9;
         break;

    case '.':
         k=1;
         break;
     }
    }
    else
    {
     switch(ch[i])
     {
    case '0':
         x=x+(0/(l));
          break;
    case '1':
         x=x+(1/(l));
         break;
    case '2':
         x=x+(2/(l));
         break;
    case '3':
         x=x+(3/(l));
         break;
    case '4':
         x=x+(4/(l));
         break;
    case '5':
         x=x+(5/(l));
         break;
    case '6':
         x=x+(6/(l));
         break;
    case '7':
         x=x+(7/(l));
         break;
    case '8':
         x=x+(8/(l));
         break;
    case '9':
         x=x+(9/(l));
         break;

    case '.':
         cout<<"Invalid ";
         break;
     }
     l*=10;
    }
      i++;
  }


  cout<<"Value"<<x;
  getch();
}

C Program for Circular Link List

/*Creating Circular Link List*/
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int item;
struct node *add;
}*last,*start;
//---------
void creat(struct node *q)
{
  if(q==NULL)
  {
    start=(node*)malloc(sizeof(node));
    q=start;
  }
  cout<<"\nEnter Value.:";
  cin>>q->item;
  cout<<"Continue 1.Yes/2.NO.:";
  int choice;
  cin>>choice;
  if(choice==1)
  {
    q->add=(node*)malloc(sizeof(node));
    creat(q->add);
  }
  else
  {
   last=q;
    last->add=start;
  }
}
//-----------
void print(struct node *q)
{
 cout<<" "<<q->item;
 if(q==last)return;
 print(q->add);
}
//------------
void ins(struct node *q,int num)
{
 struct node *t1,*t2;
 t2=(node*)malloc(sizeof(node));
 t2->item=num;
 if(q->item>num)
 {
  t2->add=start;
  start=t2;
 }
 else if(last->item<num)
 {
   last->add=t2;
   last=t2;
   last->add=start;
 }
 else
 {
  t1=q;
  while(q->item<num&&q!=last)
  { t1=q;q=q->add;}
  t2->add=t1->add;
  t1->add=t2;
 }
}
void del(node *q,int num)
{
 if(q->item==num)
 {
  start=start->add;
  free(q);
  return;
 }
 node *t1;
 t1=q;
 q=q->add;
 while(q->item!=num&&q!=start)
 { t1=q;q=q->add;}
 if(q==start)
 {
  cout<<"\nNot Found";
  return;
 }
 if(q==last)
 {
  last=t1;
  last->add=start;
 }
 else
 { t1->add=q->add;}

 free(q);
}
void main()
{
clrscr();
start=NULL;
cout<<"\nEnter List in Incresing Order\n";
creat(start);
print(start);
cout<<"\nEnter Value To Insert";
int x;
cin>>x;
ins(start,x);
cout<<"After Inserting";
print(start);
cout<<"\nEnter Value To Delete";
cin>>x;
del(start,x);
cout<<"After Deleting";
print(start);
getch();
}

C Program for Circular Queue

#include<iostream.h>
#include<process.h>
#include<conio.h>
class queue
{
  int x[10],rear,front,i,t;
  public:
  queue()
  {
   front=-1;
   rear=-1;
   for(int i=0;i<10;i++)
   x[i]=0;
  }
  void push(int y)
  {
   if((front==0&&rear==9)||(front==rear+1))
   {
       cout<<"Overflow";
       return;
   }
   if(rear==9)
    rear=0;
   else
    rear++;
   x[rear]=y;
   if(front==-1)front=0;
 }
  int pop()
  {
   if((front==-1))
   {
       cout<<"Underflow";
       return NULL;
   }
   t=x[front];
   x[front]=0;
   if(rear==front)
   front=rear=-1;
   else
   {
    if(front==9)
      front=0;
     else
      front++;
   }
   return t;
  }
  void show()
  {
    if(front==-1&&rear==-1)return;
    if(front<=rear)
    {
     for(int i=front;i<=rear;i++)
     cout<<" "<<x[i];
    }
    else
    {
     i=front;
     while(i!=rear)
     {
      cout<<" "<<x[i];
      i++;
      if(i==10)
      i=0;
     }
    }
  }
  void process()
  {
    clrscr();
    while(1)
    {
//      clrscr();
      cout<<"\nEnter Choice\n 1.PUSH  2.POP  3.Exit";
      char ch;
      int t;
      ch=getche();
      switch(ch)
      {
    case '1':
    cout<<"\nEnter no.:";
    cin>>t;
    push(t);
    cout<<"Elements in Queue.:";
    show();
    break;
    case '2':
    t=pop();
    cout<<"\nElement POPED.:"<<t;
    cout<<"\nElements in Queue.:";
    show();
    break;
    default:
    exit(0);
     }
     getch();
    }
  }
};
void main()
{
  queue q;
  q.process();
}

C Program to Compare two Trees

/* Compare two trees Considering Branches As Well AS Values*/
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
struct tree
{
       int item;
       struct tree *right,*left;
};
void create(struct tree *p)
{
     int choice;
     cout<<"\nEnter value";
     cin>>p->item;
     cout<<"\nDo you Want to Enter left Child of "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->left=(tree*)malloc(sizeof(tree));
          create(p->left);
     }
     else
     {
          p->left=NULL;
     }
     cout<<"Do you Want to Enter Right Child of  "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->right=(tree*)malloc(sizeof(tree));
          create(p->right);
     }
     else
     {
          p->right=NULL;
     }
}
void print_inorder(struct tree *p)
{
     if(p==NULL)return;
     print_inorder(p->left);
     cout<<endl<<p->item;
     print_inorder(p->right);
}
int compare(tree *p,tree *q)
{
   if(p==NULL&&q==NULL)
   return 1;
   else if(p==NULL||q==NULL)
   return 0;
   else if(p->item==q->item)
   {
    int s1=compare(p->left,q->left);
    int s2=compare(p->right,q->right);
    return s1*s2;
   }
   else
   return 0;
}
void main()
{
     clrscr();
     struct tree *a;
     cout<<"\nProgram To Compare Two Trees\n";
     cout<<"Enter First Tree\n";
     a=(tree*)malloc(sizeof(tree));
     create(a);
     struct tree *b;
     cout<<"\nEnter Second Tree\n";
     b=(tree*)malloc(sizeof(tree));
     create(b);
     if(compare(a,b)==1)
     cout<<"Both Trees Have Similar Branches With Values";
     else
     cout<<"Both Trees doesn't have Similar Branches With Values";
     getch();
}

C Program to Count number of nodes in Binary Tree

#include<iostream.h>
#include<alloc.h>
#include<conio.h>
struct tree
{
       int item;
       struct tree *right,*left;
};
void create(struct tree *p)
{
     int choice;
     cout<<"\nEnter value";
     cin>>p->item;
     cout<<"Do you Want to Enter left Child of "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->left=(tree*)malloc(sizeof(tree));
          create(p->left);
     }
     else
     {
          p->left=NULL;
     }
     cout<<"Do you Want to Enter Right Child of  "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->right=(tree*)malloc(sizeof(tree));
          create(p->right);
     }
     else
     {
          p->right=NULL;
     }
}
static int i=0;
void count(struct tree *p)
{
     if(p==NULL)return;
     i++;
     count(p->left);
     count(p->right);
}
int main()
{
     clrscr();
     struct tree *a;
     a=(tree*)malloc(sizeof(tree));
     cout<<"Enter Tree\n";
     create(a);
     count(a);
     cout<<"\nTotal Nodes="<<i;
     getch();
     return 0;
}

C Program for Deleting node from Binary Search Tree

#include<iostream.h>
#include<alloc.h>
#include<conio.h>
struct tree
{
  int item;
  struct tree *left,*right;
}*h;
void join(tree *p,tree *q)
{
  if(p==NULL)
  {h=q;return;}
  if(p->item<q->item)
  {
    if(p->right!=NULL)
    join(p->right,q);
    else
    p->right=q;
  }
  else
  {
    if(p->left!=NULL)
    join(p->left,q);
    else
    p->left=q;
  }
}
void BST()
{
  tree *q;
  cout<<"Enter Value";
  q=(tree*)malloc(sizeof(tree));
  cin>>q->item;
  q->right=NULL;
  q->left=NULL;
  join(h,q);
  cout<<"Continue y/n";
  if(getche()=='y')
  BST();
  else
  return;
}
void print_inorder(struct tree *p)
{

     if(p==NULL)return;
     print_inorder(p->left);
     cout<<endl<<p->item;
     print_inorder(p->right);
}
void del(tree *q,int num)
{
  tree *parent=NULL;
  while(q!=NULL)
  {
    if(q->item==num)
    break;
    else if(q->item>num)
    {parent=q;q=q->left;}
    else
    {parent=q;q=q->right;}
  }
  cout<<"Parent"<<parent->item;

  if(q->left==NULL&&q->right==NULL)
  {
   if(parent->item<num)parent->right=NULL;
   else parent->left=NULL;
   free(q);
  }
  else if(q->left!=NULL&&q->right==NULL)
  {
   if(parent->item<num)parent->right=q->left;
   else parent->left=q->left;
   free(q);
  }
  else if(q->left==NULL&&q->right!=NULL)
  {
   if(parent->item<num)parent->right=q->right;
   else parent->left=q->right;
   free(q);
  }
  else
  {
    tree *temp=NULL,*inorder_successor=NULL;
    temp=q;
    inorder_successor=q->right;
    while(inorder_successor->left!=NULL)
    {
     temp=inorder_successor;
     inorder_successor=inorder_successor->left;
    }
    if(inorder_successor->right!=NULL)
    temp->left=inorder_successor->right;
    else if(q!=temp)
    temp->left=NULL;
    else
    temp->right=NULL;

    inorder_successor->left=q->left;
    inorder_successor->right=q->right;

    if(parent->item<inorder_successor->item)parent->right=inorder_successor;
    else parent->left=inorder_successor;
  }
}
void main()
{
 clrscr();
 h=NULL;
 cout<<"\nEnter Values to Build a B.S.T\n";
 BST();
 cout<<"\nInorder Traversal of B.S.T";
 print_inorder(h);
 cout<<"Enter";
 int x;
 cin>>x;
 del(h,x);
 print_inorder(h);
 getch();
}

C++ program for Binary Search Tress (BST)

#include<iostream.h>
#include<alloc.h>
#include<conio.h>
struct tree
{
       int value;
       struct tree *right,*left;
};
void create(struct tree *h,struct tree *q)
{
   if(q->value>h->value)
   {
     if(h->right!=NULL)
     create(h->right,q);
     else
     h->right=q;
   }
   else//(q->value>h-value)
   {
     if(h->left!=NULL)
     create(h->left,q);
     else
     h->left=q;
   }

}
void BST(tree *h)
{
 cout<<"Enter Value";
 struct tree *q;
 q=(tree*)malloc(sizeof(tree));
 cin>>q->value;
 q->right=NULL;
 q->left=NULL;
 create(h,q);
 cout<<"Continue Y/N";
 if(getch()=='y')
 BST(h);
 else
 return;
}
void print_inorder(struct tree *p)
{
     if(p==NULL)return;
     print_inorder(p->left);
     cout<<endl<<p->value;
     print_inorder(p->right);
}
void main()
{
     clrscr();
     struct tree *a;
     a=(tree*)malloc(sizeof(tree));
     cout<<"Enter Tree\n";
     BST(a);
//     cout<<"\n Preorder\n";
     //print_preorder(a);
     cout<<"\n Inorder\n";
     print_inorder(a);
//     cout<<"\n Postorder\n";
  ///   print_postorder(a);
     getch();
}

C Program for Binary Search

/*Binary Search*/
#include<iostream.h>
#include<conio.h>
int x[20],s;
int search(int lb,int ub)
{
  int mid;
  if(ub<lb)return 0;
  mid=(lb+ub)/2;
  if(s==x[mid])return mid;
  else if(s<x[mid])search(lb,mid-1);
  else search(mid+1,ub);
}
void main()
{
 int i,j;
 cout<<"Enter Size";
 cin>>j;
 cout<<"Enter element";
 for(i=1;i<=j;i++)
 cin>>x[i];
 cout<<"Enter Element";
 cin>>s;
 cout<<"Position"<<search(1,j);
 getch();
}