-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Search Tree.c
74 lines (64 loc) · 1.72 KB
/
Binary Search Tree.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
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
Node* insert(Node* root, int data) {
if (root == NULL) {
root = createNode(data);
} else if (data <= root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
int search(Node* root, int data) {
if (root == NULL) return 0;
else if (root->data == data) return 1;
else if (data <= root->data) return search(root->left, data);
else return search(root->right, data);
}
void inorderTraversal(Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
Node* root = NULL;
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 15);
root = insert(root, 3);
root = insert(root, 7);
printf("Inorder traversal: ");
inorderTraversal(root);
printf("\n");
int searchKey = 7;
if (search(root, searchKey)) {
printf("%d is found in the tree.\n", searchKey);
} else {
printf("%d is not found in the tree.\n", searchKey);
}
searchKey = 12;
if (search(root, searchKey)) {
printf("%d is found in the tree.\n", searchKey);
} else {
printf("%d is not found in the tree.\n", searchKey);
}
return 0;
}