How to draw a search tree in prolog?

5.4k Views Asked by At

I need to know how can I draw a search tree for a specific query and how can I trace a code in prolog, and here is an example:

mystery([], []).
mystery([b,a|XS], [z,z|YS]) :- mystery(XS, YS).
mystery([X|XS], [X|YS]) :- mystery(XS, YS).

a) Draw the search tree for the following query:

?- mystery([b,a,b,a,b], Z).
1

There are 1 best solutions below

0
On

I'm going to tell you how we have learnt to draw the search tree for the query

?- mystery([b,a,b], Z).

which is similar to yours so that you can understand the way search trees are drawn and be able to make one for your exercise.

                          ?- mystery([b,a,b],Z)
                (1st solution) /          \ (2nd solution)
                              /            \
                          Z = Z'           Z = Z'
                         L'= [b]           L' = [a,b]
                    mystery(L',Z')         mystery(L',Z')
                            |                    |
                            |                    |
                         Z'= Z"               L" = [b]
                        L" = []               Z' = Z"
                      mystery(L",Z")       mystery(L",Z")
                            |                    |
                            |                    |
                        Z" = []               L''' = []
                        Z' = [b]              Z" = Z'''
                        Z = [z,z,b]        mystery(L''',Z''')
                                                 |
                                                 |
                                              Z''' = []
                                              Z" = [b]
                                              Z' = [a,b]
                                              Z = [b,a,b]

All steps are occured while facts and rules are executed. If you cannot understand how your program is working I suggest you use trace mode while you execute it.