[Haskell-cafe] Trees (Rose Trees?)

Andrew Wagner wagner.andrew at gmail.com
Mon Jul 21 23:21:37 EDT 2008


There are a few different kinds of trees, but if you only need to
store data on the leaves, that representation will work. As for your
sumTree function, you are indeed missing a case...consider sumTree
(Leaf 3)! Once you deal with that case, the other two can actually be
combined (since sum [] = 0).

2008/7/21 Ryan Bloor <ryanbloor at hotmail.com>:
> hi
>
> I was curious as to whether my implementation of a Rose Tree and a sumTree
> function was correct. The aumTree adds up the elements of a tree.
>
> data Tree a = Leaf a | Node [Tree a]
>
> sumTree :: Tree Int -> Int
> sumTree (Node []) = 0
> sumTree (Node xs) = sum (map sumTree xs)
>
> The problem with this is I get a pattern matching error. Am I representing
> trees right... see below.
>
> Also, would an empty tree be represented by ... Node [] with this
> implementation?
> How would I represent a tree of the form... Tree (Node 2(Node 6 Empty Empty)
> Empty) taken from a binary one.
> Like this? Node [ [Leaf 2], Node [ Leaf 6,Node[],Node[] ], Node[] ]
>
> Ryan
>
>
>
> ________________________________
> Find out how to make Messenger your very own TV! Try it Now!
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list