-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree.c
More file actions
153 lines (153 loc) · 2.71 KB
/
tree.c
File metadata and controls
153 lines (153 loc) · 2.71 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Designed by neel patel..
this program is written in dev-cpp & compiled by tdm-gcc 4.8.1 compiler
getche() used to eliminate garbage input.it can be replaced by scanf().
*/
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct neel
{
char x;
struct neel *l,*r;
}*root,*p1;
int f1;
void insert(char,char);
struct neel * travers(char);
struct neel * inorder(struct neel *,char);
void preorder(struct neel *p);
void postorder(struct neel *p);
void main()
{
char c,d;
printf("enter root node..");
scanr:c=getche();
if(!(isalpha(c)||('0'<=c&&c<='9')))
{
printf("\nonly alphabats & number is allowed!!!\n");
goto scanr;
}
root=(struct neel *)malloc(sizeof(struct neel));
root->x=c;
root->l=NULL;
root->r=NULL;
for(;1;)
{
printf("\nmenu\n1.insert\n2.inorder travers\n3.preorder travers\n4.postorder travers\n0.exit");
printf("\nenter your choice..\n");
switch(getche())
{
case '1':printf("\nenter parent node & new node..");
c=getche();
printf(" ");
d=getche();
insert(c,d);
break;
case '2':travers('#');break;
case '3':printf("\nPREORDER SEQUENCE :- ");
preorder(root);
break;
case '4':printf("\nPOSTORDER SEQUENCE :- ");
postorder(root);
break;
case '0':return;
break;
default:
printf("wrong choice...");
}
}
getch();
}
void insert(char p,char c)
{
char s;
if(!(isalpha(c)||('0'<=c&&c<='9')))
{
printf("\nonly alphabats & number is allowed!!!\n");
return;
}
struct neel *f;
f=travers(p);
if(f==NULL)
{
printf("\n%c not found!!",p);
return;
}
neel:printf("\nwhere you want to insert\n1.left\n2.right\n");
s=getche();
if(s=='1')
{
if(f->l!=NULL)
{
printf("\nleft link of %c is not available..\n",p);
return;
}
else
f->l=(struct neel *)malloc(sizeof(struct neel));
f->l->x=c;
f->l->l=NULL;
f->l->r=NULL;
}
else if(s=='2')
{
if(f->r!=NULL)
{
printf("\nright link of %c is not available..\n",p);
return;
}
else
f->r=(struct neel *)malloc(sizeof(struct neel));
f->r->x=c;
f->r->l=NULL;
f->r->r=NULL;
}
else
goto neel;
printf("\n%c is inserted..\n",c);
}
struct neel * travers(char a)
{
p1=NULL;
if(a!='#')
{
f1=0;
return inorder(root,a);
}
f1=1;
printf("\nINORDER SEQUENCE :- ");
inorder(root,a);
return NULL;
}
struct neel * inorder(struct neel *p,char c)
{
if(p1!=NULL)
return p1;
if(p->l!=NULL)
inorder(p->l,c);
if(f1)
printf("%c ",p->x);
if(p->x==c)
{
p1=p;
return p1;
}
if(p->r!=NULL)
inorder(p->r,c);
return p1;
}
void preorder(struct neel *p)
{
printf("%c ",p->x);
if(p->l!=NULL)
preorder(p->l);
if(p->r!=NULL)
preorder(p->r);
}
void postorder(struct neel *p)
{
if(p->l!=NULL)
postorder(p->l);
if(p->r!=NULL)
postorder(p->r);
printf("%c ",p->x);
}