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.
Here is the code My height function works correctly.
deepest(node(L,X,R),X):- height(L,0),height(R,0).
deepest(node(L,_,R),X):- height(L,D1),height(R,D2), D1 > D2, deepest(L,X).
deepest(node(L,_,R),X):- height(L,D1),height(R,D2), D1 =< D2, deepest(R,X).
edit: example
?- 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.
deepest(node(leaf,X,leaf),X).?