-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree3.c
427 lines (355 loc) · 11.1 KB
/
Tree3.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* Basic Binary tree with basic operations*/
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *right;
struct Node *left;
} Node;
Node *createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory Allocation Failed. Reallocating The memory\n");
Node* newNode = (Node *)realloc(newNode, sizeof(Node));
}
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
Node* insert(Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
}
else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
Node* findMin(Node* root) {
while (root->left != NULL) {
root = root->left;
}
return root;
}
Node* findMax(Node* root) {
while (root->right != NULL) {
root = root->right;
}
return root;
}
Node* delete(Node* root, int data) {
if (root == NULL) {
printf("The %d is Not Founded in the Tree\n", data);
return root;
}
if (data < root->data) {
root->left = delete (root->left, data);
}
else if (data > root->data) {
root->right = delete (root->right, data);
}
else {
// Node with 1 || 0 child nodes
if (root->left == NULL) {
Node* temp = root->right;
printf("The %d is deleted from the tree\n", root->data);
free(root);
return temp;
}
else if (root->right == NULL) {
Node *temp = root->left;
printf("The %d is deleted from the tree\n", root->data);
free(root);
return temp;
}
// Node with 2 child nodes
Node *temp = findMin(root->right);
root->data = temp->data;
root->right = delete (root->right, temp->data);
}
return root;
}
Node* search(Node* root, int key) {
if (root == NULL) {
return root;
}
if (root->data == key) {
return root;
}
if (key < root->data) {
return search(root->left, key);
}
return search(root->right, key);
}
int height(Node* root) {
if (root == NULL) {
return -1;
}
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight >rightHeight ? leftHeight : rightHeight) + 1;
}
int depth(Node* root, Node* node, int level) {
if (root == NULL) {
return -1;
}
if (root == node) {
return level;
}
int leftDepth = depth(root->left, node, level + 1);
if (leftDepth != -1) {
return leftDepth;
}
return depth(root->right, node, level + 1);
}
int level(Node* root, Node* node) {
if (root == node) {
return 0;
}
return depth(root, node, 0);
}
Node* findParent(Node* root, Node* node) {
if (root == NULL || root->left == node || root->right == node) {
return node;
}
Node *foundParent = findParent(root->left, node);
if (foundParent == NULL) {
foundParent = findParent(root->right, node);
}
return foundParent;
}
int diameter(Node* root, int* height) {
int lh = 0,rh = 0;
int ldiameter = 0, rdiameter = 0;
if (root == NULL) {
*height = 0;
return 0;
}
ldiameter = diameter(root->left, &lh);
rdiameter = diameter(root->right, &rh);
*height = (lh > rh ? lh : rh) + 1;
return (lh + rh + 1) > (ldiameter > rdiameter ? ldiameter : rdiameter) ? (lh + rh + 1) : (ldiameter > rdiameter ? ldiameter : rdiameter);
}
void findLeaves(Node* root) {
if (root == NULL) {
return;
}
if (root->left == NULL && root->right == NULL) {
printf("%d -> ", root->data);
}
findLeaves(root->left);
findLeaves(root->right);
}
Node* findSibling(Node* root, Node* node) {
if (root == NULL || root->left == NULL || root->right == NULL) {
return NULL;
}
if (root->left != NULL && root->right != NULL) {
if (root->left == node) {
return root->right;
}
if (root->right == node) {
return root->left;
}
}
Node *foundSibling = findSibling(root->left, node);
foundSibling = findSibling(root->right, node);
if (foundSibling == NULL) {
return NULL;
}
return foundSibling;
}
void findChildren(Node* node) {
if (node == NULL) {
printf("Underflow\n");
return;
}
if (node->left != NULL) {
printf(" %d and ", node->left->data);
}
if (node->right != NULL) {
printf("%d\n", node->right->data);
}
}
void inOrder(Node* root) {
if (root == NULL) {
return;
}
inOrder(root->left);
printf("%d -> ", root->data);
inOrder(root->right);
}
void preOrder(Node* root) {
if (root == NULL) {
return;
}
printf("%d -> ", root->data);
preOrder(root->left);
preOrder(root->right);
}
void postOrder(Node* root) {
if (root == NULL) {
return;
}
postOrder(root->left);
postOrder(root->right);
printf("%d -> ", root->data);
}
void printCurrentLevel(Node* root, int level) {
if (root == NULL) {
return;
}
if (level == 1)
{
printf("%d -> ", root->data);
}
else if (level > 1) {
printCurrentLevel(root->left, level - 1);
printCurrentLevel(root->right, level - 1);
}
}
void levelOrder(Node* root) {
int h = height(root);
for (int i = 1; i <= h; ++i) {
printCurrentLevel(root, i);
}
}
int main() {
Node *root = NULL;
int choice, data, treeHeight = 0;
Node *node;
do {
printf("\n\tMenu\n1. Insert\n2. Delete\n3. Search\n4. Height Of the tree\n5. Depth of Node\n6. Level of Node\n7. Find the Parent of Node\n8. Diameter of Tree\n9. Find all leaf Nodes\n10. Find Siblings of Node\n11. Find Children Of of Node\n12. In - Order Traversal\n13. Pre - Order Traversal\n14. Post - Order Traversal\n15. Level - Order Traversal\n16. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the value to insert: ");
scanf("%d", &data);
root = insert(root, data);
printf("The %d in inserted to the Tree\n", data);
break;
case 2:
printf("Enter the value to delete: ");
scanf("%d", &data);
root = delete(root, data);
break;
case 3:
printf("Enter the value to search: ");
scanf("%d", &data);
node = search(root, data);
if (node != NULL) {
printf("Value %d found in the tree.\n", data);
}
else {
printf("Value %d Not found in the tree.\n", data);
}
break;
case 4:
printf("Height Of the Tree: %d", height(root));
break;
case 5:
printf("Enter node value find depth: ");
scanf("%d", &data);
node = search(root, data);
if (node != NULL) {
printf("Depth of the node %d: %d\n", data, depth(root, node, 0));
}
else {
printf("Node %d not found in the tree.\n", data);
}
break;
case 6:
printf("Enter The node to find level; ");
scanf("%d", &data);
node = search(root, data);
if (node != NULL) {
printf("Level of the node %d: %d\n", data, level(root, node));
}
else {
printf("Node %d not found in the tree.\n", data);
}
break;
case 7:
printf("Enter node value to find parent: ");
scanf("%d", &data);
node = search(root, data);
if (node != NULL) {
Node* parent = findParent(root, node);
if (parent != NULL) {
printf("Parent of node %d: %d\n", data, parent->data);
}
else {
printf("Node %d is the root node and has no parent.\n", data);
}
}
break;
case 8:
printf("Diameter of tree: %d\n", diameter(root, &treeHeight));
break;
case 9:
printf("Leaf node: ");
findLeaves(root);
printf("NULL\n");
break;
case 10:
printf("Enter Node value to find sibling: ");
scanf("%d", &data);
node = search(root , data);
if (node != NULL) {
Node *sibling = findSibling(root, node);
if (sibling != NULL) {
printf("Sibling of node %d: %d\n", data, sibling->data);
}
else {
printf("Node %d has no sibling.\n", data);
}
}
else {
printf("Node %d not found in the tree.\n", data);
}
break;
case 11:
printf("Enter node value to find children: ");
scanf("%d", &data);
node = search(root, data);
if (node != NULL) {
findChildren(node);
}
else {
printf("Node %d not found in the tree.\n", data);
}
break;
case 12:
printf("In - order Traversal: ");
inOrder(root);
printf("NULL\n");
break;
case 13:
printf("Pre - Order Traversal: ");
preOrder(root);
printf("NULL\n");
break;
case 14:
printf("Post - Order Traversal: ");
postOrder(root);
printf("NULL\n");
break;
case 15:
printf("Level - Order Traversal: ");
levelOrder(root);
printf("NULL\n");
break;
case 16:
printf("Exiting The Program. Good Bye\n");
exit(0);
default:
printf("Invalid option. please choose a correct one!\n");
break;
}
} while (1);
return 0;
}