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">&lt;<a href="mailto:rk@trie.org">rk@trie.org</a>&gt;</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 &lt;<a href="mailto:mabufo@gmail.com">mabufo@gmail.com</a>&gt; wrote:<br>
&gt; Hi guys! Just so we are all on the same page, this problem is an<br>
&gt; exercise from the end of Chapter 3 in the book Real World Haskell<br>
&gt; (#8).<br>
&gt;<br>
&gt; The problem calls for me to write a function to figure out the height<br>
&gt; of our user defined binary tree type.<br>
&gt; Here is the type:<br>
&gt;  --chapter3 binary tree recursive type<br>
&gt; data Tree a = Node a (Tree a) (Tree a)<br>
&gt;                          | Empty<br>
&gt;                                deriving (Show)<br>
&gt; With this type, we&#39;d create a tree with no leaves like: Node &quot;tree&quot;<br>
&gt; Empty Empty, and a tree with a single leaf like Node &quot;tree2&quot; = Empty<br>
&gt; (Node &quot;leaf&quot; Empty Empty)<br>
&gt;<br>
&gt; Being new to Haskell, I&#39;m not sure how to traverse this binary tree<br>
&gt; type that the book has given. No doubt we&#39;ll be using some crafty<br>
&gt; recursion to get this done. So to summarize what I&#39;d like to know, 1)<br>
&gt; what is the best way to figure out the height of this binary tree type<br>
&gt; that I have, or rather any binary tree in general? 2) How do I<br>
&gt; translate that into Haskell code.<br>
&gt;<br>
&gt; Verbose explanations are appreciated.<br>
&gt;<br>
&gt; Thanks guys!<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; -Matthew<br>
&gt; _______________________________________________<br>
&gt; Beginners mailing list<br>
&gt; <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
&gt;<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>