Professional Documents
Culture Documents
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.
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.
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
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.
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
Balance Factor
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
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
2) AVL Tree
3) AVL Tree