treeMerge in swi-prolog - prolog

I am trying to build a predicate treeMerge(A,B,C) that returns true if C is the result of merging the two trees A and B. Any suggestions on how I can implement this? I have a rough idea, I am thinking of merging the root, then first child then second and so on, but I am fairly new to prolog.

The idea is to get the list of the nodes inside a tree then add each nodes in the other tree. I've developed a predicate merge/3 which merges the two tree as input in a third tree (insert all the nodes of the first tree into the second). A tree is represented as t(Node,Left,Right). Here is the code:
getNodes(nil,[]).
getNodes(t(E,L,R),[E|S]) :-
append(SL,SR,S),
getNodes(L,SL),
getNodes(R,SR),!.
insert(Node,nil,t(Node,nil,nil)).
insert(Node,t(El,Left,Right),t(El,L,R)):-
(Node > El ->
insert(Node,Right,R),
L = Left
; insert(Node,Left,L),
R = Right).
insertNodesList([],T,T).
insertNodesList([H|T],Tree,M):-
insert(H,Tree,T1),
insertNodesList(T,T1,M).
merge(T1,T2,Merged):-
getNodes(T1,LNodesT1),
insertNodesList(LNodesT1,T2,Merged).
Query:
?-merge(t(3,t(2,t(1,nil,nil),nil),t(4,nil,t(7,nil,t(5,nil,nil)))),t(3,t(2,nil,nil),t(4,nil,t(7,nil,t(5,nil,nil)))),T).
T = t(3, t(2, t(2, t(1, nil, nil), nil), t(3, nil, nil)), t(4, t(4, nil, nil), t(7, t(7, t(5, nil, nil), nil), t(5, nil, nil))))

Related

Find binary search trees based on traversal in prolog

I am trying to write a Prolog predicate that gives a possible binary search tree for a given traversal. I chose to represent the tree like t(Root, Left Subtree, Right Subtree), leaves are simply t(Number) and when a subtree does not exist its value is nil.
This is what I have so far (only for postorder traversal in this case):
post(nil, []).
post(t(X), [X]).
post(t(N, L, R), T) :-
post(L, TL), post(R, TR),
append([TL, TR, [N]], T).
This works nicely in one way, but hangs in the other direction:
?- post(t(8, t(5, t(2), nil), t(12, t(9), t(16))), Trav).
Trav = [2, 5, 9, 16, 12, 8].
?- post(Tree, [2, 5, 9, 16, 12, 8]).
Tree = t(8, nil, t(12, nil, t(16, nil, t(9, nil, t(5, nil, t(2)))))) ;
Tree = t(8, nil, t(12, nil, t(16, nil, t(9, nil, t(5, nil, t(2, nil, nil)))))) ;
[execution hangs here]
I realized that post does not require a binary search tree, ie there is no requirement for all nodes in the left subtree to be less than the root node and all nodes in the right subtree to be greater than the root node, so I also wrote this:
tree(t(_)).
tree(nil).
tree(t(N, L, R)) :-
tree(L), tree(R),
( L = t(NL, _, _) -> NL < N ; true ),
( R = t(NR, _, _) -> NR > N ; true ).
I thought I could just do ?- post(Tree, [...]), tree(Tree). to make Prolog only return actual binary search trees. However, I seem to be stuck already at generating possible trees.
How could I improve my code? Is this even doable?
My suggestion is to write different code for different directions. Here is the code to transform a list back into a tree. The main difference with the original code is that we deconstruct the list (with last/3 and append/3) before reconstructing the tree. Note that I added the code to check for a search tree in third clause (the two maplist/2calls) but those can be as well removed if you want to keep it separately.
postlist_to_tree([],nil).
postlist_to_tree([X],t(X)).
postlist_to_tree(Xs,t(Last,LT,RT)):-
last(Fore,Last,Xs),
append(LL,RL,Fore),
maplist('>='(Last),LL),
maplist('=<'(Last),RL),
postlist_to_tree(LL, LT),
postlist_to_tree(RL, RT).
Now to have it as a single predicate, I would suggest to do something like this, using a non-logical predicate (ground/1 in this example) to decide which version to call depending on the instantiation of the arguments at call time.
post2(Tree,List):-
ground(List),
!,
postlist_to_tree(List,Tree).
post2(Tree,List):-
post(Tree,List).

Trying to add siblings to every node in a binary tree with one child so that all nodes have 2 or zero children. In other words no nodes have 1 child

