AVL Tree - Notes
AVL Tree - Notes
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of
left and right subtrees cannot be more than one for all nodes.
A self-balancing tree is a binary search tree that balances the height after insertion and
deletion according to some balancing rules.
The balance factor of node N is height(right(N)) – height(left(N)). In an AVL Tree, the balance
factor of a node could be only one of 1, 0, or -1 values.
The AVL Tree checks the balance factor of its nodes after the insertion or deletion of a node. If
the balance factor of a node is greater than one or less than -1, the tree rebalances itself.
There are four operations to rebalance a tree:
Right Rotation
Left Rotation
Left-Right Rotation
Right-Left Rotation
Implementation:
class AVLNode
{
public AVLNode()
{
left = null;
right = null;
data = 0;
height = 0;
}
public AVLNode(int n)
{
left = null;
right = null;
data = n;
height = 0;
}
}
class AVLTree
{
private AVLNode root;
public AVLTree()
{
root = null;
}
import java.util.Scanner;