惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
博客园 - Franky
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
爱范儿
爱范儿
罗磊的独立博客
博客园_首页
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
V
Visual Studio Blog
T
Tailwind CSS Blog

Hacker News

GitHub - SeanFDZ/macmind: Single-layer transformer in HyperTalk for the classic Macintosh Show HN: Agent-cache – Multi-tier LLM/tool/session caching for Valkey and Redis Bonsai 1-bit WebGPU - a Hugging Face Space by webml-community Moving a large-scale metrics pipeline from StatsD to OpenTelemetry / Prometheus GitHub - Nightmare-Eclipse/RedSun: The Red Sun vulnerability repository GitHub - SethPyle376/hiraeth: Local AWS emulator focused on fast integration testing, with SQS support, SQLite-backed state, and a debug-friendly web UI. GitHub - macOS26/Agent: Any AI, replaces Claude Code, Cursor, OpenClaw. Over 18 LLM providers (Claude, OpenAI, Gemini, Ollama, Zai, HF, Qwen) wired into a native Mac app that writes code, builds Xcode projects, bumps versions, manages git, automates Safari, use AppleScript, JS or Accessibility, extend Agent! w/ MCP Servers, run tasks from your iPhone via Messages. YouTube now lets you turn off Shorts I Made a Terminal Pager Burgers | マクドナルド公式 Commands — HackerNews CLI documentation ChatGPT for Excel PiCore - Raspberry Pi Port of Tiny Core Linux Live Nation illegally monopolized ticketing market, jury finds Google Broke Its Promise to Me. Now ICE Has My Data. Founding Engineer at Adaptional | Y Combinator CRISPR takes important step toward silencing Down syndrome’s extra chromosome GitHub - saffron-health/libretto: The AI toolkit for building reliable browser automations US v. Heppner (S.D.N.Y. 2026) no attorney-client privilege for AI chats [pdf] Retrofitting JIT Compilers into C Interpreters IPv6 – Google The Accursèd Alphabetical Clock Cybersecurity Looks Like Proof of Work Now Fragments: April 14 Cal.com Goes Closed Source: Why AI Security Is Forcing Our Decision | Cal.com - Scheduling Software for Online Bookings Laravel raised money and now injects ads directly into your agent When moving fast, talking is the first thing to break Too much Discussion of the XOR swap trick – Heather Cafe Introduction to Spherical Harmonics for Graphics Programmers The Grand Line
Rigorous Nonsense - Easy Random Trees
2026-05-06 · via Hacker News

Posted on February 27, 2026 by Brandon Wilson

Can you think of a way to efficiently generate a random plane tree?

Richard P. Stanley in his book Catalan Numbers has a really nifty combinatorial proof of why Catalan numbers have the formula

\[ C_n = {1 \over n+1}{2n \choose n} \]

The standard proof uses generating functions applied to an inductive definition of the Catalan numbers, which frankly does little to illumiate their connection with combinatorial objects, despite the fact that the appearance of a binomial coefficient gives a hint that it’s counting something, and the division suggests we might be looking at some kind of equivalence class.

Stanley’s proof is so direct and beautiful, I cannot help but share. However, let us instantiate the proof as a little program that generates a random tree with \(n\) nodes:

D←{n←⍵-1                 ⍝ The ⍵-1st Catalan number counts number of ⍵-node trees
 x←x[?⍨≢x←1⍪(n⍴1)⍪n⍴¯1]  ⍝ Random step vector of length 1+2×⍵
 d←0⍪¯1↓+⍀x              ⍝ Depth vector
 i←⊃⌽⍸(⌊⍀d)=d            ⍝ Righmost lowest node
 x⌽⍨←i                   ⍝ Strict ballot sequence
 d←¯1+(x=1)⌿+⍀x          ⍝ Corresponding tree node depths
}

At center stage here is the isomorphism between plane trees and strict ballot sequences, which is directly connected via the depth vector tree representation, which I’ll assume is already familiar to readers here. The image to have in mind is a depth-first search traversal pattern.

