Visualizations
265 / 500

265 · Math

Catalan Bijections

One Catalan object drawn five ways at once, linked piece by piece as it morphs.

A binary tree with n nodes is redrawn live as a triangulation of an (n+2)-gon, non-crossing chords on a circle, a Dyck path and a balanced parentheses word, each read off the tree by a classical bijection (a node is one matched pair, one chord and one triangle), so every node keeps its color in every view. The tour visits all C(n) objects along a Hamiltonian path of the rotation graph found by depth-first search with Warnsdorff's rule, so each step is a single tree rotation, which is the same thing as a single diagonal flip, and only the pieces that really change move. The fifth panel is the associahedron in Loday's coordinates, reduced to three principal axes and spinning: every object is a vertex, every flip an edge, visited vertices stay lit, the latest steps of the tour trail behind like a comet, and dashed edges show the flips available from where you are.

Try it. Hover any piece to light up its counterparts in every view. Click a piece to rotate that node above its parent (flipping its diagonal), click a vertex of the associahedron to walk there by the shortest flip sequence, or click empty space to step. Change n from 1 to 7 with the buttons or the up and down arrows; left and right step, Space pauses and S shuffles.

  • Catalan bijections
  • Tree rotations and diagonal flips
  • Hamiltonian path search
  • Loday associahedron with PCA projection

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build an interactive "Catalan numbers" explorer with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window. Use a light paper background and dark ink.
- Generate every balanced parentheses word with n pairs (start with n = 4, which gives 14 words) using a small recursive function.
- Turn the current word into a binary tree with the rule word = "(" left ")" right, giving each node an id, and give every id its own color.
- Draw two linked views side by side: the tree (x from in-order position, y from depth) and the Dyck path (each "(" is a step up, each ")" a step down), with the parentheses printed under the path, one character per step. Color each node, its matching up and down steps and its two parentheses the same.
- Press the arrow keys to move to the next or previous word.

Once that works, make it beautiful:
- Add a third view: the triangulation of an (n+2)-gon. The root is the triangle on the bottom edge with its apex at 1 + size(left); recurse into the two smaller polygons. Fill each triangle with its node's color.
- Hit test the pointer in every view, and when it is over a piece, dim everything except that piece and its counterparts.
- Animate between words: interpolate node positions, step positions and triangle corners with an ease-in-out curve over about 0.8 seconds.

Explain the bijections in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as adding non-crossing chords on a circle, ordering the words so that each step is a single tree rotation (one diagonal flip), or drawing the associahedron whose vertices are all the objects.
PreviousCooperationA few defectors in a sea of cooperators unfold into an endless symmetric kaleidoscope. NextSky MachineDrag the sun and watch real Rayleigh and Mie scattering paint the sky.

Related visualizations

  • Penrose PentagridMath Five grids of parallel lines dualize into a living Penrose tiling of stained glass.
  • Tessellation MorphMath Drag one edge of an Escher-style creature and every copy reshapes while still tiling.
  • Dissection ProofsMath Proofs without words in cut paper, plus any polygon you draw cut into any other.
  • TonnetzSound Euler's lattice of tones, where chords flip like cards and the plane folds into a torus.
  • MCTS BloomAlgorithms Four-in-a-row against Monte Carlo Tree Search, its search tree blooming as a flower.
  • Four-Dimensional PolytopesMath All six regular 4D polytopes, double-rotating through themselves in stereographic 3D.
  • Tutte EmbeddingAlgorithms A hopeless tangle of edges relaxes into a crossing-free drawing, as Tutte proved it must.
  • Dancing LinksAlgorithms Knuth's Algorithm X tiles boards with wooden pentominoes while its links visibly dance.
  • Balancing MobileAlgorithms A red-black or AVL tree hangs as a spring-physics mobile while a plain BST droops.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs