Earn without any Investment!

Monday, October 25, 2010

In-Order Traversal of Binary Tree

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    node *left;
    node *right;
}*tree;
  //*tree=NULL;
node *insert(node *tree,int ele);
void inorder(node *tree);
int count=1;
void main()
{
   clrscr();
   char ch;
   int ele;
   do
   {
     clrscr();
     cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
     cout<<"\n\t2----IN-ORDER TRAVERSAL.";
     cout<<"\n\t3----EXIT.";
     cout<<"\n\tENTER CHOICE::";
     ch=getch();
     switch(ch)
     {
       case '1':
     cout<<"\n\tENTER THE ELEMENT::";
     cin>>ele;
     tree=insert(tree,ele);
     break;
       case '2':
     cout<<"\n\tIN-ORDER TRAVERSAL OF A TREE\n\t";
     inorder(tree);
     getch();
     break;
       case '3':
     exit(0);
      }
    }while(ch!='3');
 }
node *insert(node *tree,int ele)
{
   if(tree==NULL)
   {
     tree=new node;
     tree->left=tree->right=NULL;
     tree->data=ele;
     count++;
    }
    else if(count%2==0)
      tree->left=insert(tree->left,ele);
    else
      tree->right=insert(tree->right,ele);
    return(tree);
}
void inorder(node *tree)
{
  if(tree!=NULL)
  {
    inorder(tree->left);
    cout<<tree->data<<"\t";
    inorder(tree->right);
   }
}

Binary tree Traversal

      #include<iostream.h>
      #include<conio.h>
      #include<stdio.h>
      #include<stdlib.h>
      struct node
      {
           int data;
           node *left;
           node *right;
      }*tree;

      //*tree=NULL;
      node *insert(node *tree,int ele);

      void preorder(node *tree);
      void inorder(node *tree);
      void postorder(node *tree);
      int count=1;

      void main()
      {
           clrscr();
           char ch;
           int ele;
           do
           {
            clrscr();
            cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
            cout<<"\n\t2----PRE-ORDER TRAVERSAL.";
            cout<<"\n\t3----IN-ORDER TRAVERSAL.";
            cout<<"\n\t4----POST-ORDER TRAVERSAL.";
            cout<<"\n\t5----EXIT.";
            cout<<"\n\tENTER CHOICE::";
            ch=getch();
            switch(ch)
            {
             case '1':

             cout<<"\n\tENTER THE ELEMENT::";
             cin>>ele;
             tree=insert(tree,ele);
             break;

             case '2':
             cout<<"\n\t****PRE-ORDER TRAVERSAL OF A TREE****";
             preorder(tree);
             getch();
             break;

             case '3':
             cout<<"\n\t****IN-ORDER TRAVERSAL OF A TREE****";
             inorder(tree);
             getch();
             break;

             case '4':
             cout<<"\n\t****POST-ORDER TRAVERSAL OF A TREE****";
             postorder(tree);
             getch();
             break;

             case '5':
             exit(0);
            }
           }while(ch!='5');
      }

      node *insert(node *tree,int ele)
      {
           if(tree==NULL)
           {
            tree=new node;
            tree->left=tree->right=NULL;
            tree->data=ele;
            count++;
           }
           else
           if(count%2==0)
           tree->left=insert(tree->left,ele);
           else
           tree->right=insert(tree->right,ele);
           return(tree);
      }

      void
       preorder(node *tree)
      {
           if(tree!=NULL)
           {
            cout<<tree->data<<"\t";
            preorder(tree->left);
            preorder(tree->right);
                   }
      }

      void inorder(node *tree)
      {
           if(tree!=NULL)
           {
            inorder(tree->left);
            cout<<tree->data<<"\t";
            inorder(tree->right);
            }
      }

      void postorder(node *tree)
      {
           if(tree!=NULL)
           {
            postorder(tree->left);
            postorder(tree->right);
            cout<<tree->data<<"\t";
            }
      }

Hashing Using C++

#include<iostream.h>
#include<conio.h>
#include<string.h>
             class hash
            {
             struct node
                {
                char key[50];
                long add;
                node *next;
                }*head;
              public:
                 hash()
                   {
                   head=NULL;
                }
            void folding(char a[])
                {
                long i=0,j=0,k=0,l=0,s=0;
                 i=strlen(a);
                 j=i/2;
                 while(k<j)
                   {
                   l=l*10+a[k];
                   k++;
                   }
                 while(j<i)
                   {
                   s=s*10+a[j];
                   j++;
                   }
                   l=s+l;

                   chain(a,l);
                 }
                  void chain(char a[],long l)
                  {
                   node *temp,*r;
                   temp=head;
                   if(temp==NULL)
                     {
                     temp=new node;
                     strcpy(temp->key,a);
                     temp->add=l;
                     temp->next=NULL;
                     head=temp;
                     }
                   else
                     {
                     while(temp->next!=NULL)
                    temp=temp->next;
                    r=new node;
                       strcpy( r->key,a);
                       r->add=l;
                       r->next=NULL;
                       temp->next=r;
                       }
                    }
                 void show()
                    {
                    node *temp;
                    temp=head;
                    if(head==NULL)
                      cout<<"\n Table is empty";
                      else
                    {
                     cout<<" key  " <<"        address  ";
                     cout<<"\n ----........--------";
                    while(temp!=NULL)
                       {
                       cout<<"\n";
                       cout<<temp->key<<"        "<<temp->add;
                       temp=temp->next;
                       }
                    }
                    }

              void search(char a[])
                   {
                   long i=0,j=0,k=0,l=0,s=0;
                  node *temp;
                  temp=head;
                  i=strlen(a);
                  j=i/2;
                  while(k<j)
                   {
                   l=l*10+a[k];
                   k++;
                   }
                 while(j<i)
                   {
                   s=s*10+a[j];
                   j++;
                   }
                   l=s+l;
                  while(temp!=NULL)
                    {
                    if(temp->add==l)
                      {
                      cout<<"\n"<<a<<" is found";
                      s=-10;
                      break;
                      }
                    else
                       {
                    temp=temp->next;
                    }
                    }
                  if(s!=-10)
                      {
                      cout<<"\n"<<a<<" string not found ";
                      }
                  }
                  };
void main()
{
              hash k;
              clrscr();
              char c='y',ch[50];
              while(1)
               {
               cout<<"\nEnter string to save in hash table ";
               cin>>ch;
               k.folding(ch);
               cout<<"\nFor again enter y for exit press any ";
               cin>>c;
               if(c!='y')
                 break;
               }
               k.show();
              cout<<"\n\nFor searching Enter string ";
              cin>>ch;
              k.search(ch);
     getch();
}