Buscar..


Arbol de prueba

El árbol de prueba (también árbol de búsqueda o árbol de derivación) es un árbol que muestra la ejecución de un programa Prolog. Este árbol ayuda a visualizar el proceso de seguimiento cronológico presente en Prolog. La raíz del árbol representa la consulta inicial y las ramas se crean cuando se producen puntos de elección. Cada nodo en el árbol representa un objetivo. Las ramas solo se convierten en hojas cuando se probó verdadero / falso para el (los) objetivo (s) requerido (s) y la búsqueda en Prolog se realiza de una manera de profundidad de izquierda a derecha .

Considere el siguiente ejemplo:

% Facts
father_child(paul,chris).        % Paul is the father of Chris and Ellen
father_child(paul,ellen).
mother_child(ellen,angie).       % Ellen is the mother of Angie and Peter
mother_child(ellen,peter).


% Rules
grandfather_grandchild(X,Y) :-
    father_child(X,Z),
    father_child(Z,Y).

grandfather_grandchild(X,Y) :-
    father_child(X,Z),
    mother_child(Z,Y).

Cuando ahora consultamos:

?- grandfather_grandchild(paul,peter).

El siguiente árbol de pruebas visualiza el proceso de búsqueda primero en profundidad:

                                   ?- grandfather_grandchild(paul,peter).
                                       /                             \
                                      /                               \
  ?- father_child(paul,Z1),father_child(Z1,peter).            ?- father_child(paul,Z2),mother_child(Z2,peter).
             /                   \                                    /                              \
      {Z1=chris}             {Z1=ellen}                         {Z2=chris}                        {Z2=ellen}
           /                       \                                /                                  \      
?- father_child(chris,peter).  ?- father_child(ellen,peter).  ?- mother_child(chris,peter). ?- mother_child(ellen,peter).
         |                         |                               |                               /              \ 
       fail                      fail                            fail                          fail(*)          success  

(*) falla para mother_child(ellen,angie) donde 'angie' no coincide con 'peter'



Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow