-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew.cpp
216 lines (212 loc) Β· 3.21 KB
/
New.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <stdlib.h>
typedef struct bst{
int info;
struct bst *left,*right;
}bst;
bst *insert(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;
}
bst *delete(bst *root, int x){
BST *p,*q,*u;
p=root;
q=NULL; /* p is the node to be deleted & q is the parent of p*/
while(p!=NULL){ /search routines begins/
if(x<p->info){
q=p;
p=p->left;
} else if(x>p->info){
q=p;
p=p->right;
} else
break;
}/Search Routine Ends/
/case1: The node to be deleted (p) is a leaf node/
if((p->left==NULL)&&(p->right==NULL)){
if(q==NULL){ /subcase1: p is the root node i.e. q is NULL/
free(p);
root=NULL;
} else{ /*subcase2: q
is not NULL*/
if(q->left==p)
q->left=NULL;
else
q->right=NULL;
free(p);
}
} /* End of case1*/
/case2: The node to be deleted (p) has only left child/
else if((p->left!=NULL)&&(p->right==NULL)){
if(q==NULL){ /subcase1: p is the root node i.e. q is NULL/
root=p->left;
p->left=NULL;
free(p);
} else{ /*subcase2: q
is not NULL*/
if(q->left==p)
{
q->left=p->left;
p->left=NULL;
free(p);
}
else
{
q->right=p->left;
p->left=NULL;
free(p);
}
}
} /* End of case2*/
/case3: The node to be deleted (p) has only right child/
else if((p->left==NULL)&&(p->right!=NULL)){
if(q==NULL){ /subcase1: p is the root node i.e. q is NULL/
root=p->right;
p->right=NULL;
free(p);
} else{ /*subcase2: q
is not NULL*/
if(q->right==p)
{
q->right=p-> right;
p-> right=NULL;
free(p);
}
else
{
q->left=p->right;
p-> right =NULL;
free(p);
}
}
} /* End of case3*/
/case4: The node to be deleted (p) has both left and right child/
else
{
q=p;
u=p->right;
while(u->left!=NULL)
{
q=u;
u=u->left;
}
swap(&(p->info), &(u->info));
if(q->left==u)
q->left=u->right;
else
q->right=u->right;
free(u);
}/end of case4/
return root;
}/end of function/
void swap(int *i,int*j)
{
int t=*i;
*i=*j;
*j=t;
}
void searh(bst *root,int x)
{
bst *p;
if(root==NULL)
{
printf("\n Imposiible\n");
exit(0);
}
else
{
p=root;
while(p!=NULL)
{
if(x<p->info)
{
p=p->left;
}
else if(x>p->info)
{
p=p->right;
}
else
printf("\nfound\n");
}
if(p==NULL)
printf("\nfound\n");
}
}
void inorder(bst * root)
{
bst *p=root;
if(p!=NULL)
{
inorder(p->left);
printf("%d\t",p->info);
inorder(p->right);
}
}
int main()
{
bst *root=NULL;
int ele,ch;
do{
printf("\n*****MENU***\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("4. Inorder Traversal\n");
printf("5. EXIT\n");
printf("\t\t\tEnter your choice [1-5]:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the node information to be insert:");
scanf("%d",&ele);
root=insert(root,ele);
break;
case 2:
printf("Enter the node information to be deleted:");
scanf("%d",&ele);
root=delete(root,ele);
break;
case 3:
printf("Enter the node to be search:");
scanf("%d",&ele);
searh(root,ele);
break;
case 4:
inorder(root);
break;
case 5:
printf("\nThank You.\n");
exit(0);
default:
printf("\nInvalid Choice\n");
}
}while(1);
return 0;
}