Hi, I am a beginner in prolog and I am a student on ternary trees. I have been trying to solve the following exercise for some time Go deep into a ternary tree and calculate the sum of each branch from the root to a leaf
Hi, I am a beginner in prolog and I am a student on ternary trees. I have been trying to solve the following exercise for some time Go deep into a ternary tree and calculate the sum of each branch from the root to a leaf
The ternary tree is represented as a arbol(Raiz, Hijo1, Hijo2, Hijo3)
structure, where Raiz
is a number, Hijo1
, Hijo2
e Hijo3
are ternary trees, and vacio
represents an empty tree:
vacio
) is 0. arbol(R,X,Y,Z)
) is the sum of the root ( R
) plus the sum of each of the children ( X
, Y
and Z
). Represented in prolog:
suma(vacio, 0).
suma(arbol(R,X,Y,Z), N) :-
suma(X, A),
suma(Y, B),
suma(Z, C),
N is R+A+B+C.