I am a total beginner to logic programming and Prolog so the problem is that i am trying to implement a binary tree in Prolog which i did like this:
tree(-).
tree(n(X,L,R)):- tree(L), tree(R).
now i need to implement the following predicates
count(X,Tree,Res).
which should count how many times X is given in the passed tree and return it in Res.
sum(Tree, Sum).
which should return the Sum of all Nodes in the passed tree if Numerical.
My idea for this one was something like this:
treeSum([],0).
treeSum(tree(X,T1,T2), S) :-
treeSum(T1,S1),
treeSum(T2,S2),
S is X + S1 + S2.
replace(X , Y, TreeIn , TreeOut).
which should swap every Node containing X with Y
in TreeIn and return TreeOut.