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 Your Choice:1

Enter no. of elements:4

Enter tree data:7 12 4 9

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

Enter Your Choice:4

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 Your Choice:3

Enter a data:7

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

Enter Your Choice:4

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:

Enter Your Choice:5

18 thoughts on “Program for AVL Tree in C”

  1. 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 ?

  2. 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

  3. 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.

  4. 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:
    instead of
    else
    return(T->left);
    the following should be used:
    else {
    node *TL=T->left;
    free (T);
    return(TL);
    }

Leave a Comment

Your email address will not be published. Required fields are marked *