|
ADS
Algorithms and Data Structures
|
The numbers of Catalan, 1, 1, 2, 5, 14, 42, and so on: \( C_n \) counts the binary trees of \( n \) nodes (so the binary search trees on \( n \) keys, the trees whose symmetric visit is \( 1 , \ldots , n \)), the strings of \( n \) pairs of balanced parentheses, the full binary trees of \( n + 1 \) leaves (the parenthesizations of a product of \( n + 1 \) factors), the triangulations of a convex polygon of \( n + 2 \) sides, and the monotone lattice paths that never cross the diagonal. More...
#include "ADS/Contract.h"#include <algorithm>#include <cstddef>#include <cstdint>#include <vector>#include "ADS/numeric/impl/Catalan.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Functions | |
| template<class T = std::uint64_t> | |
| T | ADS::catalan (int n) |
| the number \( C_n \) of Catalan, the binary trees of \( n \) nodes | |
The numbers of Catalan, 1, 1, 2, 5, 14, 42, and so on: \( C_n \) counts the binary trees of \( n \) nodes (so the binary search trees on \( n \) keys, the trees whose symmetric visit is \( 1 , \ldots , n \)), the strings of \( n \) pairs of balanced parentheses, the full binary trees of \( n + 1 \) leaves (the parenthesizations of a product of \( n + 1 \) factors), the triangulations of a convex polygon of \( n + 2 \) sides, and the monotone lattice paths that never cross the diagonal.
catalan() fills the table of the recurrence on the size of the left subtree, \( C_0 = 1 \) and \( C_n = \sum_{k=0}^{n-1} C_k C_{n-1-k} \), with \( \Theta( n^2 ) \) operations on numbers of \( \Theta( n ) \) bits, and with T a BigInteger for any \( n \) [see BigInteger.h]. The closed form \( C_n = \binom{2n}{n} / ( n + 1 ) \) and the recurrence \( C_{n+1} = 2 ( 2 n + 1 ) C_n / ( n + 2 ) \) take \( O( n ) \) operations, but their intermediate values exceed the result, so that in 64 bits they stop before the table does, which reaches \( C_{36} \); asymptotically \( C_n \sim 4^n / ( n^{3/2} \sqrt{\pi} ) \).
The contracts [see Contract.h]: \( n \ge 0 \), and \( C_n \) representable in T (\( n \le 36 \) for 64 bits unsigned). The functions are defined in Catalan.tpp.