title: Prolog, binary tree
body: <p>I want to find the deepest element of a binary tree, So far the code only works for empty tree or the height is one. </p>
  
  <p>Here is the code
  My height function works correctly.</p>
  
  <pre><code>deepest(node(L,X,R),X):- height(L,0),height(R,0).
  deepest(node(L,_,R),X):- height(L,D1),height(R,D2), D1 &gt; D2,  deepest(L,X).
  deepest(node(L,_,R),X):- height(L,D1),height(R,D2), D1 =&lt; D2, deepest(R,X).
  </code></pre>
  
  <p>edit: example</p>
  
  <pre><code>?- deepest(node(node(node(leaf,8,leaf),20,leaf),
                        30,
                        node(node(leaf,88,leaf),33,node(leaf,888,leaf))),
                   X).
  X = 8 ;
  X = 88 ;
  X = 888 ;
  false.
  </code></pre>