|
ADS
Algorithms and Data Structures
|
The numbers of Catalan, 1, 1, 2, 5, 14, 42, and so on. More...
Files | |
| file | ADS/numeric/Catalan.h |
| 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. | |
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.
The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.
| T ADS::catalan | ( | int | n | ) |
the number \( C_n \) of Catalan, the binary trees of \( n \) nodes
In pseudocode (the exercise on the binary trees of \( n \) nodes of Esercizi Programmazione dinamica), by the size \( k \) of the left subtree:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{catalan}( n )\quad \triangleright\ C[ i ] \text{ the binary trees of } i \text{ nodes} \cr 2 & \quad C[ 0 ] \gets 1 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do}\ C[ i ] \gets \textstyle\sum_{k=0}^{i-1} C[ k ] \, C[ i - 1 - k ]\quad \triangleright\ k \text{ nodes on the left} \cr 4 & \quad \textbf{return}\ C[ n ] \end{array}} \]