-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.c
154 lines (143 loc) · 2.7 KB
/
BST.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
int flag=0;
typedef struct BST
{
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *,node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);
node *get_node()
{
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void insert (node *root, node *new_node)
{
if (new_node->data<root->data)
{
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data)
{
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
node *search(node *root, int key, node **parent)
{
node *temp;
temp=root;
while(temp != NULL)
{
if (temp->data==key)
{
printf("\nThe %d Element is Present", temp->data) ;
flag=1;
return temp;
}
*parent=temp;
if (temp->data>key)
temp=temp->lchild;
else
temp=temp->rchild;
}return NULL;
}
void inorder(node *temp)
{
if(temp != NULL)
{
inorder (temp -> lchild) ;
printf ("\t%d\t", temp->data) ;
inorder (temp->rchild) ;
}
}
void preorder(node *temp)
{
if (temp != NULL)
{
printf ("\t%d\t", temp->data) ;
preorder (temp->lchild) ;
preorder (temp->rchild) ;
}
}
void postorder(node *temp)
{
if (temp != NULL)
{
postorder (temp->lchild) ;
postorder (temp->rchild) ;
printf ("\t%d\t", temp->data) ;
}
}
void main()
{
int choice;
int ans = 1;
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
printf("\nProgram For Binary Search Tree ");
do
{
printf ("\nl.Create");
printf ("\n2.Search");
printf ("\n3.Recursive Traversals") ;
printf ("\n4.Exit") ;
printf("\nEnter your choice :");
scanf("%d", &choice) ;
switch (choice)
{
case 1:
do
{
new_node = get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data) ;
if (root == NULL)
root = new_node;
else
insert (root, new_node);
printf("\nWant To enter More Elements? (1/0)");
scanf("%d",&ans);
}while(ans);
break;
case 2:
printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
if (flag==1)
printf("\nParent of node %d is %d", tmp->data, parent->data);
else
printf("\n The %d Element is not Present", key) ;
flag=0;
break;
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else
{
printf("\nThe Inorder display : ");
inorder (root) ;
printf("\nThe Preorder display : ");
preorder (root) ;
printf("\nThe Postorder display : ");
postorder (root) ;
}
break;
}
} while (choice != 4);
}