AVL Tree Program in C
AVL Tree Program in C
/*
3. */
4.
5. #include<stdio.h>
6. #include<stdlib.h>
7.
9. struct node
10. {
15. };
16.
19.
32.
34. {
38.
40. {
49.
52.
53. switch(user_choice)
54. {
55. case 1:
60.
61. case 2:
65. break;
66.
67. case 3:
72. {
74. }
75. else
76. {
78. }
79. break;
80. case 4:
81. inorder(root);
82. break;
83.
84. case 5:
85. preorder(root);
86. break;
87.
88. case 6:
89. postorder(root);
90. break;
91.
92. case 7:
94. return 1;
95.
96. default:
98. }
99.
102. }
103.
104. return 0;
105. }
106.
109. {
111.
114. {
117. }
122. }
123.
126. {
130.
134.
137. }
138.
141. {
149.
152. }
153.
156. {
159. return 0;
161. lh = 0;
162. else
163. lh = 1 + root->left->ht;
165. rh = 0;
166. else
167. rh = 1 + root->right->ht;
169. }
170.
173. {
174. int lh, rh;
176. {
177. return 0;
178. }
180. lh = 0;
181. else
182. lh = 1 + root->left->ht;
184. rh = 0;
185. else
186. rh = 1 + root->right->ht;
187.
191. }
192.
195. {
197. {
200. {
202. }
203. root = new_node;
204. }
206. {
209.
212. {
214. {
216. }
217. else
218. {
221. }
222. }
223. }
224. else
225. {
228.
230. if (balance_factor(root) == 2)
231. {
232. if (data < root->left->data)
233. {
235. }
236. else
237. {
240. }
241. }
242. }
246. }
247.
250. {
252.
254. {
256. }
257.
259. {
262. {
264. {
266. }
267. else
268. {
271. }
272. }
273. }
275. {
278. {
280. {
282. }
283. else
284. {
287. }
288. }
289. }
290. else
291. {
293. {
297.
300. if (balance_factor(root) == 2)
301. {
303. {
305. }
306. else
307. {
310. }
311. }
312. }
313. else
314. {
316. }
317. }
320. }
321.
324. {
326. {
328. }
329.
331. {
333. }
334.
336. {
338. }
339. else
340. {
342. }
343. }
344.
347. {
348. if (root == NULL)
349. {
350. return;
351. }
352.
353. inorder(root->left);
355. inorder(root->right);
356. }
357.
360. {
362. {
363. return;
364. }
365.
367. preorder(root->left);
368. preorder(root->right);
369. }
370.
373. {
375. {
376. return;
377. }
378.
379. postorder(root->left);
380. postorder(root->right);
382. }
Program Explanation
On Inserting a new node into the AVL Tree it is necessary to maintain the
balance factor of the tree. To insert a new node into the AVL tree, we have
to follow the given steps:
1. Insert a new element in the tree with the same binary search tree
logic.
10 15 20 9 5 16 17 8 6
In this case, we are inserting the nodes into AVL tree and finding
traversals.
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 34
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 78
Do you want to continue? y
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 99
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
34 78 99
Do you want to continue? y
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
78 34 99
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
34 99 78
In this case, we are deleting the nodes from the AVL tree.
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 34
Enter data: 78
Enter data: 99
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
34 78 99
Do you want to continue? y
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 78
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
34 99
1. Start comparing the key with the root node of the tree.
2. If the key is greater than the node value then move to the right
subtree.
3. If the key is less than the node value then move to the left subtree.
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 34
Enter data: 78
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
34 78 99
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 99
Node found
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 66