-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Search_Tree.c
81 lines (72 loc) · 1.93 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
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
// Structure for a tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Creating a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Inserting a node in the BST
struct Node* insert(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
// In-order traversal of the BST
void inOrderTraversal(struct Node* root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}
void preOrderTraversal(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
inOrderTraversal(root->left);
inOrderTraversal(root->right);
}
}
void postOrderTraversal(struct Node* root) {
if (root != NULL) {
inOrderTraversal(root->left);
inOrderTraversal(root->right);
printf("%d ", root->data);
}
}
// Main function
int main() {
struct Node* root = NULL;
int n, data;
printf("Enter the number of nodes to insert: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter value to insert: ");
scanf("%d", &data);
root = insert(root, data);
}
printf("\nIn-order traversal of the BST: ");
inOrderTraversal(root);
printf("\n");
printf("\nPre-order traversal of the BST: ");
preOrderTraversal(root);
printf("\n");
printf("\nPost-order traversal of the BST: ");
postOrderTraversal(root);
printf("\n");
return 0;
}