And here's an approach using a Discriminated Union:

1
2
3
4
5
6
7
8
9
10
11
12
13
#light

type 'a Tree =
    | Node of 'a Tree list
    | Leaf of 'a

let rec countLeaves = function
    | Leaf _       -> 1
    | Node []      -> 0
    | Node (t::ts) -> countLeaves t + countLeaves (Node(ts))

let t = Node([ Leaf(1); Leaf(2); Node([ Leaf(3); Leaf(4) ]) ])
printfn "%d" (countLeaves t)
By on 1/2/2009 9:32 AM ()

There's quite a few choices, probably the closest to a tree view is just to use a record:

type Node<'a> =

{ Value: 'a;

Children: List<Node<'a>> }

But if you have representation that this so close, you may as well use use the TreeNode directly. It's quite useful to places the values you're interested in the .Tag property.

Cheers,

Rob

By on 1/2/2009 9:26 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper