How to replace a matched node in a binary tree with prolog? Properties of tree: it is not a binary search tree, but every element is unique, so replace operation will effect one element in tree at maximum.
initial tree definition:
tree('Q',
tree('P',
tree('R',
empty,
empty),
tree('S',
empty,
empty)),
tree('T',
empty,
empty))
Let's say new node to replace node 'R' with tree('new', tree('child1', empty, empty), tree('child2', empty, empty))
expected result:
tree('Q',
tree('P',
tree(tree('new',
tree('child1',
empty,
empty),
tree('child2',
empty,
empty)),
tree('S',
empty,
empty)
)),
tree('T',
empty,
empty))
Current status of the code:
:- dynamic([tree/1]).
run:-
retractall(tree(_)),
assertz(tree(tree('Q', tree('P', tree('R', empty, empty), tree('S', empty, empty)), tree('T', empty, empty)))),
retract(tree(T)),
insert('newElement', T, NewTree),
assertz(tree(NewTree)),
tree(T),write(T),!.
insert(NewItem,empty,tree(NewItem,empty,empty)):- !.
insert(NewItem,tree(Element,Left,Right),tree(Element,NewLeft,Right)):-
true, %match function needs to be here
!,insert(NewItem,Left,NewLeft).
insert(NewItem,tree(Element,Left,Right),tree(Element,Left,NewRight)):-
insert(NewItem,Right,NewRight).
I've changed a bit the syntax of your tree, since it was too much verbose for my taste.
Maybe you could consider using a supported tree format, like XML (coded as element/3),
that would give you much power in pattern matching, via library(xpath). Anyway
replace_tree(Old, New, Old, New).
replace_tree(Old, New, t(Key, L, R), t(Key, L1, R1)) :-
replace_tree(Old, New, L, L1),
replace_tree(Old, New, R, R1).
% base case of the recursive data structure
replace_tree(_Old, _New, t, t).
yields
?- T=t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(3,t,t))),t), replace_tree(t(3,X,Y),t(new,X,Y),T,O).
T = t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(3, t, t))), t),
X = t(4, t, t),
Y = t(5, t, t),
O = t(1, t(2, t(new, t(4, t, t), t(5, t, t)), t(6, t, t(3, t, t))), t) ;
T = t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(3, t, t))), t),
X = Y, Y = t,
O = t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(new, t, t))), t) ;
T = O, O = t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(3, t, t))), t) ;
false.
Saving to a file and reading from a file.... If you have a Prolog term, you can read it from a file and write it to a file using ISO Prolog read_term and write_term.
In a file 't.txt' you can have:
tree(b, tree(a, empty, empty), tree(c, empty, empty)).
and then, from the top level:
?- open('t.txt', read, File), read_term(File, Tree, []), close(File).
File = <stream>(0x1a38450),
Tree = tree(b, tree(a, empty, empty), tree(c, empty, empty)).
So this is all in the manual of your Prolog implementation. I am using SWI-Prolog for demonstrating.
Then, is your tree organized in any particular way? It doesn't say, but assuming it is a binary search tree, in file tree.pl:
% insert(T0, E, T1)
% Adding E to the binary search tree T0 results in T1.
insert(empty, E, tree(E, empty, empty)).
insert(tree(X, Left, Right), E, tree(X, Left1, Right)) :-
E #< X,
insert(Left, E, Left1).
insert(tree(X, Left, Right), E, tree(X, Left, Right1)) :-
E #> X,
insert(Right, E, Right1).
Then,
?- [tree].
true.
?- open('t.txt', read, File),
read_term(File, Tree, []),
insert(Tree, x, T1),
close(File).
File = <stream>(0x1a20e80),
Tree = tree(b, tree(a, empty, empty), tree(c, empty, empty)),
T1 = tree(b, tree(a, empty, empty), tree(c, empty, tree(x, empty, empty))).
... and so on.
Related
I am trying to create a swap function in prolog but I ended up with an infinite loop, I tried to debug it using trace()
An example of this function is swap(4, 3, ["You", "Are", "Awesome", "thank", "You"], SwappedList)
With the output being
["You", "Are", "thank", "Awesome", "You"]
In the trace output, it is showing that the problem is in the delete as it is failing and redoes the split
/* Getting the nth element of the list*/
n_thelement(1, [Head|_], Head).
n_thelement(N, [_|Tail], Item):-
NewN is N-1,
n_thelement(NewN, Tail, Item).
/* Deleting the element of the desired Nth element*/
delete(X, [X|Tail], Tail).
delete(X, [Head|Tail], [Head|Item]):-
delete(X, Tail, Item).
/* Adding the deleted element to the beginning of the list*/
append([], Element, Element).
append([Head], Element, [Head|Element]).
swap(X, X, List, List).
swap(X, Y, List, NList):-
n_thelement(X, List, Num1),
n_thelement(Y, List, Num2),
split(X, List, B1, A1),
delete(Num1, A1, L1),
append([Num2], L1, NList1),
append(B1, NList1, NList2),
split(Y, NList2, B2, A2),
delete(Num2, A2, L2),
append([Num1], L2, NList3),
append(B2, NList3, NList).
split(1, [Head|Tail], Head, Tail).
split(N, [Old_List|New_List], Old_List, New_List):-
NewN is N -1,
split(NewN, _, Old_List, New_List).
If I understand your problem statement correctly, given to indices into a list, M and N such that M < N and M and N are both valid indices into the list, you want to swap the elements at those indices.
I would first make the indices zero-relative instead of 1-relative as that makes the math a little easier.
So, you want to break up the list into 5 pieces, 3 of which are themselves lists of any length and two of which are the list entries to be swapped:
As: The lead-in prefix of the list. It is of length M.
B: The 1st item to be swapped.
Cs: The middle segment of the list. It is of length N - (M+1).
D: The 2nd item to be swapped.
Es: The suffix/remainder of the list. It is of any length.
append/3 is useful for deconstruction and reconstruction of lists, making the actual swap easy. You have 3 cases.
First, the special case of both indices being the same, in which case, there is no work to do:
swap( M, M, Ls, Ls ).
Second, the case of the indices being out of order, in which case we just recursively swap them to put them in order:
swap( M, N, Ls, Rs ) :- M > N, swap(N,M,Ls,Rs).
Third, the general case:
swap( M, N, Ls, Rs ) :- % if the 2 indices differ
M < N, % - and are in order
M >= 0, % - and M is greater than or equal to zero
N >= 0, % - and N is greater than or equal to zero
X is N - (M+1), % - compute the length of the middle segment
length( As, M ), % - construct an empty, unbound list of length M, the length of the prefix
length( Cs, X ), % - and construct an empty, unbound list of that length
append( As, [B|T1], Ls), % - get the prefix (As) and the first item (B) to be swapped
append( Cs, [D|Es], T1), % - get the middle segment (Cs), the second item (D) to be swapped, and the suffix (Es)
append( As, [D|Cs], T2), % - concatenate As, D, and Cs, then...
append( T2, [B|Es], Rs ) % - concatenate that with B and the suffix
. % Easy!
You can define a predicate to replace the i-th item of the list for another:
replace(Index, [Old|Rest], [New|Rest], Old, New) :- Index == 0, !.
replace(Index, [First|Rest], [First|NewRest], Old, New) :-
Index > 0,
Previous is Index - 1,
replace(Previous, Rest, NewRest, Old, New).
Examples:
?- replace(1, [a,b,c,d,e], List1, Old1, x).
List1 = [a, x, c, d, e],
Old1 = b.
?- replace(1, [a,b,c,d,e], List1, Old1, New1).
List1 = [a, New1, c, d, e],
Old1 = b.
?- replace(4, [a,b,c,d,e], List2, Old2, New2).
List2 = [a, b, c, d, New2],
Old2 = e.
Then, using this predicate, you can define:
swap(I, J, OldList, NewList) :-
replace(I, OldList, List, X, Y),
replace(J, List, NewList, Y, X).
Examples:
?- swap(3, 2, ["You", "Are", "Awesome", "thank", "You"], L).
L = ["You", "Are", "thank", "Awesome", "You"].
?- swap(1, 4, [a,b,c,d,e], L).
L = [a, e, c, d, b].
?- swap(0, 3, [a,b,c,d,e], L).
L = [d, b, c, a, e].
?- swap(1, 0, [a,b,c,d,e], L).
L = [b, a, c, d, e].
?- swap(2, 2, [a,b,c,d,e], L).
L = [a, b, c, d, e].
?- swap(3, 9, [a,b,c,d,e], L).
false.
Let's say you have a binary search tree:
t (73, t (31, t(5,nil,nil), nil), t (101, t (83, nil, t(97,nil,nil)), t(200,nil,nil)))
which is:
73
/ \
31 101
/ / \
5 83 200
/
97
I need to write a predicate subtree(X1,X2,T) that would take 2 values from the tree (X1 and X2) and find the smallest common parent for them, and store its subtree in T.
So for the example above, if I query : subtree(83,200,X).
I should be getting back:
t(101,t(83,nil,t(97,nil,nil)),t(200,nil,nil))
which is:
101
/ \
83 200
/
97
Since 101 is the smallest common value to both of my numbers, I get that subtree back. How could I do that?
Thanks!
Here is my code for this problem. Just call
tree(X),common_ancestor(83,200,X,Y)
You will get your answer in Y.
tree3(X) :- X = t (73, t (31, t(5,nil,nil), nil), t (101, t (83, nil, t(97,nil,nil)), t(200,nil,nil))).
% Chech membership in tree:
in(X, t(X, _, _)).
in(X, t(_, L, _)):-
in(X, L).
in(X, t(_, _, R)):-
in(X, R).
% Getting subtree given the value of root
get_subtree(X, t(X, L, R),Res) :- Res = t(X,L,R).
get_subtree(X, t(_, L, _),Res):-
get_subtree(X, L,Res).
get_subtree(X, t(_, _, R),Res):-
get_subtree(X, R,Res).
% least_common_ancestor(N1, N2, Tree, Res) assignes the value of the least common ancestor to Res.
% Note that it's assumed all nodes have different values.
% Base cases: when one value is the parent of the other, then the parent is the LCA:
least_common_ancestor(N1, N2, t(N1, L, R), N1):-
(in(N2, L) ; in(N2, R)), !.
least_common_ancestor(N1, N2, t(N2, L, R), N2):-
(in(N1, L) ; in(N1, R)), !.
% If one is in the left (right) subtree and the other is in the right (left) subtree then the current root is the LCA
least_common_ancestor(N1, N2, t(X, L, R), Res):-
((in(N1, L), in(N2, R)) ; (in(N1, R), in(N2, L))), !,
Res = X.
% Otherwise, recurse to both subtrees:
least_common_ancestor(N1, N2, t(_, L, _), Res):-
least_common_ancestor(N1, N2, L, Res), !.
least_common_ancestor(N1, N2, t(_, _, R), Res):-
least_common_ancestor(N1, N2, R, Res).
% The main function
commonGP(Ka,Kb,T,ST) :-
least_common_ancestor(Ka,Kb,T,Res), get_subtree(Res,T,ST).
I'm new to Prolog and I'm trying to get my head around lists. The problem I'm struggling with is:
Given numbers in the form of lists (1 : [x], 3: [x, x, x]), implement the 'times' predicate /3.
E.g.: times([x, x], [x, x, x], R).
R = [x, x, x, x, x, x].
The plus, and successor predicates where 2 previous points of the exercise. I know I'm not using the successor predicate, but it didn't seem that useful later on.
This is what i've tried so far
successor([], [x]).
successor([X|T], R) :-
append([X|T], [X], R).
plus(L1, L2, R) :- append(L1, L2, R).
times([], _, []).
times(_, [], []).
times([_], L, L).
times(L, [_], L).
times([_|T], L2, R) :- plus(L2, R, RN),
times(T, L2, RN).
The output is:
R is [].
I think you make things too complicated here. You can define successor as:
successor(T, [x|T]).
We can define plus/3 as:
plus([], T, T).
plus([x|R], S, [x|T]) :-
plus(R, S, T).
This is more or less the implementation of append/3, except that here we check if the first list only contains x.
For times/3 we know that if the first item is empty, the result is empty:
times([], _, []).
and for a times/3 where the first item has shape [x|R], we need to add the second item to the result of a call to times/3 with R:
times([x|R], S, T) :-
times(R, S, T1),
plus(S, T1, T).
So putting it all together, we obtain:
successor(T, [x|T]).
plus([], T, T).
plus([x|R], S, [x|T]) :-
plus(R, S, T).
times([], _, []).
times([x|R], S, T) :-
times(R, S, T1),
plus(S, T1, T).
Example of the node looks like:
node(3, nil, 14).
node(14, nil, 15).
node(15, nil, 92).
I have seen similar questions asked here however mine is different as my nodes have 3 instead of 2 values in the parameter.
Example of how it should run:
?- inOrder(3, X).
X = [3, 14, 15, 35, 65, 89, 92] .
My current code is:
% the in-order traversal of a leaf is the leaf
inOrder(X, [X]) :-
node(X, nil, nil).
% if the node only has a left child, we traverse that
inOrder(x, [X|T]) :-
node(X, L, [X|T]),
inOrder(L, T).
% if the node only has a right child we traverse that
inOrder(x, [X|T]) :-
node(X, nil, R),
inOrder(R, T).
% if the node has both, we traverse them both.
inOrder(x, [X|T]) :-
node(L, X, R),
L \= nil, R \= nil,
inOrder(L, T1),
inOrder(R, T2),
append(T1, T, T2).
It returns false instead of an actual value.
There are several twists in your representation. In general, treelike structures are not flattened out in the database, here with node/3, but rather maintained in a single term.
Also, it seems to be a good idea to insist that each node has its own fact. In your example 92 needs a fact!
So given all these precautions one can write, using DCGs:
node(3, nil, 14).
node(14, nil, 15).
node(15, nil, 92).
node(92, nil, nil). % added!
inorder(I, Xs) :-
phrase(inorder(I), Xs).
inorder(nil) -->
[].
inorder(I) -->
{dif(I, nil)},
{node(I, L, R)},
inorder(L),
[I],
inorder(R).
?- inorder(3,L).
L = [3,14,15,92]
; false.
The check your database for orphaned nodes:
orphan(Nr) :-
node(_, L, R),
( Nr = L ; Nr = R ),
dif(Nr, nil),
\+ node(Nr, _, _).
So orphan(N) should fail on your database.
To get rid of the leftover choicepoint ; false. add the following rule in front of inorder//1:
inorder(Nil) --> {Nil == nil}, !.
You are using a lower-case x in your recursive cases for inOrder; that should be a variable. But that probably isn't the only problem.
I have to write out a program in Prolog that prints out letter sequences, Implement in Prolog a predicate twist/2 for ‘twisting’ pairs of entries of a list and discarding the entries in between. More precisely,
• Interchange the 1st and 2nd entries,
• Discard the 3rd entry,
• Interchange the 4th and 5th entries,
• Discard the 6th entry and so on as follows:
twist([’B’,r,a,d,f,o,r,d], T)--->
twist([’B’,r,a,d,f,o,r,d], [], y, T)--->
twist([a,d,f,o,r,d], [’B’,r], n, T)--->
twist([d,f,o,r,d], [’B’,r], y, T)--->
twist([o,r,d], [d,f,’B’,r], n, T)--->
twist([r,d], [d,f,’B’,r], y, T)--->
twist([], [r,d,d,f,’B’,r], n, T)--->
reverse([r,d,d,f,’B’,r], T)--->
T = [r,’B’,f,d,d,r] ---> success
So far I have:
twist(L,T) :-
twist(L, [], y, T). % clause 0: invoke auxiliary predicate
twist([], Acc, L) :- reverse(Acc, L),
twist(A,G,_|T), Acc, L) :- twist(T,[A,G|Acc], L),
twist([A,G], Acc, L) :- twist([],[A,G|Acc], L),
twist([], Acc, L) :- reverse(Acc, L).
I'm sure that's right but i keep getting the same error.
here's the full error message:
4 ?- twist([b, r ,a ,d ,f ,o ,r ,d], T).
ERROR: twist/2: Undefined procedure: twist/4
ERROR: However, there are definitions for:
ERROR: twist/2
Exception: (7) twist([b, r, a, d, f, o, r, d], [], y, _G2583) ?
Any help would be great.
try this:
twist([],[]).
twist([X],[X]).
twist([X,Y],[Y,X]).
twist([X,Y,_|Tail],[Y,X|NewTail]):- twist(Tail, NewTail).
And then ask:
?- twist([b, r ,a ,d ,f ,o ,r ,d], T).
T = [r, b, f, d, d, r] ;
false.
credits go to #lurker.