combinat-0.2.8.2: Generate and manipulate various combinatorial objects.

Safe HaskellNone
LanguageHaskell2010

Math.Combinat.Trees.Binary

Contents

Description

Binary trees, forests, etc. See: Donald E. Knuth: The Art of Computer Programming, vol 4, pre-fascicle 4A.

For example, here are all the binary trees on 4 nodes:

Synopsis

Types

data BinTree a Source #

A binary tree with leaves decorated with type a.

Constructors

Branch (BinTree a) (BinTree a) 
Leaf a 

Instances

Monad BinTree Source # 

Methods

(>>=) :: BinTree a -> (a -> BinTree b) -> BinTree b #

(>>) :: BinTree a -> BinTree b -> BinTree b #

return :: a -> BinTree a #

fail :: String -> BinTree a #

Functor BinTree Source # 

Methods

fmap :: (a -> b) -> BinTree a -> BinTree b #

(<$) :: a -> BinTree b -> BinTree a #

Applicative BinTree Source # 

Methods

pure :: a -> BinTree a #

(<*>) :: BinTree (a -> b) -> BinTree a -> BinTree b #

(*>) :: BinTree a -> BinTree b -> BinTree b #

(<*) :: BinTree a -> BinTree b -> BinTree a #

Foldable BinTree Source # 

Methods

fold :: Monoid m => BinTree m -> m #

foldMap :: Monoid m => (a -> m) -> BinTree a -> m #

foldr :: (a -> b -> b) -> b -> BinTree a -> b #

foldr' :: (a -> b -> b) -> b -> BinTree a -> b #

foldl :: (b -> a -> b) -> b -> BinTree a -> b #

foldl' :: (b -> a -> b) -> b -> BinTree a -> b #

foldr1 :: (a -> a -> a) -> BinTree a -> a #

foldl1 :: (a -> a -> a) -> BinTree a -> a #

toList :: BinTree a -> [a] #

null :: BinTree a -> Bool #

length :: BinTree a -> Int #

elem :: Eq a => a -> BinTree a -> Bool #

maximum :: Ord a => BinTree a -> a #

minimum :: Ord a => BinTree a -> a #

sum :: Num a => BinTree a -> a #

product :: Num a => BinTree a -> a #

Traversable BinTree Source # 

Methods

traverse :: Applicative f => (a -> f b) -> BinTree a -> f (BinTree b) #

sequenceA :: Applicative f => BinTree (f a) -> f (BinTree a) #

mapM :: Monad m => (a -> m b) -> BinTree a -> m (BinTree b) #

sequence :: Monad m => BinTree (m a) -> m (BinTree a) #

Eq a => Eq (BinTree a) Source # 

Methods

(==) :: BinTree a -> BinTree a -> Bool #

(/=) :: BinTree a -> BinTree a -> Bool #

Ord a => Ord (BinTree a) Source # 

Methods

compare :: BinTree a -> BinTree a -> Ordering #

(<) :: BinTree a -> BinTree a -> Bool #

(<=) :: BinTree a -> BinTree a -> Bool #

(>) :: BinTree a -> BinTree a -> Bool #

(>=) :: BinTree a -> BinTree a -> Bool #

max :: BinTree a -> BinTree a -> BinTree a #

min :: BinTree a -> BinTree a -> BinTree a #

Read a => Read (BinTree a) Source # 
Show a => Show (BinTree a) Source # 

Methods

showsPrec :: Int -> BinTree a -> ShowS #

show :: BinTree a -> String #

showList :: [BinTree a] -> ShowS #

DrawASCII (BinTree ()) Source # 

Methods

ascii :: BinTree () -> ASCII Source #

HasNumberOfLeaves (BinTree a) Source # 
HasNumberOfNodes (BinTree a) Source # 

graft :: BinTree (BinTree a) -> BinTree a Source #

The monadic join operation of binary trees

data BinTree' a b Source #

A binary tree with leaves and internal nodes decorated with types a and b, respectively.

Constructors

