forked from atyant-yadav/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarytree.c
181 lines (144 loc) · 4.5 KB
/
binarytree.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 20
struct node
{
int data;
struct node *lchild;
struct node *rchild;
};
struct node *start=NULL;
struct node *nod, *temp;
struct node *build_node[SIZE];
int no_nodes;
void create();
void inordertrav(struct node *n);
void preordertrav(struct node *n);
void postordertrav(struct node *n);
void display();
int main()
{
int opt;
do
{
printf("\n 1.Create Binary tree\n");
printf("\n 2.Inorder traversal of the Binary tree\n");
printf("\n 3.Postorder traversal of the Binary tree\n");
printf("\n 3.Preorder traversal of the Binary tree\n");
printf("\n 5.Display Binary tree\n");
printf("\n 6.Quit\n");
printf("\nEnter option: ");
scanf("%d",&opt);
switch(opt)
{
case 1:
create();
break;
case 2:
inordertrav(start);
break;
case 3:
postordertrav(start);
break;
case 4:
preordertrav(start);
break;
case 5:
display();
break;
case 6:
exit(1);
}
} while(1);
}
void create()
{
int i, item[SIZE];
int lchld, rchld;
printf(" Enter the number of nodes in the binary tree inclusive of the null child nodes\n");
printf(" (input the highest numbered node)!\n");
printf("-----------------------------------------------------\n");
scanf("%d", &no_nodes);
printf("\nEnter the elements in a left to right fashion \n");
printf("in the order of their levels in the binary tree! \n");
printf("\nEnter the number 0 to indicate a null child\n");
printf("------------------------------------------------------\n");
for (i=1; i<=no_nodes; i++)
{scanf("%d", &item[i]);
if (item[i] !=0)
{ nod=(struct node *)calloc(1,sizeof(struct node));
nod->data = item[i];
build_node[i] = nod;
}
else build_node[i]= 0;
}
for (i=1; i<=no_nodes; i++)
{
temp = build_node[i];
if (temp !=0)
{
lchld = 2*i;
rchld = 2*i+1;
if (lchld <= no_nodes)
temp->lchild = build_node[lchld];
else temp->lchild = 0;
if (rchld <= no_nodes)
temp->rchild = build_node[rchld];
else temp->rchild = 0;
}
}
start = build_node[1]; /* start points to the root node of the binary tree*/
}
void inordertrav(struct node *n)
{
if (n != 0)
{ inordertrav(n->lchild);
printf(" %d ", n->data);
inordertrav(n->rchild);
}
}
void postordertrav(struct node *n)
{
if (n != 0)
{ postordertrav(n->lchild);
postordertrav(n->rchild);
printf(" %d ", n->data);
}
}
void preordertrav(struct node *n)
{
if (n != 0)
{
printf(" %d ", n->data);
preordertrav(n->lchild);
preordertrav(n->rchild);
}
}
void display()
{ int level,i=0, count =1;
if(start == NULL)
printf("\n Binary tree is empty! \n");
else
{ printf(" display of node number and data of the nodes in the Binary tree\n");
for(i=1; i<=no_nodes; i++)
{temp = build_node[i];
if (temp!=0)
printf(" (%d, %d) ",i, temp->data);
}
printf("\n Hierarchy of nodes in the Binary tree: \n");
level =1;
i=1;
while(i<=no_nodes)
{ temp = build_node[i];
if (temp !=0) printf(" %d ", temp->data);
else printf(" 0 ");
count = count +1;
i=i+1;
if (count>pow(2,level-1)){ printf("\n\n");
count =1;
level =level +1;
}
}
}
}