-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree.c
More file actions
163 lines (148 loc) · 3.32 KB
/
binarytree.c
File metadata and controls
163 lines (148 loc) · 3.32 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
#include <stdio.h>
#include <stdlib.h>
struct node {
struct node *left;
struct node *right;
int data;
};
struct node *root;
struct node* newNode(int value) {
struct node *newnode = malloc(sizeof(struct node));
newnode->data = value;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
struct node* insert(struct node* root, int value) {
if (root == NULL) {
return newNode(value);
} else if (value == root->data) {
printf("Same data can't be stored\n");
} else if (value > root->data) {
root->right = insert(root->right, value);
} else if (value < root->data) {
root->left = insert(root->left, value);
}
return root;
}
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->data);
inorderTraversal(root->right);
}
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->data);
}
struct node* search(struct node* root, int key) {
if (root == NULL) {
printf("\nNot FOUND!\n");
return NULL;
} else if (root->data == key) {
printf("\nFOUND! Node data: %d\n", root->data);
return root;
} else if (root->data < key) {
return search(root->right, key);
} else {
return search(root->left, key);
}
}
struct node* minValueNode(struct node* node) {
struct node* current = node;
while (current && current->left != NULL) {
current = current->left;
}
return current;
}
struct node* deleteNode(struct node* root, int key) {
if (root == NULL) {
return root;
}
if (key < root->data) {
root->left = deleteNode(root->left, key);
} else if (key > root->data) {
root->right = deleteNode(root->right, key);
} else {
if (root->left == NULL) {
struct node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
int main() {
int opt;
int value, searchv, key;
do {
printf("\n1) Create Root Node \n2) Insert Node\n3) Search\n");
printf("4) Inorder Traversal \n5) Preorder Traversal \n6) Postorder Traversal \n7) Delete \n8) Quit \n");
printf("Choose Option :: ");
scanf("%d", &opt);
switch (opt) {
case 1:
printf("\nEnter a number: ");
scanf("%d", &value);
root = newNode(value);
break;
case 2:
printf("\nEnter a number: ");
scanf("%d", &value);
root = insert(root, value);
printf("Inorder Traversal after insertion: ");
inorderTraversal(root);
printf("\n");
break;
case 3:
printf("\nEnter a number: ");
scanf("%d", &searchv);
struct node* foundNode = search(root, searchv);
if (!foundNode) {
printf("\nNode with value %d not found.\n", searchv);
}
break;
case 4:
printf("\nInorder Traversal: ");
inorderTraversal(root);
printf("\n");
break;
case 5:
printf("\nPreorder Traversal: ");
preorderTraversal(root);
printf("\n");
break;
case 6:
printf("\nPostorder Traversal: ");
postorderTraversal(root);
printf("\n");
break;
case 7:
printf("\nEnter a number to be deleted: ");
scanf("%d", &key);
root = deleteNode(root, key);
break;
case 8:
printf("Exiting program...\n");
break;
default:
printf("Invalid option!\n");
}
} while (opt != 8);
return 0;
}