How to generate maximally unbalanced AVL trees
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
An AVL tree is never allowed to become wildly skewed, but some valid AVL trees are still more lopsided than others. A maximally unbalanced AVL tree means a legal AVL tree of a given height that uses the minimum number of nodes, which makes the shape as stretched as the AVL rule allows.
The Shape of the Most Skewed Valid AVL Tree
For a tree to be AVL-valid, every node must have left and right subtree heights differing by at most one. If you want the most lopsided shape at height h, one subtree should have height h - 1 and the other should have height h - 2.
That leads to the classic recurrence for the minimum number of nodes in an AVL tree:
N(h) = 1 + N(h - 1) + N(h - 2)
with base cases:
- '
N(0) = 1' - '
N(1) = 2'
This is the key insight. A maximally unbalanced AVL tree is built by recursively attaching:
- a minimal AVL tree of height
h - 1 - a minimal AVL tree of height
h - 2
The tree is still balanced, but only barely.
Recursive Construction
The easiest way to generate the shape is to construct only the tree heights first, then assign keys in sorted order so the result is also a valid binary search tree.
Here is a Python example that builds a minimal-node AVL tree for a requested height:
This code gives the left subtree the larger height. If you want the mirrored version, swap the recursive calls for the left and right child.
Why This Produces Maximum AVL Imbalance
Suppose the root has height h. Any AVL-valid pair of subtree heights must be one of the following:
- '
h - 1andh - 1' - '
h - 1andh - 2'
The pair h - 1 and h - 2 is the most skewed legal choice. If you also want the fewest nodes for that height, each subtree must itself be a minimal AVL tree. That is why the recurrence works at every level, not just the root.
You can also compute the minimal node counts directly:
Typical output is:
Those counts grow in a Fibonacci-like way because each height depends on the previous two heights.
Assigning Keys Correctly
If you only care about shape, you can store placeholder values. If you need a true AVL search tree, assign keys in in-order sequence. The earlier Python example does that by:
- building the left subtree
- placing the current key at the root
- building the right subtree
That guarantees:
- every left key is smaller than the root key
- every right key is larger than the root key
So the result is both an AVL tree and a binary search tree.
Common Pitfalls
One common mistake is to keep extending only one side of the tree. That quickly breaks the AVL rule because subtree heights start differing by more than one. A valid maximally unbalanced AVL tree is not arbitrary skew; it is carefully skewed.
Another mistake is using the wrong base cases. If you define height differently, your recurrence can still be right in spirit but wrong in counts. Be consistent about whether an empty tree has height -1 or 0.
A third issue is inserting sequential keys into a normal AVL implementation and expecting the final shape to match the minimal-node construction exactly. AVL insertions preserve balance, but the resulting shape depends on rotation history. If you want the specific maximally unbalanced form, construct it directly.
Finally, people often confuse "maximally unbalanced AVL" with "worst-case AVL after arbitrary insertions." The minimal-node tree is the theoretical extreme for a chosen height, not the only possible sparse AVL tree.
Summary
- A maximally unbalanced AVL tree is the minimal-node AVL tree for a given height.
- Its subtree heights are recursively
h - 1andh - 2. - The node-count recurrence is
N(h) = 1 + N(h - 1) + N(h - 2). - Build the shape recursively, then assign keys in-order if you need a search tree.
- Do not confuse a legal AVL extreme with an arbitrarily skewed binary tree.
Related reading
- How to generate n different colors for any natural number n?
- how to generate Narcissistic numbers faster?
- How to generate random graphs?
- How to generate random numbers biased towards one value in a range?
- How to generate the power-set of a given List?
- How to get a colorbar in networkx.draw_networkx?
- How to generate Sudoku boards with unique solutions
- How to get a specific sequence like this?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.