#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

 

typedef struct b_tree

{

 int node;

 struct b_tree *left,*right;

}*BT;

typedef struct queue

{

 BT data;

 struct queue *next;

}*Q;

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

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 NODE :: ");

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

 return(new);

}

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

BT create()

{

 BT head,new,temp;

 int attach;

 char c;

 head=new_node();

 new=get_node();

 head->left=new;

 do

 {

  attach=0;

  temp=head->left;

  new=get_node();

  while(attach==0)

  {

   printf("\n\n\t THE CURRENT NODE IS :: %d",temp->node);

   printf("\n\n\t ATTACH %d TO RIGHT OR LEFT OF %d (L/R) :: ",new->node,temp->node);

   c=getche();

      if(c=='l'||c=='L')

        if(temp->left!=NULL)

            temp=temp->left;

        else

         {

            temp->left=new;

            attach=1;

         }

      if(c=='r'||c=='R')

        if(temp->right!=NULL)

            temp=temp->right;

        else

         {

            temp->right=new;

            attach=1;

         }

   }

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

  c=getche();

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

 

 return(head);

}

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

Q n_node()

{

 Q new=malloc(sizeof(struct queue));

 new->next=NULL;

 return(new);

}

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

void add_queue(Q h,BT temp)

{

 Q t,new;

 new=n_node();

 new=temp;

 if(h->next==NULL)

   h->next=new;

 t=h->next;

 while(t!=NULL)

   t=t->next;

 t->next=new;

}

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

Q delete_q(Q h)

{

 Q t;

 t=h->next;

 h->next=t->next;

 return(t);

}

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

Q q_empty(Q h)

{

 return(h->next==NULL);

}

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

void print_queue(Q h)

{

 Q t;

 t=h->next;

 printf("\n\n\t ");

 while(t!=NULL)

  {

   printf("%d ",t->data);

   t=t->next;

  }

}

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

void print_tree(BT head)

{

 BT temp;

 Q h;

 temp=head->left;

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

 add_queue(h,temp);

 while(!q_empty(h))

 {

  temp=delete_q(h);

  if(temp->left!=NULL)

       {

        printf("\n\n\t %d",temp->left->node);

        add_queue(h,temp->left);

       }

  if(temp->right!=NULL)

       {

        printf("\n\n\t %d",temp->right->node);

        add_queue(h,temp->right);

       }

  }

}

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

void main()

{

 BT head;

 int choice;

 textcolor(14);

 while(1)

  {

      clrscr();

      printf("\n\n\t ******* MENU *********");

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

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

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

      printf("\n\n\t 4> COUNT LEAF NODES");

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

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

      scanf("%d",&choice);

 

      switch(choice)

      {

       case 1:head=create();

                  print_tree(head);

                  break;

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

                  if(getch()==27)

                    exit(0);

                  break;

      }

      getch();

  }

}

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

 

 



hit counter script