You are on page 1of 7

AVL Tree (Height Balanced tree)

Previously we have studied Binary Search Tree and we learned that Binary
Search Trees provide efficient access to data. Using binary search trees we
can implement basic operations such as Search, Insertion, Deletion
Find-Max and Find-Min in log (n) time.

Search
Insertion
Deletion
Find minimum value in BST.
Find maximum value in BST.

All these operations run in


log (n) time, i.e. Equal to
height of Binary Search
Tree.

Binary search tree provide efficient access of data in log (n) time if and only
if binary search tree is balanced.
If binary search tree is not balanced, then above operations are performed
in linear time i.e. O (n) time, which is a poor asymptotic complexity.

As we know insertion of elements in binary search tree in some order


(ascending or descending order) can lead to a highly unbalanced tree,
a.k.a. degenerate or "almost degenerate" with most of the n elements
descending as a linked list in one of the sub-tree of a node.
For example when we insert numbers from 1 to 8 in a Binary Search Tree
in ascending order, then tree no longer remain BST, but behave more like
linked list. So the search efficiency of the tree becomes O (n).

Note
Balanced BST maintains h O (log n) BST operations run in O (log n) time.
Unbalanced BST maintains h = O (n) BST operations run in O (n) time.

Unbalanced binary search tree are undesirable because of its linear time
complexity. So balanced binary search trees are more desirable, but Question
arise is, how to maintain tree to be a well-balanced if some operations
(like insertion, deletion) put behind the tree to be unbalanced.
As we know Complete Binary Tree is always balanced tree. So, can we use
complete binary tree for maintaining tree to a balanced?
No!! We dont use complete binary tree for maintaining Tree to be balanced.
Because this become very expensive task.
We want complete binary search tree after every operation because the
insert or delete operation may cause the tree no longer to be complete binary
tree. So after every operation (insert/deletion) we need to restore its binary
search tree and completeness property, which is very expensive task.
Example

Insert 2

Insert a/c to
BST property

2
After every insert or delete operation, we need

to restore completeness property along with


Binary search tree property, which is very

expensive task. Hence complete binary tree is


not used to maintain tree to be a balanced.

8
4

To address this problem, researchers Adelson, Velskii and Landis defined the
concept of height-balance for a node and developed new insert and delete
algorithms that would reorder the elements to maintain the tree heightbalance. So the binary search trees with these new algorithms are known as
AVL trees after their creators.
An AVL tree is a special binary search tree, with some additional restriction
imposed on it (binary search tree).
i.e.

AVL Tree = Binary Search Tree + Some Restriction

These Restriction are: Tree must be balanced (fully balanced or partially balanced).
Balance factor is associated with every node of an AVL tree, which assure the
balanced property of tree.
Balance factor = height (left sub-tree) height (right sub-tree)
AVL tree has balance factor calculated at every node. For every node, heights
of left and right sub-tree cannot differ by more than 1.
Example
Values 2, 3, 4, 5 are inserted into Binary Search Tree and in AVL Tree.
See the difference between these two Trees.

2
3
3
2

4
5
5
Binary Search Tree

A V L Tree

Important points regarding AVL Tree


An AVL tree is a binary search tree with a balance condition such that for
any node in the tree, the height of the Left and Right sub-trees can differ
by at most 1.
Balance factor is associated with every node of an AVL tree, which assure
the balanced property of the tree.
AVL tree is a special binary search tree which is always partially balanced.
AVL trees are known as height-balanced binary search trees.
AVL Trees are partially balanced but not always a Perfect Balanced Tree.
AVL tree is always a balanced tree, so depth of deepest node in AVL Tree is
always equal to log (n), where n are number of nodes in AVL Tree.
As depth/Height of the AVL tree is always O (log n), hence Search or Insert
or Remove operation always run in O (log n).

Worst case running time of operation: Search/Insert/Remove is equal to


Height of AVL Tree and Height of AVL Tree always remain log (n).

Balance Factor

Balance factor is a variable which is associated with every node of an AVL


tree, which is used to check whether any node in the AVL have violated
balanced property of tree or not. If any node in AVL Tree have violated the
AVL-ness property then restore its property by performing a set of
manipulations on the tree (re-arrangement of some nodes).
Balance factor of a node in AVL tree is calculated as:
Balance factor = height (Left sub-tree) - height (Right sub-tree)
OR
Balance factor = height (Right sub-tree) - height (Left sub-tree)

We will take first one, so dont confuse with this if you find second one is used
in other study resources.
If balance factor 2 or balance factor 2 then tree is un-balanced, we must
restore it by performing a set of manipulation (called rotation) on the tree.
How to calculate Balance Factor, let us see with the help of examples,
Height of tree having only one node = 0, Height of empty node = -1
BF = 2 2 = 0

BF = 1 2 = -1

5
BF = 1-(-1) = 2

BF = 0-(-1)=1

6
BF = 0-(-1)=1

BF = 0-(-1)=1

BF = -1-(-1) =0

BF = -1-(1) = -2

BF = -1-(1) = -2

BF = -1-(0) =-1

6
BF = -1-(-1) =0

BF = -1-(-1) =0

BF = -1-(-1) =0

Which of the below given is/are AVL Trees?

Exercise

1)

2)

15

34
6

3)

28

3
6

30

4)

5
3

63

6)

50

10

38
26

84
64

59

90

70

15
12

78
60

64

40

20

80

62

5)

75

6
4

40

22

50

62

1) Not an AVL Tree

2) AVL Tree

3) AVL Tree

4) Not an AVL Tree

5) Not an AVL Tree

6) Not an AVL Tree

You might also like