Branch' (BinTree' a b) b (BinTree' a b) 
Leaf' a 

Instances

(Eq a, Eq b) => Eq (BinTree' a b) Source # 

Methods

(==) :: BinTree' a b -> BinTree' a b -> Bool #

(/=) :: BinTree' a b -> BinTree' a b -> Bool #

(Ord a, Ord b) => Ord (BinTree' a b) Source # 

Methods

compare :: BinTree' a b -> BinTree' a b -> Ordering #

(<) :: BinTree' a b -> BinTree' a b -> Bool #

(<=) :: BinTree' a b -> BinTree' a b -> Bool #

(>) :: BinTree' a b -> BinTree' a b -> Bool #

(>=) :: BinTree' a b -> BinTree' a b -> Bool #

max :: BinTree' a b -> BinTree' a b -> BinTree' a b #

min :: BinTree' a b -> BinTree' a b -> BinTree' a b #

(Read a, Read b) => Read (BinTree' a b) Source # 
(Show a, Show b) => Show (BinTree' a b) Source # 

Methods

showsPrec :: Int -> BinTree' a b -> ShowS #

show :: BinTree' a b -> String #

showList :: [BinTree' a b] -> ShowS #

HasNumberOfLeaves (BinTree' a b) Source # 
HasNumberOfNodes (BinTree' a b) Source # 

Methods

numberOfNodes :: BinTree' a b -> Int Source #

Conversion to rose trees (Data.Tree)

toRoseTree :: BinTree a -> Tree (Maybe a) Source #

Convert a binary tree to a rose tree (from Data.Tree)

module Data.Tree

Enumerate leaves

enumerateLeaves_ :: BinTree a -> BinTree Int Source #

Enumerates the leaves a tree, starting from 0, ignoring old labels

enumerateLeaves :: BinTree a -> BinTree (a, Int) Source #

Enumerates the leaves a tree, starting from zero

enumerateLeaves' :: BinTree a -> (Int, BinTree (a, Int)) Source #

Enumerates the leaves a tree, starting from zero, and also returns the number of leaves

Nested parentheses

nestedParentheses :: Int -> [[Paren]] Source #

Generates all sequences of nested parentheses of length 2n in lexigraphic order.

Synonym for fasc4A_algorithm_P.

fasc4A_algorithm_P :: Int -> [[Paren]] Source #

Generates all sequences of nested parentheses of length 2n. Order is lexicographical (when right parentheses are considered smaller then left ones). Based on "Algorithm P" in Knuth, but less efficient because of the "idiomatic" code.

fasc4A_algorithm_W :: RandomGen g => Int -> g -> ([Paren], g) Source #

Generates a uniformly random sequence of nested parentheses of length 2n. Based on "Algorithm W" in Knuth.

fasc4A_algorithm_U Source #

Arguments

:: Int

n

-> Integer

N; should satisfy 1 <= N <= C(n)

-> [Paren] 

Nth sequence of nested parentheses of length 2n. The order is the same as in fasc4A_algorithm_P. Based on "Algorithm U" in Knuth.

Generating binary trees

binaryTrees :: Int -> [BinTree ()] Source #

Generates all binary trees with n nodes. At the moment just a synonym for binaryTreesNaive.

countBinaryTrees :: Int -> Integer Source #

# = 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.

randomBinaryTree :: RandomGen g => Int -> g -> (BinTree (), g) Source #

Generates an uniformly random binary tree, using fasc4A_algorithm_R.

fasc4A_algorithm_R :: RandomGen g => Int -> g -> (BinTree' Int Int, g) Source #

Grows a uniformly random binary tree. "Algorithm R" (Remy's procudere) in Knuth. Nodes are decorated with odd numbers, leaves with even numbers (from the set [0..2n]). Uses mutable arrays internally.

ASCII drawing

asciiBinaryTree_ :: BinTree a -> ASCII Source #

Draws a binary tree in ASCII, ignoring node labels.

Example:

autoTabulate RowMajor (Right 5) $ map asciiBinaryTree_ $ binaryTrees 4

Graphviz drawing

graphvizDotForest Source #

Arguments

:: Show a 
=> Bool

make the individual trees clustered subgraphs

-> Bool

reverse the direction of the arrows

-> String

name of the graph

-> Forest a 
-> Dot 

Generates graphviz .dot file from a forest. The first argument tells whether to make the individual trees clustered subgraphs; the second is the name of the graph.

graphvizDotTree Source #

Arguments

:: Show a 
=> Bool

reverse the direction of the arrow

-> String

name of the graph

-> Tree a 
-> Dot 

Generates graphviz .dot file from a tree. The first argument is the name of the graph.

Bijections