To start off this is a homework question poised to me.
I am supposed to write a predicate btree_height\2 that takes a Binary tree and (for now) just returns the height of the tree.
A binary tree is represented as:
node(node(leaf, x, leaf), x, node(leaf, x, leaf))
where the x's are integer values of the node. (This is just an example tree).
My code is as follows:
btree_height(leaf, 0).
btree_height(node(leaf,_,leaf), 1).
btree_height(node(LT,_,RT), D):-
btree_height(LT, DL),
btree_height(RT, DR),
D is max(DL,DR)+1.
The problem that I am having is that when I call btree_height(BT, D) and supply it with a BT if the depth is 4 then it recurses 4 times and "returns" the number 4 four times. According to my professor this is an incorrect behavior as it should only return the number 4 once. (Using the example above it returns the number 2 twice)
This is my first time coding in Prolog and I have no idea where I should start looking.
This is technically SWI-Prolog if it makes a difference...