#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 temp;

 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 LEFT OR RIGHT 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;

        }

   else

      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 q_create()

{

 Q h;

 h=malloc(sizeof(struct queue));

 h->next=NULL;

 return(h);

}

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

void add_q(Q h,BT t)

{

 Q temp,new;

 new=malloc(sizeof(struct queue));

 new=t;

 if(h->next==NULL)

   h->next=new;

 temp=h->next;

 while(temp!=NULL)

   temp=temp->next;

 temp->next=new;

}

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

Q delete_q(Q h)

{

 Q temp;

 temp=h->next;

 h->next=temp->next;

 return(temp);

}

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

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->temp);

   t=t->next;

  }

}

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

void bfs_tree(BT head)

{

 BT temp;

 Q h;

 h=q_create();

 temp=head->left;

 add_q(h,temp);

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

 while(!q_empty(h))

  {

   temp=delete_q(h);

   print_queue(h);

   if(temp->left!=NULL)

       {

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

        add_q(h,temp->left);

       }

   if(temp->left==NULL)

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

 

   if(temp->right!=NULL)

       {

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

        add_q(h,temp->right);

       }

   if(temp->right==NULL)

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

 

  }

}

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

void main()

{

 BT head;

 textcolor(14);

 clrscr();

 head=create();

 bfs_tree(head);

 getch();

}

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

 



hit counter script