Module CCKTree
Lazy Tree Structure
This structure can be used to represent trees and directed graphs (as infinite trees) in a lazy fashion. Like CCKList
, it is a structural type.
type 'a iter
= ('a -> unit) -> unit
type 'a gen
= unit -> 'a option
type 'a printer
= Stdlib.Format.formatter -> 'a -> unit
Basics
type +'a t
= unit -> [ `Nil | `Node of 'a * 'a t list ]
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
Fold on values in no specified order. May not terminate if the tree is infinite.
val height : _ t -> int
Length of the longest path to empty leaves.
Graph Traversals
class type 'a pset = object ... end
Abstract Set structure
val set_of_cmp : cmp:('a -> 'a -> int) -> unit -> 'a pset
Build a set structure given a total ordering.
val dfs : pset:'a pset -> 'a t -> [ `Enter of 'a | `Exit of 'a ] Stdlib.Seq.t
Depth-first traversal of the tree.
val force : 'a t -> [ `Nil | `Node of 'a * 'b list ] as 'b
force t
evaluatest
completely and returns a regular tree structure.- since
- 0.13
Pretty-printing
Example (tree of calls for naive Fibonacci function):
let mk_fib n =
let rec fib' l r i =
if i=n then r else fib' r (l+r) (i+1)
in fib' 1 1 1;;
let rec fib n = match n with
| 0 | 1 -> CCKTree.singleton (`Cst n)
| _ -> CCKTree.node2 (`Plus (mk_fib n)) (fib (n-1)) (fib (n-2));;
let pp_node fmt = function
| `Cst n -> Format.fprintf fmt "%d" n
| `Plus n -> Format.fprintf fmt "%d" n;;
Format.printf "%a@." (CCKTree.pp pp_node) (fib 8);;
Pretty printing in the DOT (graphviz) format
module Dot : sig ... end