%3C/text%3E%3C/svg%3E">
💻
💻 Tech & IT

Tree Height Calculator (BST, AVL, red-black, B-tree)

Results update as you type.

Your numbers

Understanding the tree height calculator — concept and principle

The height of a search tree decides how many nodes a lookup must visit, and therefore how fast search, insert and delete are. This calculator compares, for n keys, the best possible height of a binary tree with the worst-case heights guaranteed by AVL trees, red-black trees and B-trees. Heights here count levels of nodes on the longest root-to-leaf path.

Tree heights for a million keysA perfect binary tree needs 20 levels, AVL at most 28.4, red-black at most 39.9, and a B-tree of order 256 only 3.Levels needed for 1,000,000 keys20Perfect≤ 28.4AVL max≤ 39.9Red-black3B-tree256children1 bar = 1 level ≈ 1 step (or disk read)
Wide B-tree nodes cut a million keys down to 3 levels; balanced binary trees need 20 to 40.

How it is calculated

The principle

Balanced trees guarantee a height of O(log n) by limiting how lopsided they can become. In an AVL tree the two subtrees of every node differ in height by at most one; the sparsest such trees are Fibonacci trees, and since Fibonacci numbers grow like the golden ratio φ ≈ 1.618, the height is at most log_φ n ≈ 1.44 log₂ n. A red-black tree requires the same number of black nodes on every path and forbids two reds in a row, so the longest path is at most twice the shortest, giving 2 log₂(n + 1). A B-tree node holds many keys and has at least t children, so the tree is very flat; databases use it because each level is roughly one page read.

Worked example

  1. Inputs: n = 1,000,000 keys, B-tree order 256.
  2. Perfect binary tree: log₂ 1,000,001 ≈ 19.93, rounded up to 20 levels.
  3. AVL: 1.44 × log₂ 1,000,002 − 0.328 = 1.44 × 19.9316 − 0.328 ≈ 28.37346289.
  4. Red-black: 2 × 19.9316 ≈ 39.86314002.
  5. B-tree: t = 128; log₁₂₈(500,000.5) ≈ 2.70, ⌊2.70⌋ + 1 = 3 levels.
How Perfect binary tree min height changes with Keys nHow Perfect binary tree min height chang…18192021250,000500,000750,0001e+061.25e+061.5e+062e+06Example: 20Keys n
Each point is the calculator re-run with only “Keys n” changed; the gold point is the default example. Change the inputs above and this chart follows your numbers.
Keys nPerfect binary tree min height
250,00018
500,00019
750,00020
1e+0620
1.25e+0621
1.5e+0621
2e+0621

How to read the result

A balanced binary tree with a million keys needs fewer than 40 comparisons even in the worst case, while a plain BST built from sorted input degrades into a 1,000,000-level list. A B-tree of order 256 reaches any of a million keys in at most 3 node reads.

Common mistakes

Keep in mind: These are theoretical bounds. Real B+-tree indexes have variable key sizes and fill factors, so measure actual depth in your database.

Frequently asked questions

Why not always use a perfectly balanced tree?

Keeping it perfect after every insert would require costly rebuilding; AVL and red-black trees accept a slightly taller tree for O(log n) updates.

Which is faster, AVL or red-black?

AVL trees are shorter and suit read-heavy work; red-black trees rotate less on updates.

How tall is a random BST?

With keys inserted in random order the average node depth is about 1.39 log₂ n, but the worst case is still n.

Sources: G. M. Adelson-Velsky, E. M. Landis, "An algorithm for the organization of information", 1962 · T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, MIT Press (red-black trees, B-trees) · R. Bayer, E. McCreight, "Organization and maintenance of large ordered indexes", Acta Informatica 1, 1972

Checked 8 Nov 2026. Standard formulas and RFC/IEEE definitions, computed locally in your browser — nothing you type is sent anywhere.

💻 All tech & IT tools →🌍 Calculators by country →