My prolog program is supposed take a binary tree and make a new tree that adds a child to any node that only has one child so that all nodes have 2 or zero children. The binary tree representation being used is along the lines of t(73,t(31,t(5,nil,nil),nil),t(101,t(83,nil,t(97,nil,nil)),nil)). The issue is that it only fixes the nodes that have a missing right child and its not working for nodes that are missing a left node.
treeEx(X) :-
X = t(73,t(31,t(5,nil,nil),nil),t(101,t(83,nil,t(97,nil,nil)),nil)).
singleFill(t(_,nil,nil),_,_):-!.
singleFill(t(Root,nil,Right),V,t(Root,t(V,nil,nil),Right)):-
singleFill(Right,V,_).
singleFill(t(Root,Left,nil),V,t(Root,Left,t(V,nil,nil))):-
singleFill(Left,V,_).
singleFill(t(Root,Left,Right),V,t(Root,LD,RD)):-
singleFill(Left,V,LD),
singleFill(Right,V,RD).
When using the query treeEx(T),singleFill(T,0,L). it should produce the result:
T = t(73, t(31, t(5, nil, nil), nil), t(101, t(83, nil, t(97,
nil, nil)), nil)),
L = t(73, t(31, t(5, nil, nil), t(0, nil, nil)), t(101, t(83,
t(0, nil, nil), t(97, nil, nil)), t(0, nil, nil)))
however mine produces:
T = t(73, t(31, t(5, nil, nil), nil), t(101, t(83, nil, t(97, nil,
nil)), nil)),
L = t(73, t(31, t(5, nil, nil), t(0, nil, nil)), t(101, t(83, nil,
t(97, nil, nil)), t(0, nil, nil))) .
The problem is that the node with 83 has one child but its not adding the zero. When I traced it I noticed that it was because there are 2 separate reclusive calls that have 2 different forms of the tree so I think that only the nodes with right children are saving the changes to the new tree. However the changes to the left tree are not
So, right off the bat we have a few weird things about your code:
?- singleFill(t(1,nil,nil), 0, Y).
true.
It's weird to me that this is true but didn't bind anything on Y. This must be the behavior that you noticed. I think it may stem from this:
?- singleFill(nil, 0, Y).
false.
Namely, your code doesn't handle the base case of the recursion. This is why you seem to need so many cases. Always check your predicates with simple inputs close to the base cases, those are the ones that will help you see the problem. In actuality, you only need three cases:
single_fill(nil, New, t(New, nil, nil)).
single_fill(t(V, nil, nil), _, t(V, nil, nil)) :- !.
single_fill(t(V, Left, Right), New, t(V, NewLeft, NewRight)) :-
single_fill(Left, New, NewLeft),
single_fill(Right, New, NewRight).
Notice that I'm only ignoring one variable here, and it's the fill variable in the case where no filling happens.
I still have a cut here, which I'm not happy about. Without the cut, the first solution matches your specification but you get several more, basically one for each case where there was nil in both branches in the tree. With the cut, this predicate will successfully "check" those inputs even though it wouldn't generate them, which is a violation of backwards correctness. This suggests to me that there may be a better way to write this that doesn't have this problem. But I couldn't think of it. I feel like it should be sufficient to say Left \= nil, Right \= nil instead but that didn't work.

Prolog, result correctly given but false is printed

I have for example the following function computing the set of all internal nodes in a binary tree (nodes which have at least one child, i.e. are no leaves)
internals(nil,[]).
internals(t(_,nil,nil),[]).
internals(t(X,L,nil),[X|S]) :- L = t(_,_,_), internals(L,S).
internals(t(X,nil,R),[X|S]) :- R = t(_,_,_), internals(R,S).
internals(t(X,L,R),[X|S]) :- L = t(_,_,_), R = t(_,_,_),
internals(L,SL), internals(R,SR), append(SL,SR,S).
the result of this is
?- internals(t(3, t(2, t(1, nil, nil), nil), t(5, nil, t(7, nil, nil))), X).
X = [3, 2, 5] ;
false.
Why does it end in false and how come the following code does the same but does not result in false
internals(nil,[]).
internals(t(_,nil,nil),[]) :- !.
internals(t(X,L,R),[X|Xs]):-
internals(L,LI), internals(R,RI),
append(LI,RI,Xs).
?- internals(t(3, t(2, t(1, nil, nil), nil), t(5, nil, t(7, nil, nil))), X).
X = [3, 2, 5].

Get all possible binary trees using Prolog?

