Random Tree Generator
A tree is the tidiest graph there is: every node reachable, exactly one path between any pair, and no cycles anywhere. That makes it the right test input for hierarchy code, file-system models, org structures and any traversal you want to reason about without worrying about revisiting nodes. Each tree here is built by a random attachment walk, so the shapes vary from long chains to wide stars.
What this generator does
Builds a random tree by walking the nodes in a shuffled order and attaching each to one of the nodes already placed. That produces a uniformly varied set of shapes and guarantees connectivity in exactly n-1 edges. The result is re-checked: the edge count must be n-1 and the graph must be connected, or it is not a tree.
How to use this tool
- Choose how many nodes.
- Generate — you always get exactly one edge fewer than you have nodes.
- Copy the edge list or DOT.
- Seed it to reproduce a particular shape.
Understanding the controls
- How many nodes
- Between 2 and 60. The edge count follows automatically — a tree has no choice about it.
- Weighted edges
- Whole-number weights, which turn the tree into input for a weighted path exercise.
- Seed
- Any word reproduces the same result exactly. Leave it empty and the browser's cryptographic generator is used instead.
Common use cases
- Testing traversal code against shapes you did not choose by hand
- Generating a hierarchy for a UI component to render
- Producing org-structure or category-tree sample data
- Showing why a tree needs no visited set while a general graph does
- Building input for a shortest-path exercise where the answer is unique
How this generator works
Each node after the first is attached to a uniformly chosen node already in the tree. This cannot create a cycle, because every new edge connects a new node to the existing structure, and it cannot leave anything unreachable, because every node is attached exactly once.
Randomness and fairness
Each node attaches to a uniformly chosen node already placed, so the tree shape is random while the edge count never is. Seeded draws are reproducible and so not cryptographically secure.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- Trees here are undirected. Root it yourself if you need a hierarchy with a direction.
- The attachment method favours slightly deeper shapes than a uniform draw over all labelled trees would.
- No tree is kept between visits — seed it if a fixture depends on the exact shape.
Privacy and your data
Trees are generated in your browser. Nothing you set or copy leaves the page or persists between visits.
Related generators
- Binary Search Tree GeneratorAn insertion order and the tree it builds, with all three traversals, the height, and whether it came out balanced.
- Graph Traversal GeneratorThe same graph visited breadth-first and depth-first side by side, with the hop count each node sits at.
- Random Graph GeneratorRandom graphs at a density you choose, drawn and exported as an edge list or DOT, with connectivity measured rather than assumed.
- DAG GeneratorDirected acyclic graphs where the absence of cycles is structural rather than checked afterwards — for testing schedulers, build systems and topological sorts.
- Task Allocation GeneratorShare tasks out across a team with real rules — must do, must not do, capacity limits — and a clear answer when the rules cannot all hold.