-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
201 lines (170 loc) · 4.59 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct bstNode{
int data;
struct bstNode *left;
struct bstNode *right;
};
// function creates a new node in the heap
struct bstNode *getNewNode(int data){
struct bstNode *newNode = (struct bstNode*)malloc(sizeof(struct bstNode));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
};
// A recursive function to insert data into the tree
struct bstNode *insert(struct bstNode *root, int data){
if(root == NULL){ // if the tree is empty
root = getNewNode(data);
}else if( data < root->data){ // if the data is less than the data in the current node, insert to the left subtree
root->left = insert(root->left, data);
}else{ // if the data is greater than the data in the current node, insert to the right subtree
root->right = insert(root->right, data);
}
return root;
};
bool search(struct bstNode *root, int data){
if(root == NULL){
return false;
}
else if(root->data == data){
return true;
}
else if(data <= root->data){
return search(root->left, data);
}
else{
return search(root->right, data);
}
}
// iterative solution to find the min value
int findMin(struct bstNode *root){
if (root == NULL){
printf("Error: The tree is empty!\n");
return -1;
}
struct bstNode *current = root;
while(current->left != NULL){
current = current->left;
}
return current->data;
}
// recursive solution to find the max value
int findMax(struct bstNode *root){
if (root == NULL){
printf("Error: The tree is empty!\n");
return -1;
}
else if (root->right == NULL){
return root->data;
}
// search in right sub tree
return findMax(root->right);
}
// A recursive implementation to find the height of the tree
int height(struct bstNode *root){
if(root == NULL){
return -1;
}
return max(height(root->left), height(root->right)) + 1;
}
// function to find the max value of two int
int max(int left, int right){
if (left > right){
return left;
}else{
return right;
}
}
// Pre-order traversal of tree using recursion
void preOrder(struct bstNode *root){
if(root == NULL) return;
printf("%d ", root->data);
preOrder(root->left);
preOrder(root->right);
}
// In-order traversal of tree using recursion
void inOrder(struct bstNode *root){
if(root == NULL) return;
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
// Post-order traversal of tree using recursion
void postOrder(struct bstNode *root){
if(root == NULL) return;
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
// Function deletes a node given its data value as input
struct bstNode *deleteNode(struct bstNode *root, int data){
if (root == NULL) return root;
else if (data < root->data){
root->left = deleteNode(root->left, data);
}
else if (data > root->data){
root->right = deleteNode(root->right, data);
}
else{ // when data == the root->data
// case 1: No child
if (root->left == NULL && root->right == NULL){
free(root);
root = NULL;
return root;
}
// case 2: One child
else if (root->left == NULL){
struct bstNode *temp = root;
root = root->right;
free(temp);
temp = NULL;
return root;
}
// case 3: 2 Children
else{
struct bstNode *temp = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
}
return root;
};
int main()
{
struct BstNode *root = NULL; // Creating an empty tree
printf("The max value: %d\n", findMax(root));
root = insert(root,27);
root = insert(root,14);
root = insert(root,35);
root = insert(root,10);
root = insert(root,19);
root = insert(root,31);
root = insert(root,42 );
int number = 20;
if(search(root,number) == true){
printf("Found!\n");
}
else{
printf("Not found!\n");
}
printf("The min value: %d\n", findMin(root));
printf("The max value: %d\n", findMax(root));
printf("The height of the tree: %d\n", height(root));
printf("\nPre-order: ");
preOrder(root);
printf("\n");
printf("\nIn-order: ");
inOrder(root);
printf("\n");
printf("\nPost-order: ");
postOrder(root);
printf("\n");
deleteNode(root, 31);
printf("\nIn-order: ");
inOrder(root);
printf("\n");
return 0;
}