I'm aware there are lots of questions about defining a binary tree to check whether something is a binary tree or not,but i could not find a thread that tackled this question in the "opposite direction".
Why does your definition of binary tree not return all possible binary trees when called as "es_arbol(X)"? Explain in detail and try to implement a different definition that does return all possible binary tree structures.
Ok,so basically i'm stuck in this part of an assignment.After defining my binary-tree-validating function i noticed that when called with no arguments it just returns trees that "grow" through their right nodes,or at least that's how i interpret the output of swi-prolog.What i am not getting is,assuming my definition is correct,Prolog should be able to construct them in both ways.If not,i would like if someone could point me in the right direction to work out a more general definition of a binary tree,or maybe explain why is it that my definition is not sufficient.
This is my definition:
es_arbol(nil).
es_arbol(arbol(_,I,D)) :- es_arbol(I), es_arbol(D).
The reason that your predicate generates an infinite number of trees along one branch is because you have more than one recursion, and like any language, Prolog will continue making the first recursive call it finds until it returns, which in this case, never will. So you always recurse on one leg of the tree. In other words, you have at least two variables in each tree (the left and right subtrees) that have an infinite number of possibilities.
Binary trees have an infinite number of recursive possibilities along two dimensions. You need a way to order the trees using a single-dimensional metric. One such metric could be the count of nodes in the tree. If you order your trees by node count, starting with node count 0, then for each node count, there are a finite number of trees to enumerate. Here is the general way this would work:
Nodes is a valid number of nodes
nil is a valid binary tree with 0 nodes
arbol(_, TL, TR) is a valid binary tree with N nodes if NL and NR2 add up to N-1 and TL is a valid binary tree of NL nodes, and TR is a valid binary tree of NR nodes. Since Prolog will find all solutions from a given point before backtracking prior to that point, it will search for all of the trees with the given number of nodes first before backtracking to a new valid number of nodes.
In Prolog, it looks like this.
:- use_module(library(clpfd)).
es_arbol(Tree) :-
length(_, Nodes),
es_arbol(Tree, Nodes).
I'm using length/2 to "generate" Nodes values of 0, 1, 2, etc. es_arbol(Tree) will succeed with binary trees with successive node counts starting at 0. For a given node count, Nodes, it will find all the solutions to es_arbol(Tree, Nodes) before it finally fails and backtracks to length(_, Nodes) again which will succeed on the next value of Node.
es_arbol(nil, 0).
es_arbol(arbol(_,TreeL,TreeR), N) :-
N #> 0,
NL + NR #= N - 1,
NL #>= 0, NR #>= 0,
es_arbol(TreeL, NL),
es_arbol(TreeR, NR).
The base case is trivial. nil is the tree with 0 nodes. The recursive case says that arbol(_,L,R) is a binary tree with N nodes if N > 0, NL and NR are non-negative integers that add up to N, and TL and TR are binary trees with length NL and NR, respectively.
The results of running the above code are:
?- es_arbol(Tree).
Tree = nil ;
Tree = arbol(_G258, nil, nil) ;
Tree = arbol(_G17, nil, arbol(_G157, nil, nil)) ;
Tree = arbol(_G17, arbol(_G200, nil, nil), nil) ;
Tree = arbol(_G14, nil, arbol(_G154, nil, arbol(_G593, nil, nil))) ;
Tree = arbol(_G14, nil, arbol(_G154, arbol(_G603, nil, nil), nil)) ;
Tree = arbol(_G14, arbol(_G130, nil, nil), arbol(_G191, nil, nil)) ;
Tree = arbol(_G14, arbol(_G53, nil, arbol(_G193, nil, nil)), nil) ;
Tree = arbol(_G14, arbol(_G53, arbol(_G236, nil, nil), nil), nil) ;
Tree = arbol(_G14, nil, arbol(_G100, nil, arbol(_G214, nil, arbol(_G354, nil, nil)))) ;
Tree = arbol(_G14, nil, arbol(_G100, nil, arbol(_G214, arbol(_G397, nil, nil), nil))) ;
Tree = arbol(_G14, nil, arbol(_G100, arbol(_G216, nil, nil), arbol(_G277, nil, nil))) ;
Tree = arbol(_G14, nil, arbol(_G100, arbol(_G139, nil, arbol(_G279, nil, nil)), nil)) ;
Tree = arbol(_G14, nil, arbol(_G100, arbol(_G139, arbol(_G322, nil, nil), nil), nil)) ;
Tree = arbol(_G14, arbol(_G130, nil, nil), arbol(_G191, nil, arbol(_G664, nil, nil))) ;
Tree = arbol(_G14, arbol(_G130, nil, nil), arbol(_G191, arbol(_G674, nil, nil), nil)) ;
Tree = arbol(_G14, arbol(_G132, nil, arbol(_G272, nil, nil)), arbol(_G676, nil, nil)) .
...
As #false has pointed out in the comments, the use of CLP(FD) is not the most efficient way in this case of applying a enumerative constraint. An alternative, more efficient means would be to use between/3:
es_arbol(nil, 0).
es_arbol(arbol(_,TreeL,TreeR), N) :-
N > 0,
N1 is N - 1,
between(0, N1, NL),
NR is N1 - NL,
es_arbol(TreeL, NL),
es_arbol(TreeR, NR).
Lurker has already given a very nice general solution using CLP(FD) constraints.
I would like to augment the existing answer with an alternative way to constrain the depth of the search. Instead of integers, I am using a list to "count" in a symbolic way.
To reason about lists in Prolog, DCG notation (dcg) is often very convenient, also in this case:
es_arbol(nil) --> [].
es_arbol(arbol(_,I,D)) --> [_], es_arbol(I), es_arbol(D).
Declaratively, you can think about these rules as "consuming credit" to apply.
If I query naively, then I get an unfair enumeration:
?- phrase(es_arbol(A), Ls).
A = nil,
Ls = [] ;
A = arbol(_9016, nil, nil),
Ls = [_9024] ;
A = arbol(_9016, nil, arbol(_9030, nil, nil)),
Ls = [_9024, _9038] ;
A = arbol(_9016, nil, arbol(_9030, nil, arbol(_9044, nil, nil))),
Ls = [_9024, _9038, _9052] ;
A = arbol(_9016, nil, arbol(_9030, nil, arbol(_9044, nil, arbol(_9058, nil, nil)))),
Ls = [_9024, _9038, _9052, _9066] .
The point is that we can easily turn this into a fair enumeration by restricting the length of the list. For example, to get all trees with exactly two inner node, we can use:
?- phrase(es_arbol(A), [_,_]).
A = arbol(_10426, nil, arbol(_10434, nil, nil)) ;
A = arbol(_10426, arbol(_10434, nil, nil), nil) ;
false.
Building on this, we can use iterative deepening to fairly enumerate all tree shapes:
?- length(Ls, _), phrase(es_arbol(A), Ls).
Ls = [],
A = nil ;
Ls = [_7130],
A = arbol(_7142, nil, nil) ;
Ls = [_7130, _7136],
A = arbol(_7148, nil, arbol(_7156, nil, nil)) ;
Ls = [_7130, _7136],
A = arbol(_7148, arbol(_7156, nil, nil), nil) ;
Ls = [_7130, _7136, _7142],
A = arbol(_7154, nil, arbol(_7162, nil, arbol(_7170, nil, nil))) ;
Ls = [_7130, _7136, _7142],
A = arbol(_7154, nil, arbol(_7162, arbol(_7170, nil, nil), nil)) ;
Ls = [_7130, _7136, _7142],
A = arbol(_7154, arbol(_7162, nil, nil), arbol(_7170, nil, nil)) .
Thus, counting "symbolically" is sometimes a convenient alternative to using actual integers.

