AVL Trees
AVL Trees
AVL Trees
Tree is one of the most important data structure that is used for efficiently
performing operations like insertion, deletion and searching of values.
However, while working with a large volume of data, construction of a
well-balanced
balanced tree for sorting all d
data is
s not feasible. Thus only useful data
is stored as a tree, and the actual volume of data being used continually
changes through the insertion of new data and deletion of existing data.
You will find in some cases where the NULL link to a binary tree to special
links is called as threads and hence it is possible to perform traversals,
insertions, deletions without using either stack or recursion. In this
chapter, you will learn about the Height balance tree which is also known
as the AVL tree.
AVL tree is a binary search tree in which the difference of heights of left
and right subtrees of any node is less than or equal to one. The technique
of balancing the height of binary trees was developed by Adelson, Velsky,
Velsk
and Landis and henc
hencee given the short form as AVL tree or Balanced
Binary Tree.
Consider the following trees. Here we see that the first tree is balanced
and the next two trees are not balanced −
In the second tree, the left subtree of C has height 2 and the right subtree
has height 0, so the difference is 2. In the third tree, the right subtree
of A has height 2 and the left is missing, so it is 0, and the difference
diffe is 2
again. AVL tree permits difference (balance factor) to be only 1.
BalanceFactor = height(left
height(left-subtree) − height(right-subtree)
AVL Rotations
To balance itself, an AVL tree may perform the following four kinds of
rotations −
Left rotation
Right rotation
Left-Right rotation
Right-Left rotation
The first two rotations are single rotations and the next two rotations are
double rotations. To have an unbalanced tree, we at least need a tree of
height 2. With this simple tree, let's understand them one by one.
Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right
subtree of the right subtree, then we perform a single left rotation −
Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree
of the left subtree. The tree then needs a right rotation.
Left-Right Rotation
Double rotations are slightly complex version of already explained
versions of rotations. To understand them better, we should take note of
each action performed while rotation. Let's first check how to perform
Left-Right rotation. A left-right rotation is a combination of left rotation
followed by right rotation.
State Action
A node has been inserted into the right subtree of the left
subtree. This makes C an unbalanced node. These scenarios
cause AVL tree to perform left-right rotation.
Right-Left Rotation
A node has been inserted into the left subtree of the right
subtree. This makes A, an unbalanced node with balance
factor 2.
Let T be a non-empty binary tree with TL and TR as its left and right subtrees.
The tree is height balanced if:
The Balance factor of a node in a binary tree can have value 1, -1, 0, depending
on whether the height of its left subtree is greater, less than or equal to the
height of the right subtree.
Since AVL trees are height balance trees, operations like insertion and deletion
have low time complexity. Let us consider an example:
If you have the following tree having keys 1, 2, 3, 4, 5, 6, 7 and then the binary
search tree will be like the second figure:
Struct AVLNode
{
int data;
struct AVLNode *left, *right;
int balfactor;
};
Algorithm
lgorithm for Insertion
Step 1: First, insert a new element into the tree using BST's (Binary Search
Tree) insertion logic.
Step 2: After inserting the elements you have to check the Balance Factor of
each node.
Step 3: When the Balance Factor of every node will be found like 0 or 1 or -1
then the algorithm will proceed for the next operation.
Step 4: When the balance factor of any node comes other than the above
three values then the tree is said to be imbalanced. Then perform
the suitable Rotation to make it balanced and then the algorithm
will proceed for the next operation.
Insert (14)
Insert (8)
Insert (12)
Insert (46)
Insert (23)
Insert (77)
Insert (88)
Insert (20)
Insert (1)
Insert (2)
Insert (3)
Insert (4)
Insert (8)
Insert (7)
Insert (5)
Insert (11)
Insert (10)
Insert 7
The
he steps of this algorithm are :
1. Use general BST deletion algorithm to delete given key from the
AVL tree.
2. After deletion, update the height of the current node of the AVL
tree.
3a. If 'balance' > 1 then that means the height of the left sub-tree sub is
greater than the height of the right sub sub-tree.
tree. This indicates left-left
left or left-right
right case. To confirm if this is left left-left
left or left-right
left
case, we check the balance of left sub sub-tree of current
rent node. If it
greater than 0 then that confirms left left-left
left case, if it less than 0
then that confirms leftleft-right
right case. If it is equal to 0, then this we
can either consider this as a left left-left
left case or as a left-right
left case.
In this implementation, we cons consider this as left-left left case for
efficiency. For left--left
left case, we do a right rotation at the current
node. For the left--right
right case, we do a left rotation at left child of
current node followed by a right rotation at the current node
itself.
3b. If 'balance' < -11 then that means the height of the right sub-tree
sub
is greater than the height of the left sub sub-tree.
tree. This indicates
right-right
right or right
right-left
left case. In this case, if balance of right sub-
sub
tree of current node is less than 0 then this confirms right-right
right
case, if it is greater than 0 then this confirms right
right-left
left case. If it
Example walk-through: Let's delete key sequence [6,5,4] from the below AVL tree
and see how the height balance is maintained throughout.
As you can confirm this tree satisfies height balance property for AVL tree.
5) What is an AVL search tree? How do we define the height of it? Explain
about the balance factor associated with a node of an AVL tree.
6) Construct AVL tree for the following numbers 14, 8, 12, 46, 23, 5, 77, 88, 20
7)) Insert the following sequence of elements into an AVL tree, starting with an
empty tree 10, 20, 15, 25, 30, 16, 18, 19
Insert(10) insert(20)
Insert(15)
Insert(25)
Insert(30)
Insert(18)
Insert(19)
Delete(30)
9) Define balanced binary search tree. Construct binary search tree for the data
8, 10, 3, 2, 1, 5, 4, 6, Insert an element 7 into binary search tree using AVL
rotation.
11)) Create an AVL tree using the following elements as a sequence set. Show
the balance factor in the resulting tree 13, 22, 6, 9, 32, 55, 79, 65, 70
12)) Insert 42, 43, 46 and 49 in the above constructed AVL tree and show a
balanced AVL tree
15) Compare the worst case height of a red-black tree with n nodes and the
worst case height of an AVL tree with same number of nodes
16)) In an initially empty AVL tree insert the following Keys: DEC, JAN, APR,
MAR, JUL, AUG, OCT, FEB, NOV. Draw AVL tree after every insertion and
apply rotations where ever nece
necessary
17)) With suitable examples, explain different rotations associated with AVL tree
insertion.
18)) Derive an expression for maximum height of an AVL tree with n nodes.
The worst case possible height of AVL tree with n nodes is 1.44*log n.
This can be verified using AVL tree having 7 nodes and maximum height.