GenerateRandomSearch

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

  1. Choose how many nodes.
  2. Generate — you always get exactly one edge fewer than you have nodes.
  3. Copy the edge list or DOT.
  4. 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.