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.