#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

 

typedef struct b_tree

{

 int val;

 struct b_tree *left,*right;

}*BT;

//---------------------------------------------------------------------------

typedef struct que

{

 BT val;

 struct que *next;

}*Q;

//---------------------------------------------------------------------------

Q new_node1()

{

 Q new;

 new=malloc(sizeof(struct que));

 new->next=NULL;

 return(new);

}

//---------------------------------------------------------------------------

void add_q(Q head,BT t)

{

 Q temp,new;

 new=new_node1();

 new->val=t;

 temp=head;

 while(temp->next!=NULL)

   temp=temp->next;

 temp->next=new;

}

//---------------------------------------------------------------------------

BT del_q(Q head)

{

 Q temp;

 temp=head->next;

 if(temp!=NULL)

  head->next=temp->next;

 return(temp->val);

}

//---------------------------------------------------------------------------

int q_empty(Q head)

{

  return(head->next==NULL);

}

//---------------------------------------------------------------------------

BT new_node()

{

 BT new;

 new=malloc(sizeof(struct b_tree));

 new->left=new->right=NULL;

 return(new);

}

//---------------------------------------------------------------------------

BT get_node()

{

 BT new;

 new=new_node();

 printf("\n\n\t ENTER THE VER :: ");

 scanf("%d",&new->val);

 return(new);

}

//---------------------------------------------------------------------------

BT create()

{

 BT head,new,temp;

 int attach;

 char c;

 head=new_node();

 new=get_node();

 head->left=new;

 printf("\n\n\t WANT TO ADD ANYMORE NODE (Y/N) :: ");

 c=getche();

 while(c=='Y'||c=='y')

  {

   temp=head->left;

   new=get_node();

   attach=0;

   while(attach==0)

   {

   if(new->val<temp->val)

      if(temp->left!=NULL)

       temp=temp->left;

      else

       {

        temp->left=new;

        attach=1;

       }

   else

       if(temp->right!=NULL)

        temp=temp->right;

       else

        {

         temp->right=new;

         attach=1;

        }

      }

      printf("\n\n\t ANYMORE NODE (Y/N) :: ");

      c=getche();

  }

 return(head);

}

//---------------------------------------------------------------------------

void display(BT head)

{

 BT temp;

 Q h;

 int i=0;

 h=new_node1();

 temp=head->left;

 add_q(h,temp);

 add_q(h,head);

 temp=del_q(h);

 printf("\n\n\t LEVEL %d :: ",i);

 i++;

 while(!q_empty(h))

 {

  if(temp!=head)

  {

  printf("\t%d",temp->val);

  if(temp->left!=NULL)

   add_q(h,temp->left);

  if(temp->right!=NULL)

   add_q(h,temp->right);

  }

  else

   {

      printf("\n\n\t LEVEL %d :: ",i);

      add_q(h,head);

      i=i+1;

   }

  temp=del_q(h);

 }

}

//---------------------------------------------------------------------------

int search_val(BT head,int val)

{

 BT temp;

 Q h;

 temp=head->left;

 h=new_node1();

 add_q(h,temp);

 while(!q_empty(h))

 {

  temp=del_q(h);

  if(temp->val==val)

      return(1);

  else

   {

      if(temp->left!=NULL)

       add_q(h,temp->left);

      if(temp->right!=NULL)

       add_q(h,temp->right);

   }

 }

 return(0);

}

//---------------------------------------------------------------------------

 

void main()

{

 BT head;

 int choice,val;

 textcolor(10);

 while(1)

 {

  clrscr();

  printf("\n\n\t ******  BINARY SEARCH TREE *****");

  printf("\n\n\t 1> CREATE");

  printf("\n\n\t 2> DISPLAY");

  printf("\n\n\t 3> SEARCH VAL");

  printf("\n\n\t 4> EXIT");

  printf("\n\n\t ENTER YOUR CHOICE :: ");

  scanf("%d",&choice);

 

  switch(choice)

  {

   case 1:head=create();

              printf("\n\n\t THE BINARY SEARCH TREE IS :: ");

              display(head);

              break;

   case 2:printf("\n\n\t THE BINARY SEARCH TREE IS :: ");

              display(head);

              break;

   case 3:printf("\n\n\t ENTER THE VAL TO BE SERACHED :: ");

              scanf("%d",&val);

              val=search_val(head,val);

              if(val==1)

               {

                  printf("\n\n\t THE VAL IS PRESENT IN THE TREE");

                  display(head);

               }

              else

               {

                  printf("\n\n\t THE VAL IS NOT PRESENT IN THE TREE");

                  display(head);

               }

              break;

   case 4:printf("\n\n\t PRESS ESC KEY TO EXIT");

              if(getch()==27)

               exit(0);

              break;

 

  }

  getch();

 }

}

//---------------------------------------------------------------------------



hit counter script