1

如何检查元素是否在二叉树中?例如,

tree1(node(10,empty, node(100,empty, empty))).
tree2(node(~, node(+, empty, node(*, empty, empty)), node(!, node(/,empty, empty),empty))).
badTree( node(3, node(5, empty), empty)) .

?- tree2(T), memberT(!,T).
T = node(~, node(+, empty, node(*, empty, empty)), node(!, node(/, empty, empty), empty)) .
?- tree1(T),memberT(100,T).
T = node(10, empty, node(100, empty, empty)) .
4

1 回答 1

1

不确定这是否正是您的问题,但这是生成您给出的结果的代码(假设它们是规范):

memberT(Val, node( Val,  _Left, _Right)).
memberT(Val, node(_Val1,  Left, _Right)) :- memberT(Val, Left).
memberT(Val, node(_Val1, _Left,  Right)) :- memberT(Val, Right).
于 2013-02-28T10:22:00.820 回答