我试图编写一个谓词,它接受一个列表并将其转换为平衡树。我的代码如下所示:
/* make_tree(list, tree)
*
* list: list with the elements of the tree in prefix order
* tree: balanced tree of the elements in the list
*
*/
make_tree([], empty).
make_tree([H|T], node(L, R, H)):-
split_half(T, T1, T2),
make_tree(T1, L),
make_tree(T2, R).
/* split_half(list, first, second)
*
* list: list with n elements
* first: list with first (n // 2) elements
* second: list with last (n - n // 2) elements
*
*/
split_half(L, L1, L2):-
split_half(L, L, L1, L2).
split_half(L, [], [], L):- !.
split_half(L, [_], [], L):- !.
split_half([H|T], [_,_|Acc], [H|L1], L2):-
split_half(T, Acc, L1, L2).
这在调用时有效:
?- make_tree([1,2,3], Tree).
Tree = node(node(empty, empty, 2), node(empty, empty, 3), 1).
但是以其他方式调用它时不起作用,例如:
?- make_tree(L, node(node(empty, empty, 2), node(empty, empty, 3), 1)).
false.
这并不是真的必要,但我还是接受了挑战,让它双向发挥作用。我想通过使用freeze/2
on split
, like来解决这个问题,freeze(T2, split(T, T1, T2))
这使它?- make_tree(L, node(node(empty, empty, 2), node(empty, empty, 3), 1)).
起作用,但原来的想法不再适用。所以实际上我正在寻找的是某种freeze/2
可以做类似的事情freeze((T;T2), split(T, T1, T2))
。有谁知道如何解决这个问题?
提前致谢