I ran into this problem where I wanted to generate balanced trees for test cases, and I decided to crack it with math.
Let X = the number of remaining open parenthesis required to reach your target length, and Y = the number of remaining close parenthesis.
Let P(X) = X/(X+Y) * (1 + 1/(Y-X+1)). Generate an open parenthesis with probability P(X), and otherwise a close parenthesis.
This will efficiently generate an unbiased random plane tree. The reason this works has to do with counting the valid ways to complete the plane tree, using a calculation similar to the one in the article.
As someone who has never heard of most of these concepts before (plane trees, catalan numbers, ballot sequences, depth vectors), I found the question "Can you think of a way to efficiently generate a random plane tree?" confusing, and I only understood the problem being solved by first trying to understand the solution. After reading through, it seems like it's asking about generating a random plane tree drawn from a uniform distribution of all possible plane trees with a given number of nodes? Cool idea once I understood it though!
Didn’t expect a few lines of APL plus ballot-sequence magic to make random plane trees feel this intuitive—super elegant construction. Curious how this could be applied in practice, e.g. generative graphics or random UI/tree structures?
I ran into this problem where I wanted to generate balanced trees for test cases, and I decided to crack it with math.
Let X = the number of remaining open parenthesis required to reach your target length, and Y = the number of remaining close parenthesis.
Let P(X) = X/(X+Y) * (1 + 1/(Y-X+1)). Generate an open parenthesis with probability P(X), and otherwise a close parenthesis.
This will efficiently generate an unbiased random plane tree. The reason this works has to do with counting the valid ways to complete the plane tree, using a calculation similar to the one in the article.
As someone who has never heard of most of these concepts before (plane trees, catalan numbers, ballot sequences, depth vectors), I found the question "Can you think of a way to efficiently generate a random plane tree?" confusing, and I only understood the problem being solved by first trying to understand the solution. After reading through, it seems like it's asking about generating a random plane tree drawn from a uniform distribution of all possible plane trees with a given number of nodes? Cool idea once I understood it though!
Awesome! You can simulate a universe with a binary tree and help align AI agents: https://docs.google.com/document/d/1Rqt7jQEP5QAmwEcMd-X0nsfx...
Didn’t expect a few lines of APL plus ballot-sequence magic to make random plane trees feel this intuitive—super elegant construction. Curious how this could be applied in practice, e.g. generative graphics or random UI/tree structures?