Add branches in prolog [closed]

0

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

    
asked by Daniel González Martínez 04.12.2017 в 08:01
source

1 answer

0

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:

  • The sum of an empty tree ( vacio ) is 0.
  • The sum of a non-empty tree ( 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.
    
answered by 04.12.2017 / 12:48
source