The code is also extremely simple:<br><br>height Empty = 0 -- Height of the Empty Node is zero.<br>height (Node t1 t2) = 1 + (max (height t1) (height t2)) -- Height of a non empty node is = 1 + (the greater height of its 2 sub trees)<br>
<br>Regards<br><br><br><div class="gmail_quote">On Fri, Dec 25, 2009 at 02:34, Rahul Kapoor <span dir="ltr"><<a href="mailto:rk@trie.org">rk@trie.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
See if this case analysis helps:<br>
<br>
Height of the Empty Node is zero.<br>
Height of a non empty node is = 1 + (the greater height of its 2 sub trees)<br>
<br>
The function max gives you the greater of two values.<br>
<br>
HTH<br>
<font color="#888888">Rahul<br>
</font><div><div></div><div class="h5"><br>
On Thu, Dec 24, 2009 at 8:18 PM, Matt Young <<a href="mailto:mabufo@gmail.com">mabufo@gmail.com</a>> wrote:<br>
> Hi guys! Just so we are all on the same page, this problem is an<br>
> exercise from the end of Chapter 3 in the book Real World Haskell<br>
> (#8).<br>
><br>
> The problem calls for me to write a function to figure out the height<br>
> of our user defined binary tree type.<br>
> Here is the type:<br>
> --chapter3 binary tree recursive type<br>
> data Tree a = Node a (Tree a) (Tree a)<br>
> | Empty<br>
> deriving (Show)<br>
> With this type, we'd create a tree with no leaves like: Node "tree"<br>
> Empty Empty, and a tree with a single leaf like Node "tree2" = Empty<br>
> (Node "leaf" Empty Empty)<br>
><br>
> Being new to Haskell, I'm not sure how to traverse this binary tree<br>
> type that the book has given. No doubt we'll be using some crafty<br>
> recursion to get this done. So to summarize what I'd like to know, 1)<br>
> what is the best way to figure out the height of this binary tree type<br>
> that I have, or rather any binary tree in general? 2) How do I<br>
> translate that into Haskell code.<br>
><br>
> Verbose explanations are appreciated.<br>
><br>
> Thanks guys!<br>
><br>
><br>
> --<br>
> -Matthew<br>
> _______________________________________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
><br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br>