0% found this document useful (0 votes)
27 views6 pages

# Include # Include # Define F 0 # Define T 1

This document defines functions for creating, balancing, and deleting nodes from a binary search tree data structure implemented with C structs and pointers. It includes functions to insert nodes, balance the tree during insertion if it becomes left or right heavy, delete nodes by replacing them with the last node of their left subtree or right child, and balance the tree during deletion. The main function demonstrates inserting nodes into an empty tree until the user inputs 'b', then allows deleting nodes by key until 'b' is input again.

Uploaded by

Bijendra Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

# Include # Include # Define F 0 # Define T 1

This document defines functions for creating, balancing, and deleting nodes from a binary search tree data structure implemented with C structs and pointers. It includes functions to insert nodes, balance the tree during insertion if it becomes left or right heavy, delete nodes by replacing them with the last node of their left subtree or right child, and balance the tree during deletion. The main function demonstrates inserting nodes into an empty tree until the user inputs 'b', then allows deleting nodes by key until 'b' is input again.

Uploaded by

Bijendra Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. # include<stdio.

h>
2. # include<malloc.h>
3.  
4. # define F 0
5. # define T 1
6.  
7. struct NODE
8. {
9. char Info;
10. int Flag;
11. struct NODE *Left_Child;
12. struct NODE *Right_Child;
13. };
14.  
15. struct NODE *Binary_Tree (char , struct NODE *, int *);
16. void Output(struct NODE *, int );
17. struct NODE *Balance_Right_Heavy(struct NODE *, int *);
18. struct NODE *Balance_Left_Heavy(struct NODE *, int *);
19. struct NODE *DELETE(struct NODE *, struct NODE *, int *);
20. struct NODE *Delete_Element(struct NODE *, char , int *);
21.  
22. /* Function to insert an element into tree */
23.  
24. struct NODE * Binary_Tree (char Info, struct NODE *Parent, int *H)
25. {
26. struct NODE *Node1;
27. struct NODE *Node2;
28. if(!Parent)
29. {
30. Parent = (struct NODE *) malloc(sizeof(struct NODE));
31. Parent->Info = Info;
32. Parent->Left_Child = NULL;
33. Parent->Right_Child = NULL;
34. Parent->Flag = 0;
35. *H = T;
36. return (Parent);
37. }
38.  
39. if(Info < Parent->Info)
40. {
41. Parent->Left_Child = Binary_Tree(Info, Parent->Left_Child, H);
42. if(*H)
43. /* Left branch has grown higher */
44. {
45. switch(Parent->Flag)
46. {
47. case 1: /* Right heavy */
48. Parent->Flag = 0;
49. *H = F;
50. break;
51. case 0: /* Balanced tree */
52. Parent->Flag = -1;
53. break;
54. case -1: /* Left heavy */
55. Node1 = Parent->Left_Child;
56. if(Node1->Flag == -1)
57. {
58. printf("\n Left to Left Rotation\n");
59. Parent->Left_Child= Node1->Right_Child;
60. Node1->Right_Child = Parent;
61. Parent->Flag = 0;
62. Parent = Node1;
63. }
64. else
65. {
66. printf("\n Left to right rotation\n");
67. Node2 = Node1->Right_Child;
68. Node1->Right_Child = Node2->Left_Child;
69. Node2->Left_Child = Node1;
70. Parent->Left_Child = Node2->Right_Child;
71. Node2->Right_Child = Parent;
72. if(Node2->Flag == -1)
73. Parent->Flag = 1;
74. else
75. Parent->Flag = 0;
76. if(Node2->Flag == 1)
77. Node1->Flag = -1;
78. else
79. Node1->Flag = 0;
80. Parent = Node2;
81. }
82.  
83. Parent->Flag = 0;
84. *H = F;
85. }
86. }
87. }
88.  
89. if(Info > Parent->Info)
90. {
91. Parent->Right_Child = Binary_Tree(Info, Parent->Right_Child, H);
92. if(*H)
93. /* Right branch has grown higher */
94. {
95. switch(Parent->Flag)
96. {
97. case -1: /* Left heavy */
98. Parent->Flag = 0;
99. *H = F;
100. break;
101. case 0: /* Balanced tree */
102. Parent->Flag = 1;
103. break;
104.  
105. case 1: /* Right heavy */
106. Node1 = Parent->Right_Child;
107. if(Node1->Flag == 1)
108. {
109. printf("\n Right to Right Rotation\n");
110. Parent->Right_Child= Node1->Left_Child;
111. Node1->Left_Child = Parent;
112. Parent->Flag = 0;
113. Parent = Node1;
114. }
115. else
116. {
117. printf("\n Right to Left Rotation\n");
118. Node2 = Node1->Left_Child;
119. Node1->Left_Child = Node2->Right_Child;
120. Node2->Right_Child = Node1;
121. Parent->Right_Child = Node2->Left_Child;
122. Node2->Left_Child = Parent;
123.  
124. if(Node2->Flag == 1)
125. Parent->Flag = -1;
126. else
127. Parent->Flag = 0;
128. if(Node2->Flag == -1)
129. Node1->Flag = 1;
130. else
131. Node1->Flag = 0;
132. Parent = Node2;
133. }
134.  
135. Parent->Flag = 0;
136. *H = F;
137. }
138. }
139. }
140. return(Parent);
141. }
142.  
143. /* Output function */
144.  
145. void Output(struct NODE *Tree,int Level)
146. {
147. int i;
148. if (Tree)
149. {
150. Output(Tree->Right_Child, Level+1);
151. printf("\n");
152. for (i = 0; i < Level; i++)
153. printf(" ");
154. printf("%c", Tree->Info);
155. Output(Tree->Left_Child, Level+1);
156. }
157. }
158.  
159. /* Balancing Right Heavy */
160.  
161. struct NODE * Balance_Right_Heavy(struct NODE *Parent, int *H)
162. {
163. struct NODE *Node1, *Node2;
164.  
165. switch(Parent->Flag)
166. {
167. case -1:
168. Parent->Flag = 0;
169. break;
170.  
171. case 0:
172. Parent->Flag = 1;
173. *H= F;
174. break;
175.  
176. case 1: /* Rebalance */
177. Node1 = Parent->Right_Child;
178. if(Node1->Flag >= 0)
179. {
180. printf("\n Right to Right Rotation\n");
181. Parent->Right_Child= Node1->Left_Child;
182. Node1->Left_Child = Parent;
183. if(Node1->Flag == 0)
184. {
185. Parent->Flag = 1;
186. Node1->Flag = -1;
187. *H = F;
188. }
189. else
190. {
191. Parent->Flag = Node1->Flag = 0;
192. }
193. Parent = Node1;
194. }
195. else
196. {
197. printf("\n Right to Left Rotation\n");
198. Node2 = Node1->Left_Child;
199. Node1->Left_Child = Node2->Right_Child;
200. Node2->Right_Child = Node1;
201. Parent->Right_Child = Node2->Left_Child;
202. Node2->Left_Child = Parent;
203.  
204. if(Node2->Flag == 1)
205. Parent->Flag = -1;
206. else
207. Parent->Flag = 0;
208. if(Node2->Flag == -1)
209. Node1->Flag = 1;
210. else
211. Node1->Flag = 0;
212. Parent = Node2;
213. Node2->Flag = 0;
214. }
215. }
216. return(Parent);
217. }
218.  
219. /* Balancing Left Heavy */
220.  
221. struct NODE * Balance_Left_Heavy(struct NODE *Parent, int *H)
222. {
223. struct NODE *Node1, *Node2;
224.  
225. switch(Parent->Flag)
226. {
227. case 1:
228. Parent->Flag = 0;
229. break;
230.  
231. case 0:
232. Parent->Flag = -1;
233. *H= F;
234. break;
235.  
236. case -1: /* Rebalance */
237. Node1 = Parent->Left_Child;
238. if(Node1->Flag <= 0)
239. {
240. printf("\n Left to Left Rotation\n");
241. Parent->Left_Child= Node1->Right_Child;
242. Node1->Right_Child = Parent;
243. if(Node1->Flag == 0)
244. {
245. Parent->Flag = -1;
246. Node1->Flag = 1;
247. *H = F;
248. }
249. else
250. {
251. Parent->Flag = Node1->Flag = 0;
252. }
253. Parent = Node1;
254. }
255. else
256. {
257. printf("\n Left to Right Rotation\n");
258. Node2 = Node1->Right_Child;
259. Node1->Right_Child = Node2->Left_Child;
260. Node2->Left_Child = Node1;
261. Parent->Left_Child = Node2->Right_Child;
262. Node2->Right_Child = Parent;
263.  
264. if(Node2->Flag == -1)
265. Parent->Flag = 1;
266. else
267. Parent->Flag = 0;
268.  
269. if(Node2->Flag == 1)
270. Node1->Flag = -1;
271. else
272. Node1->Flag = 0;
273. Parent = Node2;
274. Node2->Flag = 0;
275. }
276. }
277. return(Parent);
278. }
279.  
280. /* Replace the node at which key is found with last right key of a left child */
281.  
282. struct NODE * DELETE(struct NODE *R, struct NODE *Temp, int *H)
283. {
284. struct NODE *Dnode = R;
285. if( R->Right_Child != NULL)
286. {
287. R->Right_Child = DELETE(R->Right_Child, Temp, H);
288. if(*H)
289. R = Balance_Left_Heavy(R, H);
290. }
291. else
292. {
293. Dnode = R;
294. Temp->Info = R->Info;
295. R = R->Left_Child;
296. free(Dnode);
297. *H = T;
298. }
299. return(R);
300. }
301. /* Delete the key element from the tree */
302.  
303. struct NODE * Delete_Element(struct NODE *Parent, char Info, int *H)
304. {
305. struct NODE *Temp;
306. if(!Parent)
307. {
308. printf("\n Information does not exist");
309. return(Parent);
310. }
311. else
312. {
313. if (Info < Parent->Info )
314. {
315. Parent->Left_Child = Delete_Element(Parent->Left_Child, Info, H);
316. if(*H)
317. Parent = Balance_Right_Heavy(Parent, H);
318. }
319. else
320. if(Info > Parent->Info)
321. {
322. Parent->Right_Child = Delete_Element(Parent->Right_Child, Info, H);
323. if(*H)
324. Parent = Balance_Left_Heavy(Parent, H);
325. }
326. else
327. {
328. Temp= Parent;
329. if(Temp->Right_Child == NULL)
330. {
331. Parent = Temp->Left_Child;
332. *H = T;
333. free(Temp);
334. }
335. else
336. if(Temp->Left_Child == NULL)
337. {
338. Parent = Temp->Right_Child;
339. *H = T;
340. free(Temp);
341. }
342. else
343. {
344. Temp->Left_Child = DELETE(Temp->Left_Child, Temp, H);
345. if(*H)
346. Parent = Balance_Right_Heavy(Parent, H);
347. }
348. }
349. }
350. return(Parent);
351. }
352.  
353. /* Function main */
354.  
355. void main()
356. {
357. int H;
358. char Info ;
359. char choice;
360. struct NODE *Tree = (struct NODE *)malloc(sizeof(struct NODE));
361. Tree = NULL;
362. printf("\n Input choice 'b' to break:");
363. choice = getchar();
364. while(choice != 'b')
365. {
366. fflush(stdin);
367. printf("\n Input information of the node: ");
368. scanf("%c", &Info);
369. Tree = Binary_Tree(Info, Tree, &H);
370. printf("\n Tree is:\n");
371. Output(Tree, 1);
372. fflush(stdin);
373. printf("\n Input choice 'b' to break:");
374. choice = getchar();
375. }
376. fflush(stdin);
377. while(1)
378. {
379. printf("\n Input choice 'b' to break:");
380. printf("\n Input the key value want to deletedir:");
381. scanf("%c", &Info);
382. if (Info == 'b')
383. break;
384. Tree = Delete_Element(Tree, Info, &H);
385. printf("\n Tree is:\n");
386. Output(Tree, 1);
387. }
388. }
389.

You might also like