如何检查元素是否在二叉树中?例如,
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)) .
答案 0 :(得分: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).