Trees, forests, etc. See: Donald E. Knuth: The Art of Computer Programming, vol 4, pre-fascicle 4A.
- data BinTree a
- leaf :: BinTree ()
- module Data.Tree
- data Paren
- = LeftParen
- | RightParen
- parenthesesToString :: [Paren] -> String
- stringToParentheses :: String -> [Paren]
- forestToNestedParentheses :: Forest a -> [Paren]
- forestToBinaryTree :: Forest a -> BinTree ()
- nestedParenthesesToForest :: [Paren] -> Maybe (Forest ())
- nestedParenthesesToForestUnsafe :: [Paren] -> Forest ()
- nestedParenthesesToBinaryTree :: [Paren] -> Maybe (BinTree ())
- nestedParenthesesToBinaryTreeUnsafe :: [Paren] -> BinTree ()
- binaryTreeToForest :: BinTree a -> Forest ()
- binaryTreeToNestedParentheses :: BinTree a -> [Paren]
- nestedParentheses :: Int -> [[Paren]]
- fasc4A_algorithm_P :: Int -> [[Paren]]
- binaryTrees :: Int -> [BinTree ()]
- countBinaryTrees :: Int -> Integer
- binaryTreesNaive :: Int -> [BinTree ()]
Types
module Data.Tree
parenthesesToString :: [Paren] -> StringSource
stringToParentheses :: String -> [Paren]Source
Bijections
forestToNestedParentheses :: Forest a -> [Paren]Source
forestToBinaryTree :: Forest a -> BinTree ()Source
binaryTreeToForest :: BinTree a -> Forest ()Source
binaryTreeToNestedParentheses :: BinTree a -> [Paren]Source
Nested parentheses
nestedParentheses :: Int -> [[Paren]]Source
Synonym for fasc4A_algorithm_P
.
fasc4A_algorithm_P :: Int -> [[Paren]]Source
Generates all sequences of nested parentheses of length 2n. Order is lexigraphic (when right parentheses are considered smaller then left ones). Based on "Algorithm P" in Knuth, but less efficient because of the "idiomatic" code.
Binary trees
binaryTrees :: Int -> [BinTree ()]Source
Generates all binary trees with n nodes.
countBinaryTrees :: Int -> IntegerSource
# = Catalan(n) = \frac { 1 } { n+1 } \binom { 2n } { n }.
This is also the counting function for forests and nested parentheses.
binaryTreesNaive :: Int -> [BinTree ()]Source
Generates all binary trees with n nodes. The naive algorithm.