-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.cpp
82 lines (82 loc) · 1.24 KB
/
BST.cpp
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
#include <stdio.h>
#include <stdlib.h>
typedef struct bst
{
int info;
struct bst *left,*right;
}BST;
BST* insert_BST(BST *,int);
BST* delet_BST(BST *,int);
void search_BST(BST *, int);
void inorder(BST *);
void swap(int *, int * );
/*******insert a node into BST*******/
BST* insert_BST(BST *root,int x)
{
BST *p, *nw;
nw=(BST*)malloc(sizeof(BST));
nw->info=x;
nw->left=nw->right=NULL;
if(root==NULL)
root=nw;
else
{
p=root;
while(p!=NULL)
{
if((x<p->info)&&(p->left!=NULL))
p=p->left;
else if((x>p->info)&&(p->right!=NULL))
p=p->right;
else
break;
}
if(x<p->info)
p->left=nw;
else
p->right=nw;
}
return root;
}
/*******Inorder Traversal of a BST*******/
void inorder(BST *root)
{
BST *p=root;
if(p!=NULL)
{
inorder(p->left);
printf("%d\t",p->info);
inorder(p->right);
}}
/*******Main Function*******/
int main()
{
BST *root=NULL;
int ele,ch;
do
{
printf("\t\n*****MENU*****\n");
printf("1. Insertion\n");
printf("2. Inorder Traversal\n");
printf("3. Exit\n");
printf("\n\t Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the data to be inserted:");
scanf("%d",&ele);
root=insert_BST(root,ele);
break;
case 2:
inorder(root);
break;
case 3:
exit(0);
default:
printf("\nERROR :- Invalid Option\n");
}
}while(1);
system("PAUSE");
return 0;
}