Prolog, reconstruct BST trees from inorder list

We well know inorder implementation for BST tree.
inorder(nil, []).
inorder(t(Root, L, R), List) :-
inorder(L, ListLeft),
inorder(R, ListRight),
append(ListLeft, [Root|ListRight], List).
However, is it possible to do it for list ? I mean reconstruct all possible BST trees, for example:
inorder(X, [1,2,3]).
X = t(1, nil, t(2, nil, t(3, nil, nil))));
X = t(3, t(2, t(1, nil, nil), nil), nil), nil);
X = t(2, t(1, nil, nil), t(3, nil, nil));
false.
It seems to be impossible for me.
First, let us use a Definite Clause Grammar (dcg) for relating trees to lists:
inorder(nil) --> [].
inorder(t(Root, L, R)) -->
inorder(L),
[Root],
inorder(R).
The trick I will now apply is described in Ulrich Neumerkel's dissertation in Taming Left Recursion.
"... we add another state for the number of tokens that can be used by newly encountered nonterminals. The number of tokens that will be read by the terminals within a single rule are therefore reserved in advance."
In our case:
inorder(nil, Es, Es) --> [].
inorder(t(Root, L, R), [_|Es0], Es) -->
inorder(L, Es0, Es1),
[Root],
inorder(R, Es1, Es).
Sample query (Ls omitted):
?- Ls = [1,2,3], phrase(inorder(Tree, Ls, _), Ls).
Tree = t(1, nil, t(2, nil, t(3, nil, nil))) ;
Tree = t(1, nil, t(3, t(2, nil, nil), nil)) ;
Tree = t(2, t(1, nil, nil), t(3, nil, nil)) ;
Tree = t(3, t(1, nil, t(2, nil, nil)), nil) ;
Tree = t(3, t(2, t(1, nil, nil), nil), nil) ;
false.
Another way to solve such issues is to use your Prolog system's tabling mechanism.
If you like the trick I've proposed here, the only change required could be
:- use_module(carlo(snippets/when_)).
inorder(t(Root, L, R), List) -:- ...
and now
?- inorder(T,[1,2,3]).
T = t(1, nil, t(2, nil, t(3, nil, nil))) ;
T = t(1, nil, t(3, t(2, nil, nil), nil)) ;
T = t(2, t(1, nil, nil), t(3, nil, nil)) ;
T = t(3, t(1, nil, t(2, nil, nil)), nil) ;
T = t(3, t(2, t(1, nil, nil), nil), nil) ;
false.

Resources