-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.txt
More file actions
136 lines (134 loc) · 2.56 KB
/
Copy pathbinary.txt
File metadata and controls
136 lines (134 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//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();
}