I am trying to solve a problem where I need to evaluate the value of a binary expression tree.
tree_calc(Tree, Eval)
where Eval
should hold the final result and Tree
must be in the form:
tree(LeftNode, Operator, RightNode)
If I create the tree
function as per the form above, how am I supposed to pass the results of the calculations back up the recursion if there isn't an empty variable to store the results?
My understanding is that an extra variable is always needed to store the result in.
Apologies in advance as I am sure I am misunderstanding something basic.
Thanks for your help.
In this context,
tree
isn't what you are calling a function, nor is it what Prolog would refer to as a predicate.tree(LeftNode, Operator, RightNode)
is just a term that is used as a structure to represent the tree. So, for example,(2 + 3) * 4
would be represented by,tree(tree(2,+,3), *, 4)
. You would call your predicate withtree_calc(tree(tree(2,+,3),*,4), Eval)
and expect that query to yield,Eval = 20
. The term,tree/3
doesn't carry results in this case.To give an example of how to access
tree/3
in a predicate, here's a simple predicate that evaluates the simplest tree (so there's no recursion to tree leaves):You can generalize
compute/4
:And you can call it like so:
For your more general case, you'll need to check
L
andR
to see if they are atree/3
structure themselves and recursively calltree_calc
recursively if so.Prolog is good at matching term patters. So if you check unification
L = tree(LL, Op, LR)
and it succeeds, that meansL
is atree
with left branchLL
, operationOp
, and right branchLR
. You can play with this behavior at the Prolog prompt to see how it works: