DEV Community

Cover image for AVL Tree : Rotation, and Balance Factor Explained
Mohamed Benlmaoujoud
Mohamed Benlmaoujoud

Posted on

AVL Tree : Rotation, and Balance Factor Explained

What is an AVL Tree?

An AVL tree is a type of binary search tree. Named after it's inventors Adelson, Velskii, and Landis, AVL trees have the property of dynamic self-balancing in addition to all the other properties exhibited by binary search trees.

A BST is a data structure composed of nodes. It has the following guarantees:

  1. Each tree has a root node (at the top)
  2. The root node has zero, one, or two child nodes
  3. Each child node has zero, one, or two child nodes
  4. Each node has up to two children
  5. For each node, its left descendants are less than the
  6. current node, which is less than the right descendants
  • So The difference between the depth of right and left sub- trees cannot be more than one |depth(right) - dépt(left)|>1. This difference is called the balance factor.

So how we can balance our BST , well 💁 we can use some ruls (rotations) to balance it .

1. Left Rotation (LL Rotation)

In left rotations, every node moves one position to left from the current position.
Image description
2. Right Rotation (RR Rotation

In right rotations, every node moves one position to right from the current position.
Image description
3. Left-Right && Right-Left Rotation (LR && RL Rotation

LR or RL rotations are a combination of a single left
resp (right) rotation followed by a single right resp(left) rotation.

Application of AVL Trees
AVL trees are beneficial in cases like a database where insertions and deletions are not that frequent, but you frequently check for entries.

Top comments (1)

Collapse
 
mjmohith profile image
Mohith J

More in depth article on AVL trees can be found here: medium.com/@mohith.j/balancing-eff...