An AVL tree is another balanced binary search tree.
DEFINITION: An AVL Tree is a height-balanced binary search tree.
DEFINITION: The balance factor of a binary tree is the difference in heights of its two subtrees (hR - hL).
The balance factor (bf) of a height balanced binary tree may take on one of the values -1, 0, +1.
2. Content
➔ About AVL Tree
➔ Operation Performed on AVL Tree
◆ Searching
◆ Traverse
◆ Insertion
◆ Deletion
➔ AVL Rotation
◆ Left Rotation
◆ Right Rotation
◆ Left-Right Rotation
◆ Right-Left Rotation
3. About AVL tree
➢ An AVL tree is a self-balancing binary search tree. It was the first
such data structure to be invented. In an AVL tree, the heights of
the two child subtrees of any node differ by at most one; if at any
time they differ by more than one, rebalancing is done to restore
this property.
➢ The AVL tree is named after its two Soviet inventors, Georgy
Adelson-Velsky and Evgenii Landis, who published it in their 1962
paper "An algorithm for the organization of information".
4. Operations
Searching
Searching for a specific key in an AVL tree can be done the same way as
that of a normal unbalanced binary search tree. The number of
comparisons required for successful search is limited by the height h and
for unsuccessful search is very close to h, so both are in O(log n).
Traversal
Traversal term refers to the process of visiting (checking and/or
updating) each node in a tree data structure exactly once.
5. Operation
Insertion
When inserting an element into an AVL tree, you initially follow the same process as
inserting into a Binary Search Tree. More explicitly: In case a preceding search has
not been successful the search routine returns the tree itself with indication
EMPTY and the new node is inserted as root. Or, if the tree has not been empty the
search routine returns a node and a direction (left or right) where the returned
node does not have a child. Then the node to be inserted is made child of the
returned node at the returned direction.
Deletion
The preliminary steps for deleting a node are described in section Binary search
tree. There, the effective deletion of the subject node or the replacement node
decreases the height of the corresponding child tree either from 1 to 0 or from 2
to 1, if that node had a child.
6. 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.
As depicted, the unbalanced node becomes the right child of its left child by
performing a right rotation.
7. AVL Rotation
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.
8. 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 −
In our example, node A has become unbalanced as a node is inserted in the right
subtree of A's right subtree. We perform the left rotation by making A the
left-subtree of B.
9. 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.
10. Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a combination of
right rotation followed by left rotation.