-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearchtree.c
196 lines (184 loc) · 3.82 KB
/
binarysearchtree.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left,*right;
};
struct node *root=NULL;
void main()
{
void insert(int);
struct node* search(struct node*,int);
int dele(int);
void inorder(struct node*);
int option,data,k;
printf("#########\tBINARY SEARCH TREE\t#########\n");
do
{
printf("\tCHOICES FOR YOU\n");
printf("1.INSERT\n");
printf("2.SEARCH\n");
printf("3.DELETE\n");
printf("4.DISPLAY\n");
printf("5.EXIT\n");
printf("enter your option:\n");
scanf("%d",&option);
switch(option)
{
case 1:printf("Enter data to insert:\n");
scanf("%d",&data);
insert(data);
break;
case 2:printf("Enter data to search:\n");
scanf("%d",&data);
search(root,data);
break;
case 3:printf("Enter data to delete:\n");
scanf("%d",&data);
k=dele(data);
if(k==0)
printf("deleted item is:%d \n",data);
break;
case 4:if(root==NULL)
printf("NULL TREE......\n");
else {
printf("Inorder traversal result: ");
inorder(root);
printf("\n");
}
break;
case 5:printf("Exiting..\n");
exit(0);
}
}while(1);
}
//function to insert a node
void insert(int x)
{
struct node *t,*t1,*t2;
t=(struct node*)malloc(sizeof(struct node));
t->data=x;
t->left=t->right=NULL;
if(root==NULL)//case of empty tree
root=t;
else
{
t1=root;
while(t1!=NULL && t1->data!=x)//condition to avoid inserting same again
{
t2=t1;//remembering parent node
if(x>t1->data)
t1=t1->right;
else
t1=t1->left;
}
if(t1!=0)
printf("duplication not allowed..\n");
else//actual insertion code block
{
if(x>t2->data)//case of new data greather than parent
t2->right=t;
else//case of new data lower than parent
t2->left=t;
}
}
}
//function to search an item
struct node* search(struct node* root,int a)
{
if(root==NULL)//case of empty tree
printf("NOT FOUND...\n");
else if(root->data==a)
{
printf("ITEM FOUND...\n");
}
else
{
if(a>root->data)//case of right subtree
return search(root->right,a);
else //case of left subtree
return search(root->left,a);
}
}
//function to delete a node
int dele(int item)
{
struct node *t1,*t2,*t3,*sucpar,*suc;
t1=root;
//loop to find the node to be deleted
while(t1!=NULL && t1->data!=item)
{
t2=t1;
if(item>t1->data)
t1=t1->right;
else
t1=t1->left;
}
//after the loop t1 points to node to be deletd
//t2 points to parent of the node to be deleted
//t1==NULL implies not found
if(t1==NULL)
printf("NOT FOUND...\n");
else //element found case
{
if(t1->right==NULL && t1->left==NULL)//case of leaf node
{
if(t1!=root){
if(t2->data>item)
t2->left=NULL;
else
t2->right=NULL;
}
else
root=NULL;
}
else if(t1->left==NULL || t1->right==NULL)//case of single child
{
if(t1!=root)
{
if(item>t2->data)//case of deleting node on the right side of parent
t2->right=t1->left==NULL?t1->right:t1->left;//case of deleting node have child on left or right
else
t2->left=t1->left=NULL?t1->right:t1->left;
}
else{
if(root->right!=NULL)
root=root->right;
else
root=root->left;
}
}
else//case of 2 child
//inorder successor will be left extreme of right subtree
{
sucpar=t1;
suc=t1->right;
while(suc->left!=NULL)
{
sucpar=suc;
suc=suc->left;
}
//copy the inorder succesor to the node to be deleted
t1->data=suc->data;
//next step is to add link to inorder succesor parent
//for that we need to find left or right of sucpar
if(suc->data<sucpar->data)
sucpar->left=suc->right;
else
sucpar->right=suc->right;
t1=suc;
}
free(t1);//comon removal for all cases
return 0;
}
}
void inorder(struct node *t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d,",t->data);
inorder(t->right);
}
}