Pages

100 Contemporary Living Room Designs

Showing posts with label ALGORITHM. Show all posts
Showing posts with label ALGORITHM. Show all posts

Tuesday, September 18, 2012

C++ PROGRAM TO IMPLEMENT BINARY SEARCH TREE.


Write a C++ program to create a class called BIN_TREE ( Binary tree) with member functions to perform inorder, preorder and postorder traversals. Create a BIN_TREE object and demonstrate the traversals.


#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
struct node
{
  int info;
  struct node *left;
  struct node *right;
};
typedef struct node tree ;
tree *root=NULL;
class BIN
{
  int num;
  tree *p,*prev,*temp;
  public:
  void insert();
  void inorder(tree *);
  void postorder(tree *);
  void preorder(tree *);
  void display();
};
void BIN:: insert()
{
  p=new(tree);
  cout<<"\n Enter  number:";
  cin>>num;
  p->info=num;
  p->left=p->right=NULL;
  if(root==NULL)
  {
    root=p;
    return;
  }
  temp=root;
  while(temp!=NULL)
  {
    if(num>=temp->info)
    {
      prev=temp;
      temp=temp->right;
    }
    else
    {
      prev=temp;
      temp=temp->left;
    }
  }
  if(num>=prev->info)
    prev->right=p;
  else
    prev->left=p;
}
void BIN::preorder(tree *temp)
{
  if(temp!=NULL)
  {
    cout<<" "<<temp->info;
    preorder(temp->left);
    preorder(temp->right);
  }
}
void BIN:: inorder(tree *temp)
{
  if(temp!=NULL)
  {
    inorder(temp->left);
    cout<<" "<<temp->info;
    inorder(temp->right);
  }
}
void  BIN::postorder(tree *temp)
{
  if(temp!=NULL)
  {
    postorder(temp->left);
    postorder(temp->right);
    cout<<" "<<temp->info;
  }
}
void BIN:: display()
{
  if(root==NULL)
  {
    cout<<"\n ***EMPTY TREE**** \n";
    return;
  }
  cout<<"\n\n THE PREORDER DISPLAY IS:   ";
  preorder(root);
  cout<<"\n\n THE INORDER DISPLAY IS:   ";
  inorder(root);
  cout<<"\n\n THE POSTORDER DISPLAY IS:   ";
  postorder(root);
}
void main()
{
  BIN o;
  int ch=1;
  int count=0;
  clrscr();
  while(ch)
  {
    cout<<"\n***********MENU***********";
    cout<<"\n1:INSERT-IN-TREE\n2:DISPLAY\n3.QUIT\n";
    cout<<"\nEnter your choice:\n";
    cin>>ch;
    switch(ch)
    {
      case 1:clrscr();
         count++;
         o.insert();
         break;
      case 2:clrscr();
      cout<<"\n\n THE NUMBER OF NODES IN THE BST is "<< count;
      o.display();
      break;
      case 3:exit(0);
    }
  }
  getch();
}


You might also like:


OUTPUT




Sunday, September 16, 2012

C++ PROGRAM TO IMPLEMENT QUICK SORT ALGORITHM.


Write a C++ program to create a template function for Quick sort and demonstrate sorting of integers and doubles.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
template<class t>
void quick(t a[],int low,int high)
 {
   t key;
   int i,j,flag=1;
   if(low<high)
   {
     key=a[low];
     i=low+1;
     j=high;
     while(flag)
     {
       while((a[i]<=key) && (i<j))
     i++;
       while(a[j]>key)
     j--;
       if(i<j)
     swap(a,i,j);
       else
     flag=0;
     }
     swap(a,low,j);
     quick(a,low,j-1);
     quick(a,j+1,high);
   }
 }

template<class t1>
void swap(t1 a[],int x,int y)
 {
   t1 temp;
   temp=a[x];
   a[x]=a[y];
   a[y]=temp;
 }
void main()
 {
   int i,n,a[20];
   clrscr();
   cout<<"Enter the number of elements to be sort::";
   cin>>n;
   cout<<"Enter elements:\n";
   for(i=0;i<n;i++)
     cin>>a[i];
   quick(a,0,n-1);
   cout<<"The sorted elements are:\n";
   for(i=0;i<n;i++)
     cout<< a[i]<<endl;;
   getch();
 }


OUTPUT