Pages

Sunday, August 29, 2010

General Tree in F#

In the previous post, we have a binary tree representation. The following is a general way to represent the tree.


type CommonTreeType =
    | EmptyTreeNode
    | CommonTreeNode of Element * CommonTreeType list


let rec iterateCommonTree tree emptyF commonF =
    match tree with
        | EmptyTreeNode -> emptyF
        | CommonTreeNode(n, children) ->
            let childResult = children |> Seq.map (fun tree->iterateCommonTree tree emptyF commonF)
            commonF n childResult

No comments: