-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.c
More file actions
219 lines (191 loc) · 3.81 KB
/
binaryTree.c
File metadata and controls
219 lines (191 loc) · 3.81 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
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
#include <stdio.h>
#include <stdlib.h>
//
// Created by Prince on 13-09-2024.
//
struct Node {
struct Node* left;
int data;
struct Node* right;
};
struct Queue {
struct Node* root;
struct Queue* next;
} *front = NULL, * rear = NULL;
void enqueue(struct Node* root) {
struct Queue* q = malloc(sizeof(struct Queue));
q->root = root;
q->next = NULL;
if (rear != NULL) {
rear->next = q;
}
else {
front = q;
}
rear = q;
}
struct Node* dequeue() {
if (front == NULL) {
printf("\nQueue is empty.");
return NULL;
}
struct Node* temp = front->root;
struct Queue* toFree = front;
front = front->next;
if (front == NULL) {
rear = NULL;
}
free(toFree);
return temp;
}
struct Node* createNode(const int data) {
struct Node* node = malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
void insertNode(struct Node** root, const int value) {
int direction;
if (*root == NULL) {
*root = createNode(value);
}
else {
printf("Left(1) or right(0) :");
scanf("%d", &direction);
if (direction) {
insertNode(&(*root)->left, value);
}
else {
insertNode(&(*root)->right, value);
}
}
}
void deleteNode(struct Node** root) {
if (*root == NULL) {
printf("Node is empty.\n");
return;
}
if ((*root)->left == NULL && (*root)->right == NULL) {
struct Node* temp = *root;
*root = NULL;
free(temp);
printf("Leaf node deleted.\n");
}
else {
int direction;
printf("Left(1) or right(0): ");
scanf("%d", &direction);
if (direction) {
deleteNode(&(*root)->left);
}
else {
deleteNode(&(*root)->right);
}
}
}
void inOrder(const struct Node* root) {
if (root != NULL) {
inOrder(root->left);
printf("[%d]", root->data);
inOrder(root->right);
}
}
void preOrder(const struct Node* root) {
if (root != NULL) {
printf("[%d]", root->data);
preOrder(root->left);
preOrder(root->right);
}
}
void postOrder(const struct Node* root) {
if (root != NULL) {
postOrder(root->left);
postOrder(root->right);
printf("[%d]", root->data);
}
}
void levelOrder(struct Node* root) {
if (root == NULL) {
printf("Empty Tree");
}
else {
enqueue(root);
struct Node* node = NULL;
while (front != NULL) {
node = dequeue();
printf("[%d]", node->data);
if (node->left != NULL) {
enqueue(node->left);
}
if (node->right != NULL) {
enqueue(node->right);
}
}
free(node);
}
}
void mainMenu() {
printf("\nOperations:");
printf("\n1.Insert Node");
printf("\n2.Delete Node");
printf("\n3.InOrder");
printf("\n4.PreOrder");
printf("\n5.PostOrder");
printf("\n6.Level Order");
printf("\n7.Exit");
printf("\nEnter your choice: ");
}
int main() {
struct Node* root = NULL;
int option, size;
do {
mainMenu();
scanf("%d", &option);
switch (option) {
case 1: {
printf("\nEnter the number of elements : ");
scanf("%d", &size);
int data[size];
for (int i = 0; i < size; i++) {
printf("Enter the element #%d : ", (i + 1));
scanf("%d", &data[i]);
insertNode(&root, data[i]);
}
break;
}
case 2: {
deleteNode(&root);
break;
}
case 3: {
printf("\nInorder Traversal:");
inOrder(root);
break;
}
case 4: {
printf("\nPreorder Traversal:");
preOrder(root);
break;
}
case 5: {
printf("\nPostorder Traversal:");
postOrder(root);
break;
}
case 6: {
printf("\nLevel order Traversal:");
levelOrder(root);
break;
}
case 7: {
printf("\nExiting...");
break;
}
default: {
printf("Invalid choice");
}
}
} while (option != 7);
return 0;
}