Skip to content

Commit cea0ea6

Browse files
Add files via upload
1 parent d38a343 commit cea0ea6

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

implementation_of_bst.c

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
typedef struct tree
4+
{
5+
struct tree*left;
6+
int info;
7+
struct tree*right;
8+
}tree;
9+
void insert(tree** root,int v)
10+
{
11+
if(*root==NULL)
12+
{
13+
tree*temp=(tree*)malloc(sizeof(tree));
14+
temp->info=v;
15+
temp->left=temp->right=NULL;
16+
*root=temp;
17+
}
18+
else if(v>((*root)->info))
19+
{
20+
insert(&((*root)->right),v);
21+
}
22+
else
23+
{
24+
insert(&((*root)->left),v);
25+
}
26+
}
27+
void inorder(tree *root)
28+
{
29+
if(root!=NULL)
30+
{
31+
inorder(root->left);
32+
printf("%d\t",root->info);
33+
inorder(root->right);
34+
}
35+
}
36+
void preorder(tree *root)
37+
{
38+
if(root!=NULL)
39+
{
40+
printf("%d\t",root->info);
41+
preorder(root->left);
42+
preorder(root->right);
43+
}
44+
}
45+
void postorder(tree *root)
46+
{
47+
if(root!=NULL)
48+
{
49+
postorder(root->left);
50+
postorder(root->right);
51+
printf("%d\t",root->info);
52+
}
53+
}
54+
int main()
55+
{
56+
tree *root=NULL;
57+
int c,v;
58+
do{
59+
printf("1-Insert element in BST\n2-Inorder traversal of BST\n3-Preorder traversal of BST\n4-Postorder traversal of BST\nEnter your choice : ");
60+
scanf("%d",&c);
61+
switch (c)
62+
{
63+
case (1):
64+
printf("Enter the element to be inserted : ");
65+
scanf("%d",&v);
66+
insert(&root,v);
67+
break;
68+
case (2):
69+
printf("The inorder traversal of the BST is : ");
70+
inorder(root);
71+
break;
72+
case (3):
73+
printf("The preorder traversal of the BST is : ");
74+
preorder(root);
75+
break;
76+
case (4):
77+
printf("The postorder traversal of the BST is : ");
78+
postorder(root);
79+
break;
80+
}
81+
}while(c>0&&c<5);
82+
return 0;
83+
}

0 commit comments

Comments
 (0)