//THE PROGRAM FOR THE CONVERTION OF GENERAL TREE TO BINARY TREE //PROGRAM EXECUTED BY O.R.SENTHIL KUMARAN #include"stdio.h" #include"malloc.h" #include"conio.h" struct node { char data; struct node *cptr,*link; }; void preorder(struct node *str) { if(str) { printf("\t%c",str->data); preorder(str->cptr); preorder(str->link); } } void inorder(struct node *str) { if(str) { inorder(str->cptr); printf("\t%c",str->data); inorder(str->link); } } void postorder(struct node *str) { if(str) { postorder(str->cptr); postorder(str->link); printf("\t%c",str->data); } } void disp(struct node *str) { clrscr(); printf("\n PREORDER \n"); preorder(str); printf("\n INORDER \n"); inorder(str); printf("\n POSTORDER \n"); postorder(str); printf("\n"); } void convert(struct node *gptr) { struct node *temp; int i,n; do { if(gptr->data=='\n') printf("NO SUBTREE FOR THE ROOT R"); else printf("\nNO. OF SUBTREE FOR THE THE NODE %c\t",gptr->data); scanf("%d",&n); fflush(stdin); }while(n<0); if(n>0) { temp=(struct node *)malloc(sizeof(struct node)); temp->link=NULL; temp->cptr=NULL; temp->data='0'; gptr->cptr=temp; if(gptr->data=='\n') { printf("\nENTER THE DATA FOR THE CHILD OF THE NODE ROOT -R"); printf("\t"); } else printf("\n ENTER THE DATA FOR THE CHILD OF THE NODE %c",gptr->data); printf("\t"); temp->data=getc(stdin); convert(temp); //CONVERSION OF GENERAL TO BINARY TREE } for(i=2;i<=n;i++) { temp->link=(struct node *)malloc(sizeof(struct node)); temp=temp->link; temp->link=NULL; temp->cptr=NULL; temp->data='\0'; if(gptr->data=='\n') { printf("\nENTER THE DATA FOR THE CHILD %d OF THE NODE ROOT R\t",i); printf("\t"); } else printf("\nENTER THE DATA FOR THE CHILD %d OF THE NODE %c\t",i,gptr->data); printf("\t"); temp->data=getc(stdin); convert(temp); } } void main() { struct node *str; int ch; str=(struct node *)malloc(sizeof(struct node)); str->cptr=NULL; str->link=NULL; str->data='R'; do { clrscr(); printf("\nBINARY TREE TO GENERAL TREE CONVERSION\n"); printf("\n\t\tC-CONVERT\n\t\tD-DISPLAY\n\t\tQ-QUIT"); printf("\nENTER YOUR CHOICE\t"); switch(getche()) { case 'C': case 'c': str=(struct node *)malloc(sizeof(struct node)); str->cptr=NULL; str->link=NULL; str->data='R'; convert(str); break; case 'D': case 'd': disp(str); getch(); break; case 'Q': case 'q': exit(); } }while(ch!=3); getch(); }