-
Notifications
You must be signed in to change notification settings - Fork 2
/
binary tree.c
214 lines (213 loc) · 5.49 KB
/
binary tree.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *result=NULL,*root = NULL,*res=NULL;
int found = 0;
void insert()
{
struct node *temp,*ptr,*preptr;
int key;
char ch;
printf("\nEnter the data to be inserted:");
scanf("%d",&key);
temp = (struct node*)malloc(sizeof(struct node));
temp -> data = key;
temp -> left = NULL;
temp -> right = NULL;
if(root == NULL)
root = temp;
else
{
ptr = root;
while(1)
{
fflush(stdin);
preptr = ptr;
printf("\n\nSelect your position (L or R) : ");
scanf("%c",&ch);
if(ch == 'R' || ch == 'r')
{
ptr = ptr -> right;
if(ptr == NULL)
{
preptr->right = temp;
return;
}
printf("\n\t\tInside right subtree.....");
}
else if(ch == 'L' || ch == 'l')
{
ptr = ptr ->left;
if(ptr == NULL)
{
preptr->left = temp;
return;
}
printf("\n\t\tInside left subtree.....");
}
else
printf("\n\t\tChoose correct option!!");
}
}
}
void display(struct node *root)
{
if(root != NULL)
{
display(root -> left);
printf("%d\t",root -> data);
display(root -> right);
}
}
struct node* search(struct node *root, int num)
{
if(root != NULL)
{
if(root -> data == num)
{
printf("\n\tElement is found!!!");
found = 1;
res=root;
return root;
}
search(root -> left, num);
search(root -> right, num);
}
if(root == NULL)
return NULL;
}
void prepointer(struct node *ptr,int num)
{
if(ptr != NULL)
{
if(ptr->right!=NULL)
if(ptr ->right->data == num)
{
result = ptr;
return;
}
if(ptr->left!=NULL)
if(ptr ->left->data == num)
{
result = ptr;
return;
}
prepointer(ptr -> left, num);
prepointer(ptr -> right, num);
}
}
struct node *last(struct node *ptr)
{
while(ptr -> right != NULL || ptr ->left != NULL)
{
while(ptr -> right != NULL)
ptr = ptr -> right;
while(ptr ->left != NULL)
ptr = ptr ->left;
}
return ptr;
}
struct node* del_last(struct node *root, int num)
{
struct node *preptr;
prepointer(root,num);
preptr = result;
if(preptr ->left!=NULL)
if(preptr -> left -> data == num)
preptr -> left = NULL;
if(preptr ->right!=NULL)
if(preptr -> right -> data == num)
preptr -> right = NULL;
return root;
}
struct node* delete(struct node *root, int num)
{
struct node *ptr, *preptr, *least;
ptr = root;
if(root == NULL)
return NULL;
else if (root -> data == num)
{
if(ptr -> left == NULL && ptr -> right == NULL)
{
printf("Successfully deleted!!!!");
return NULL;
}
else
{
if(root->right != NULL)
{
least = last(root);
root = del_last(root, least -> data);
root -> data = least -> data;
}
else
{
ptr = last(root->left);
del_last(root, ptr->data);
root -> data = ptr -> data;
}
}
printf("\nSuccessfully deleted!!!!");
return root;
}
else
{
search(root,num);
if(res == NULL)
{
printf("\nElement not found !!!!\n");
return root;
}
if(res -> left == NULL && res -> right == NULL)
del_last(root, num);
else
{
least = last(root);
del_last(root, least -> data);
res -> data = least -> data;
}
res = NULL;
printf("\nSuccessfully deleted!!!!");
return root;
}
}
int main()
{
int opt, ch = 0,num;
while(1){
printf("\n\n-------------BINARY TREE-------------\n\n1.Insert\n\n2.Display\n\n3.Search\n\n4.Delete\n\n5.Exit\n\nEnter your choice:");
scanf("%d",&opt);
switch(opt)
{
case 1:insert();break;
case 2:if(root==NULL)
{
printf("\nTree is Empty\n");
break;
}
display(root);break;
case 3:printf("\nEnter the number to be searched:");
scanf("%d",&num);
search(root,num);
if(found == 0)
printf("\n\tElement not found!!!");
found = 0;
res = NULL;
break;
case 4:printf("\nEnter the number to be deleted:");
scanf("%d",&num);
root=delete(root,num);
break;
case 5:exit(0);break;
default:printf("Enter correct option!!!");
}
}
return 0;
}