-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree_using_linked_lists.c
260 lines (213 loc) · 6.33 KB
/
binary_tree_using_linked_lists.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left, *right;
};
void displaypreorder(struct node *ptr)
{
if(ptr != NULL)
{
printf("%d\t", ptr -> data);
displaypreorder(ptr -> left);
displaypreorder(ptr -> right);
}
}
struct node * buildtree(struct node *ptr, int data)
{
int ch, leftdata, rightdata;
struct node *leftptr = NULL, *rightptr = NULL;
if(ptr != NULL)
ptr -> data = data;
printf("Does %d have left child? - 1. Yes, 2. No: ", ptr -> data);
scanf("%d", &ch);
if(ch == 1)
{
leftptr = (struct node *)malloc(sizeof(struct node));
ptr -> left = leftptr;
printf("Enter data of left child: ");
scanf("%d", &leftdata);
buildtree(leftptr, leftdata);
}
else
ptr -> left = NULL;
printf("Does %d have right child? - 1. Yes, 2. No: ", ptr -> data);
scanf("%d", &ch);
if(ch == 1)
{
rightptr = (struct node *)malloc(sizeof(struct node));
ptr -> right = rightptr;
printf("Enter data of right child: ");
scanf("%d", &rightdata);
buildtree(rightptr, rightdata);
}
else
ptr -> right = NULL;
return ptr;
}
struct node * searchnode(struct node *ptr, int key)
{
if(ptr == NULL || ptr -> data == key)
return ptr;
else
{
if(searchnode(ptr -> left, key) == NULL)
searchnode(ptr -> right, key);
}
}
struct node * insert(struct node *root, int key)
{
int ch, data;
struct node *ptr;
ptr = searchnode(root, key);
if(ptr == NULL)
{
printf("Parent node not found.");
return root;
}
if(ptr -> left == NULL || ptr -> right == NULL)
{
printf("Insert as - 1. Left Child, 2. Right Child: ");
scanf("%d", &ch);
if(ch == 1)
{
if(ptr -> left == NULL)
{
printf("Enter data of new node: ");
scanf("%d", &data);
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode -> data = data;
newnode -> left = newnode -> right = NULL;
ptr -> left = newnode;
}
else
printf("Left child is not empty.");
}
else
{
if(ptr -> right == NULL)
{
printf("Enter data of new node: ");
scanf("%d", &data);
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode -> data = data;
newnode -> left = newnode -> right = NULL;
ptr -> right = newnode;
}
else
printf("Right child is not empty.");
}
}
else
printf("Left and right children are not empty.");
return root;
}
struct node * searchparent(struct node *ptr, int data)
{
if(ptr == NULL)
return ptr;
else if(ptr -> left != NULL && ptr -> right == NULL)
{
if(ptr -> left -> data == data)
return ptr;
else
searchparent(ptr -> left, data);
}
else if(ptr -> left == NULL && ptr -> right != NULL)
{
if(ptr -> right -> data == data)
return ptr;
else
searchparent(ptr -> right, data);
}
else if(ptr -> left != NULL && ptr -> right != NULL)
{
if(ptr -> right -> data == data || ptr -> left -> data == data)
return ptr;
else
{
if(searchparent(ptr -> left, data) == NULL)
searchparent(ptr -> right, data);
}
}
}
struct node * delete(struct node *root, int data)
{
struct node *ptr = NULL, *parent = NULL;
if(root == NULL)
{
printf("\nTree is empty.");
return root;
}
else if(root -> data == data && root -> left == NULL && root -> right == NULL)
{
free(root);
root = NULL;
}
else
{
ptr = searchnode(root, data);
if(ptr == NULL)
printf("Node not found.");
else
{
if(ptr -> left == NULL && ptr -> right == NULL)
{
parent = searchparent(root, data);
if(parent -> left == ptr)
parent -> left = NULL;
else
parent -> right = NULL;
free(ptr);
}
else
printf("\nNode is not a leaf node.");
}
}
return root;
}
void main()
{
int ch, data;
printf("Enter data of root node: ");
scanf("%d", &data);
struct node *root = (struct node *)malloc(sizeof(struct node));
root = buildtree(root, data);
printf("\nBinary Tree:\t");
displaypreorder(root);
do
{
printf("\n\tMENU");
printf("\n1. Insert\t2. Delete\n3. Search\t4. Exit");
printf("\nEnter choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter parent node of new node: ");
scanf("%d", &data);
root = insert(root, data);
printf("\nBinary Tree:\t");
displaypreorder(root);
break;
case 2: printf("\nEnter node to delete: ");
scanf("%d", &data);
root = delete(root, data);
if(root != NULL)
{
printf("\nBinary Tree:\t");
displaypreorder(root);
}
else
printf("\nTree is empty.");
break;
case 3: printf("\nEnter node to search: ");
scanf("%d", & data);
if(searchnode(root, data) != NULL)
printf("\nNode found.");
else
printf("\nNode not found.");
break;
}
}while(ch >= 1 && ch <= 3);
}