Binary Search Tree Generator
A list of values, inserted one at a time into a binary search tree, and everything that follows from the order they arrived in. The shape is entirely determined by that order — the same values sorted would give a chain of height equal to their count, which is a linked list wearing a tree's name. The in-order traversal is the invariant that does not care: whatever the shape, it comes out sorted, and that is what is checked here before anything is shown.
What this generator does
Draws distinct values, inserts them into an empty tree in that order, and reports the height, all three traversals, and whether every node's subtrees are within one level of each other.
How to use this tool
- Choose how many values to insert.
- Work out the tree the insertion order produces.
- Reveal the answer for the height and all three traversals.
- Seed it if you want the same tree again.
Understanding the controls
- How many values to insert
- Between 3 and 15. Fifteen is the point where a hand-drawn tree stops fitting comfortably on a page.
- Seed
- The same word reproduces the same insertion order, which is what makes this usable as a test fixture.
- Show the answer
- Reveals the height, the balance verdict and all three traversals in full.
Common use cases
- Worked examples for teaching insertion and traversal
- Test cases for a tree implementation, with the expected traversals given
- Showing why insertion order matters and sorted input is the worst case
- Practice at reading pre-order and post-order, which are easy to confuse
- Producing the same tree for two people from one seed
How this generator works
Insertion follows the search property: smaller goes left, larger goes right, until an empty place is found. The check then confirms the property holds at every node against its own value bounds, and that the in-order traversal is exactly the sorted input — the invariant that would catch a tree built wrongly even when it looks plausible.
Randomness and fairness
The values and their order are random; the traversals and the height follow from them. Seeded trees reproduce exactly and are therefore explicitly not cryptographically secure. Without a seed the browser's cryptographically secure generator supplies the values.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- Values are distinct, so the question of how to handle duplicates does not arise.
- No self-balancing — this is a plain binary search tree, not an AVL or red-black tree.
- The tree is described by its traversals rather than drawn as a diagram.
- Deletion, the genuinely tricky operation, is not covered.
- Trees are not stored between visits; seed one you want to reuse.
Privacy and your data
Values and traversals are computed in your browser. Nothing about the tree or your seed is transmitted or kept.
Related generators
- Sorting Algorithm Trace GeneratorAn array, an algorithm, and the state after every pass, with the comparison and move counts that distinguish the three.
- Binary Heap GeneratorMin or max heaps built one insertion at a time, shown level by level, and drained to prove they sort.
- Balanced Bracket GeneratorEvery balanced bracket sequence of a given size, counted against the Catalan numbers and each checked by the running-depth rule.
- Decision Tree GeneratorBuilds a complete tree of every path through a series of decisions, so you can see how quickly the options multiply.
- Random Tree GeneratorRandom trees with exactly one path between any two nodes — n nodes, n minus 1 edges, no cycles, guaranteed connected.
- DAG GeneratorDirected acyclic graphs where the absence of cycles is structural rather than checked afterwards — for testing schedulers, build systems and topological sorts.