Struct: #Include #Include
Struct: #Include #Include
h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
1. }*root = NULL, *temp = NULL, *t2, *t1;
2.
3. void delete1();
4. void insert();
5. void delete();
6. void inorder(struct btnode *t);
7. void create();
8. void search(struct btnode *t);
9. void preorder(struct btnode *t);
10. void postorder(struct btnode *t);
11. void search1(struct btnode *t,int data);
12. int smallest(struct btnode *t);
13. int largest(struct btnode *t);
14.
15. int flag = 1;
16.
17. void main()
18. {
19. int ch;
20.
21. printf("\nOPERATIONS ---");
22. printf("\n1 - Insert an element into tree\n");
23. printf("2 - Delete an element from the tree\n");
24. printf("3 - Inorder Traversal\n");
25. printf("4 - Preorder Traversal\n");
26. printf("5 - Postorder Traversal\n");
27. printf("6 - Exit\n");
28. while(1)
29. {
30. printf("\nEnter your choice : ");
31. scanf("%d", &ch);
32. switch (ch)
33. {
34. case 1:
35. insert();
36. break;
37. case 2:
38. delete();
39. break;
40. case 3:
41. inorder(root);
42. break;
43. case 4:
44. preorder(root);
45. break;
46. case 5:
47. postorder(root);
48. break;
49. case 6:
50. exit(0);
51. default :
52. printf("Wrong choice, Please enter correct choice ");
53. break;
54. }
55. }
56. }
57.
58. /* To insert a node in the tree */
59. void insert()
60. {
61. create();
62. if (root == NULL)
63. root = temp;
64. else
65. search(root);
66. }
67.
68. /* To create a node */
69. void create()
70. {
71. int data;
72.
73. printf("Enter data of node to be inserted : ");
74. scanf("%d", &data);
75. temp = (struct btnode *)malloc(1*sizeof(struct btnode));
76. temp->value = data;
77. temp->l = temp->r = NULL;
78. }
79.
80. /* Function to search the appropriate position to insert the new node */
81. void search(struct btnode *t)
82. {
83. if ((temp->value > t->value) && (t->r != NULL)) /* value more th
an root node value insert at right */
84. search(t->r);
85. else if ((temp->value > t->value) && (t->r == NULL))
86. t->r = temp;
87. else if ((temp->value < t->value) && (t->l != NULL)) /* value less
than root node value insert at left */
88. search(t->l);
89. else if ((temp->value < t->value) && (t->l == NULL))
90. t->l = temp;
91. }
92.
93. /* recursive function to perform inorder traversal of tree */
94. void inorder(struct btnode *t)
95. {
96. if (root == NULL)
97. {
98. printf("No elements in a tree to display");
99. return;
100. }
101. if (t->l != NULL)
102. inorder(t->l);
103. printf("%d -> ", t->value);
104. if (t->r != NULL)
105. inorder(t->r);
106. }
107.
108. /* To check for the deleted node */
109. void delete()
110. {
111. int data;
112.
113. if (root == NULL)
114. {
115. printf("No elements in a tree to delete");
116. return;
117. }
118. printf("Enter the data to be deleted : ");
119. scanf("%d", &data);
120. t1 = root;
121. t2 = root;
122. search1(root, data);
123. }
124.
125. /* To find the preorder traversal */
126. void preorder(struct btnode *t)
127. {
128. if (root == NULL)
129. {
130. printf("No elements in a tree to display");
131. return;
132. }
133. printf("%d -> ", t->value);
134. if (t->l != NULL)
135. preorder(t->l);
136. if (t->r != NULL)
137. preorder(t->r);
138. }
139.
140. /* To find the postorder traversal */
141. void postorder(struct btnode *t)
142. {
143. if (root == NULL)
144. {
145. printf("No elements in a tree to display ");
146. return;
147. }
148. if (t->l != NULL)
149. postorder(t->l);
150. if (t->r != NULL)
151. postorder(t->r);
152. printf("%d -> ", t->value);
153. }
154.
155. /* Search for the appropriate position to insert the new node */
156. void search1(struct btnode *t, int data)
157. {
158. if ((data>t->value))
159. {
160. t1 = t;
161. search1(t->r, data);
162. }
163. else if ((data < t->value))
164. {
165. t1 = t;
166. search1(t->l, data);
167. }
168. else if ((data==t->value))
169. {
170. delete1(t);
171. }
172. }
173.
174. /* To delete a node */
175. void delete1(struct btnode *t)
176. {
177. int k;
178.
179. /* To delete leaf node */
180. if ((t->l == NULL) && (t->r == NULL))
181. {
182. if (t1->l == t)
183. {
184. t1->l = NULL;
185. }
186. else
187. {
188. t1->r = NULL;
189. }
190. t = NULL;
191. free(t);
192. return;
193. }
194.
195. /* To delete node having one left hand child */
196. else if ((t->r == NULL))
197. {
198. if (t1 == t)
199. {
200. root = t->l;
201. t1 = root;
202. }
203. else if (t1->l == t)
204. {
205. t1->l = t->l;
206.
}
207. else
208. {
209. t1->r = t->l;
210. }
211. t = NULL;
212. free(t);
213. return;
214. }
215.
216. /* To delete node having right hand child */
217. else if (t->l == NULL)
218. {
219. if (t1 == t)
220. {
221. root = t->r;
222. t1 = root;
223. }
224. else if (t1->r == t)
225. t1->r = t->r;
226. else
227. t1->l = t->r;
228. t == NULL;
229. free(t);
230. return;
231. }
232.
233. /* To delete node having two child */
234. else if ((t->l != NULL) && (t->r != NULL))
235. {
236. t2 = root;
237. if (t->r != NULL)
238. {
239. k = smallest(t->r);
240. flag = 1;
241. }
242. else
243. {
244. k =largest(t->l);
245. flag = 2;
246. }
247. search1(root, k);
248. t->value = k;
249. }
250.
251. }
252.
253. /* To find the smallest element in the right sub tree */
254. int smallest(struct btnode *t)
255. {
256. t2 = t;
257. if (t->l != NULL)
258. {
259. t2 = t;
260. return(smallest(t->l));
261. }
262. else
263. return (t->value);
264. }
265.
266. /* To find the largest element in the left sub tree */
267. int largest(struct btnode *t)
268. {
269. if (t->r != NULL)
270. {
271. t2 = t;
272. return(largest(t->r));
273. }
274. else
275. return(t->value);
276. }