Anda di halaman 1dari 11

902,378 Members | 

Top Members by Rank

 JOIN DANIWEB

 
 MEMBER LOG IN

 
 Hardware & Software
 Software Development
 Web Development
 Internet Marketing
 Business Exchange
 Community Center

Software Development > C > Creating AVL Tree

Ad: 

HelpDesk or Customer Support


 

 C Code Snippet

 Views: 17742

0
Creating AVL Tree
by anurag_pareek on May 23rd, 2005

it is a simple program but the beuty lies in its shortness it is written in least line as possible
By :- 
Anurag Pareek

C Code Snippet (Toggle Plain Text)

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.  
390.  
391.

Comments on this Code Snippet

Permalink

Jun 7th, 2009

0
Re: Creating AVL Tree
well great code..
but i guess it can be shortened more.....
sumeet.coolguy
Newbie Poster

 Offline
1 posts
since Jun 2009

Message:

Post Reply to this Code Snippet

Previous Thread in C Forum Timeline: Scrabble in C


Next Thread in C Forum Timeline: write the best stack !!

Tag Cloud for C


6 adt aes algorithm  array arrays  basic c++ calendar calling char character characterarray code comparing compiling conditions conver

ter cprogramming crash csv  c_programming dynamic embeddedencryption error execvp expression fault file files fopen for
k function functions gcc handling help! hex hexadecimal homeworkassignment image iteration linked linked-list linux list loop loops malloc 

matrix memorymicrocontroller mingw multiple multithreading number numbers opening_web_sites output parking pipe pointer pointers posix 

prime problem program programming pthread pthreads random read recursion reference return reusescanf() segmentationfault server shell s

ocket socket_programming sort sorting split statement string strings struct structures switch tax tree triangle ubuntu unix variable wind

ows winsock

About Us | Contact Us | Advertise | Acceptable Use Policy

Forum Index | Build Custom RSS Feed

Follow us on 
© 2010 DaniWeb® LLC

Web based IT HelpDesk with Asset Mgmt or Customer support Software with Account ... DOWNLOAD YOUR FREE
TRIAL TODAY! www.servicedeskplus.com

Anda mungkin juga menyukai