-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.c
155 lines (143 loc) · 3.61 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
155
#include<stdio.h>
#include<stdlib.h>
typedef struct BSTNODE
{
int info;
struct BSTNODE *left,*right;
} node;
node* createNode(int data)
{
node *n;
n=(node*)malloc(sizeof(node));
n->info=data;
n->left=n->right=NULL;
return n;
}
node* insertnode(node *node,int data)
{
if(node==NULL)
return createNode(data);
else if(data<node->info)
node->left=insertnode(node->left,data);
else
node->right=insertnode(node->right,data);
return node;
}
node* find(node *root,int data)
{
if(root==NULL || root->info==data)
return root;
else if(data<root->info)
return (find(root->left,data));
else if(data>root->info)
return (find(root->right,data));
}
node* findmin(node *root)
{
if(root==NULL)
return NULL;
else if(root->left==NULL)
return root;
return findmin(root->left);
}
node* findmax(node *root)
{
if(root==NULL)
return NULL;
else if(root->right==NULL)
return root;
return findmax(root->right);
}
node* delete_data(node* root,int data)
{
node *temp;
if(root==NULL)
return root;
else if(data<root->info)
delete_data(root->left,data);
else if(data>root->info)
delete_data(root->right,data);
else //ELement found
{
if(root->left && root->right) //if the node having the data have left and right tree
{
temp=findmin(root->right);
root->info=temp->info;
root->right=delete_data(root->right,root->info);
}
else //one or none child
{
temp=root;
if(root->left==NULL)
root=root->right;
if(root->right==NULL)
root=root->left;
free(temp);
}
}
return root;
}
void inOrder(node *root)
{
if(root)
{
inOrder(root->left);
printf("%d\t",root->info);
inOrder(root->right);
}
}
void main()
{
node *root=NULL;
int choice,data;
node *t;
while(1)
{
printf("\n1.Find in BST\n");
printf("2.Insert in BST\n");
printf("3.Delete from BST\n");
printf("4.Find minimum in BST\n");
printf("5.Find maximum in BST\n");
printf("6.Display Tree\n");
printf("7.Exit\n");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter element to search\n");
scanf("%d",&data);
t=find(root,data);
printf("Element found in node having address %d\n",t);
break;
case 2:
printf("Enter element to insert\n");
scanf("%d",&data);
root=insertnode(root,data);
break;
case 3:
printf("Enter element to delete\n");
scanf("%d",&data);
root=delete_data(root,data);
break;
case 4:
printf("%d is min value is BST",findmin(root));
break;
case 5:
printf("%d is max value is BST",findmax(root));
break;
case 6:
if(root)
inOrder(root);
else
printf("No data in tree\n");
break;
case 7:
exit(0);
break;
default:
printf("INVALID CHOICE\n");
break;
}
}
}