Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Balanced tree

Сбалансированное дерево

A tree is balanced when its structure keeps branches from growing too long as it gains nodes. In a balanced search tree, this limits how many keys a lookup must check, even in the worst case.

Also known as: AVL, красно-чёрное дерево, red-black tree

Why height matters

Imagine storing usernames in a binary search tree. At each node, you compare the name you want with the current key and go left for a smaller key or right for a larger one. You only follow one path, but that path could still be long. Insert names in alphabetical order into a plain binary search tree and every new node becomes the right child of the previous one. Finding the last name then takes O(n) comparisons.

A balanced search tree uses rules that keep its height, the length of the longest root-to-leaf path, at O(log n). For AVL and red-black trees, this gives worst-case O(log n) lookup, insertion, and deletion for n stored keys. The tree doesn't have to be symmetric, and some leaves can be deeper than others.

Two ways to maintain balance

AVL trees enforce a local height rule: at every node, the left and right subtree heights differ by at most one. After an update, the tree checks affected ancestors and uses single or double rotations to repair violations. A rotation rearranges a few links without changing the sorted order of the keys.

Red-black trees enforce a different set of rules using a red or black label on each node. For example, a red node can't have a red child, and all paths from a given node to the empty child positions below it contain the same number of black nodes. Together, these rules limit the tree's height. Updates can trigger recoloring, rotations, or both. AVL and red-black trees are examples of balanced trees, not alternate names for the whole category.

Example: one rotation

Inserting 10, 20, then 30 produces a right-leaning chain before AVL rebalancing. A left rotation at 10 makes 20 the root. Reading the keys in sorted order still yields 10, 20, 30.

10                20
  \              /  \
   20    →      10   30
     \
      30

When to use one

Choose an ordered tree when you need more than exact matches. Java's TreeMap, for example, uses a red-black tree and can find the next key after a given value or iterate over a range in sorted order. A hash table is often a better fit for exact lookups alone, but it doesn't provide these ordering operations directly. Database indexes often use balanced trees from the B-tree family: a node can have many children, so an index lookup can reach its target in fewer page reads.

Interview check

If you're asked whether a binary tree is balanced, clarify the rule before coding. Many interview problems mean the AVL-style height condition, even when the tree isn't a search tree. Check the condition at every node, not just the root. A single bottom-up traversal can calculate subtree heights and check balance in O(n) time; recomputing heights from scratch at each node can revisit the same nodes many times.