AVL Trees Explained: How Rotations Keep BST Operations O(log n) (opens in new tab)
You learn binary search trees and walk away believing every operation is O(log n). It isn't. That guarantee only holds when the tree stays balanced, and a plain BST has no mechanism to enforce that. Insert [1, 2, 3, 4, 5] into an empty BST and you've built a linked list with extra steps. TL;DR: An AVL tree adds one rule to a BST. No node's left and right subtrees can differ in height by more than 1. When an insertion breaks the rule, the tree fixes itself through rotations. Every operation st...
Read the original article