# Program for AVL Tree in C

Here you will get program for AVL tree in C.

An AVL (Adelson-Velskii and Landis) tree is a height balance tree. These trees are binary search trees in which the height of two siblings are not permitted to differ by more than one. i.e. [Height of the left subtree – Height of right subtree] <= 1.

A C program is given below which performs various operations like creation, insertion, deletion and printing for an AVL tree.

## Program for AVL Tree in C

Output

1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:

Enter no. of elements:4

Enter tree data:7 12 4 9

1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:

Preorder sequence:
7(Bf=-1)4(Bf=0)12(Bf=1)9(Bf=0)

Inorder sequence:
4(Bf=0)7(Bf=-1)9(Bf=0)12(Bf=1)

1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:

Enter a data:7

1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:

Preorder sequence:
9(Bf=0)4(Bf=0)12(Bf=0)

Inorder sequence:
4(Bf=0)9(Bf=0)12(Bf=0)

1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:

### 18 thoughts on “Program for AVL Tree in C”

1. Vipin Chaudhary

what the function BF will do?

1. BF function is to calculate the difference of level between left and right subtrees

2. i need real easy program

2. Thanks for the implementation.

3. Hey.. I wan to write an iterative C function that displays the integer values of a given AVL tree ..if somebody can help would be perfect ?

4. Not working properly
if i select create node and enter 5 elements 1 to 5 in order then it is giving the wrong values of BF for each node

1. yes..you are right…it is not working for that case.

1. first check it…its working.

2. Its working yaar

5. there is a mistake regarding the content of the avl tree

6. Does anyone know a function to count the number of rotations of an avl tree?

7. hello my windows is closing while im using delete and indicating that it has a problm with program

8. can i get the algorithm for this program????pls

9. Aritra Brahmachari

The height() function is not proper.
if(T->left==NULL)
lh=1; //this programmer uses lh=0 which is wrong
Same goes for T->right==NULL.
Hope this helps.

10. Is this self-balancing tree?

11. [Error] ‘malloc’ was not declared in this scope
Can someone help me? What should I do with this error?

1. At the beginning insert the sentence below:
#include

12. very nice, just one comment: if there is malloc, there also should be free.
The free should be near the end of the Delete function: