I get rather an infinite loop for the query (note the missing comma in your original query):
?- inOrder(bttree(20,
bttree(10, bttree(5, nil, nil), bttree(15, nil, nil)), bttree(30)), T).
loops.
And I get the same infinite loop, even after adding goals false
into your program. The resulting program is called a failure-slice:
inOrder(bttree(_,L,_),T):- false, inOrder(L,T).
inOrder(bttree(N,_,_),T) :- T1 = T, append(T1,N,T), false.
inOrder(bttree(_,_,R),T):- false, inOrder(R,T).
So this remaining clause alone causes this loop. In fact, any query with a node bttree/3
will now loop, even
?- inOrder(bttree(20, nil,nil), T).
loops.
So we need to fix at least that remaining visible part.
Not that clauses are read independently of each other, so your first clause reads:
inOrder(bttree(_,L,_),T):- inOrder(L,T).
So the inorder traversal of a node will be just the inorder traversal of a the left subtree. That does not seem right.
What you actually want is to describe them together. The best way is not to use append/3
but rather a dcg:
inorder(nil) --> [].
inorder(bttree(N,L,R)) -->
inorder(L),
[N],
inorder(R).
Now we get:
?- phrase(inorder(bttree(20,
bttree(10, bttree(5, nil, nil), bttree(15, nil, nil)), bttree(30))), T).
false.
Again, false! But this time it is this bttree(30)
which should be replaced:
?- phrase(inorder(bttree(20,
bttree(10, bttree(5, nil, nil), bttree(15, nil, nil)), bttree(30,nil,nil))), T).
T = [5,10,15,20,30].