Depth-first search tree traversal

To construct a depth vector, we simply write down the current depth every time we first visit a node.

We can construct a related sequence by starting at the root and writing down a \(1\), then writing down a \(1\) every time we descend to a node, and finally, writing a \(¯1\) every time we ascend. Notice, in particular, that we both descend to and ascend from every node exactly once, except for the root. This guarantees that the partial sums are at least \(1\) and the total sum is exactly \(1\). Such a vector of \(1\)’s and \(¯1\)’s is called a strict ballot sequence, and given one, it’s straighforward enough to convert it into the depth vector of the corresponding tree:

      +⍀x←1 1 ¯1 1 1 1 ¯1 ¯1 ¯1 1 ¯1 1 1 ¯1 ¯1 1 1 ¯1 ¯1  ⍝ Strict ballot sequences
1 2 1 2 3 4 3 2 1 2 1 2 3 2 1 2 3 2 1                     ⍝  have positive partial sums

      ⊢d←¯1+(x=1)⌿+⍀x                                     ⍝ Depth vectors ignore the ¯1's
0 1 1 2 3 1 1 2 1 2

Now let’s think about random sequences of \(1\)’s and \(¯1\)’s. To have any hope of being a strict ballot sequence, the sum must be \(1\), so we know a priori that there must be one more \(1\) than \(¯1\)’s. We can parameterize this as seqeuences of length \(2n+1\), with \(n+1\) ones and \(n\) negative ones.

      ⊢x←x[?⍨≢x←1⍪(n⍴1)⍪n⍴¯1]  ⍝ Compare with D above
¯1 ¯1 ¯1 1 ¯1 ¯1 ¯1 1 ¯1 ¯1 1 1 1 1 ¯1 1 1 1 1 1 ¯1

Most likely, such a random vector will not be a strict ballot sequence, since partial sums can drop below \(1\). However, consider the righmost lowest partial sum, \(N\), at index \(i\):

      i←⍸msk←s=N←⌊⌿s←+⍀x
      s⍪⍉⍪msk
¯1 ¯2 ¯3 ¯2 ¯3 ¯4 ¯5 ¯4 ¯5 ¯6 ¯5 ¯4 ¯3 ¯2 ¯3 ¯2 ¯1 0 1 2 1
 0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0 0 0 0 0

Looking at the tail sequence starting at \(s_i\), we know it must end in \(1\). That means \(s_i\) is \(N+1\) below the tail. However, everything before \(s_i\) is at most \(N\) below the start. In other words, if we transplant the tail \(x_i ... x_{2n}\) to the start and recompute partial sums, they will remain positive. Said another way, rotating the ballot sequence by \(i\) produces a strict ballot sequence. Or more consicely,

 d←0⍪¯1↓+⍀x    ⍝ Depth vector
 i←⊃⌽⍸(⌊⍀d)=d  ⍝ Righmost lowest node
 x⌽⍨←i         ⍝ Strict ballot sequence

Combining with our conversion to a depth vector finishes the impelemntation of D.

Better yet, the Catalan number formula falls right out of the above by noting that there are exactly \(2n+1 \choose n\) strings of \(1\)’s and \(¯1\)’s and that since \(n\) and \(2n+1\) are coprime, no rotations produce equal sequences. This means that the sequence length, \(2n+1\), must divide \(2n+1 \choose n\). Thus the number of strict ballot sequences with length \(2n+1\) is

\[ {1 \over 2n+1}{2n+1 \choose n} \]

It is a small matter of algebra to check that this equals the value of \(C_n\) above. For \(n = 0\), we have a strict ballot sequence of length \(1\), which corresponds to a tree of just a since root node as per the isomorphism outlined above. This means that \(C_n\) must count plane trees with \(n+1\) nodes.

Stanley’s book is delightful and worth perusing. The above just uses one isomorphism, but the book contains a veritable cornucopia and is replete with nicely illustrative diagrams to boot. Highly recommended!