Skip to content

Commit eb6dbcb

Browse files
Merge pull request #178 from Pushkrajpnaik/main
Binary Search Tree creation and it's traversal
2 parents aee1eb3 + a196381 commit eb6dbcb

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

C/bstandtraversal

32.9 KB
Binary file not shown.

C/bstandtraversal.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node {
5+
int data;
6+
struct Node* left;
7+
struct Node* right;
8+
};
9+
10+
struct Node* createNode(int data) {
11+
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
12+
newNode->data = data;
13+
newNode->left = NULL;
14+
newNode->right = NULL;
15+
return newNode;
16+
}
17+
18+
void inOrderTraversal(struct Node* root) {
19+
if (root != NULL) {
20+
inOrderTraversal(root->left);
21+
printf("%d ", root->data);
22+
inOrderTraversal(root->right);
23+
}
24+
}
25+
26+
void preOrderTraversal(struct Node* root) {
27+
if (root != NULL) {
28+
printf("%d ", root->data);
29+
preOrderTraversal(root->left);
30+
preOrderTraversal(root->right);
31+
}
32+
}
33+
34+
void postOrderTraversal(struct Node* root) {
35+
if (root != NULL) {
36+
postOrderTraversal(root->left);
37+
postOrderTraversal(root->right);
38+
printf("%d ", root->data);
39+
}
40+
}
41+
42+
int main() {
43+
struct Node* root = createNode(1);
44+
root->left = createNode(2);
45+
root->right = createNode(3);
46+
root->left->left = createNode(4);
47+
root->left->right = createNode(5);
48+
root->right->left = createNode(6);
49+
root->right->right = createNode(7);
50+
51+
printf("In-order Traversal: ");
52+
inOrderTraversal(root);
53+
printf("\n");
54+
55+
printf("Pre-order Traversal: ");
56+
preOrderTraversal(root);
57+
printf("\n");
58+
59+
printf("Post-order Traversal: ");
60+
postOrderTraversal(root);
61+
printf("\n");
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)