我试图通过inorder travel方法浏览二叉树 我的目标是找到树中特定键的出现 例如, 我有以下树:
t(t(t(nil,"d",t(nil,"g",nil)),"b",t(nil,"e",nil)),"a",t(t(nil,"f",t(nil,"h",nil)),"c",nil))
当我将使用我的inorder_finder时,我会得到以下内容: 对于“c”我会得到8 对于“d”我会得到1 因为“w”我会得到-1
我已经达到了以下代码:
inorder_finder(nil,_,_,0).
inorder_place(t(_,X,_),X,Count,Place) :-
Place is Count+1.
inorder_place(t(L,_,R),Wanted,Count,Place) :-
inorder_place(L,Wanted,Count+1,Place),
Place<1,
inorder_place(R,Wanted,Count+1,Place),
Place<1,
Count = Count+1.
我调用以下谓词:
inorder_finder inorder_place(t(t(t(nil,"d",t(nil,"g",nil)),"b",t(nil,"e",nil)),"a",t(t(nil,"f",t(nil,"h",nil)),"c",nil)),"c",1,Place)
但此刻它不起作用。 (只是总是返回false) 任何想法?
更新:我已经根据我得到的评论更新了代码 - 它仍然返回false并且不能正常工作
答案 0 :(得分:3)
评论中提到了最明显的错误,仍然存在的是:
Place<1
:我不明白为什么?地方的价值可能大于1。inorder_place
:从未定义过...... inorder_place(t(_,X,_),X,Count,Place):-Place is Count+1.
:即使你找到了你需要的信,首先递归地枚举树的左分支然后放置想要的字母(见下面的答案)inorder_finder inorder_place(...)
:仍然没有用于调用谓词的有效语法 - 它应该返回错误而不是false ... 我的实施:
inorder_finder(nil,_,Count,Count,-1).
inorder_finder(t(L,X,_),X,Count,Count2,Place):-
inorder_finder(L,X,Count,Count3,_),
Place is Count3+1,Count2 is Place.
inorder_finder(t(L,X,R),Wanted,Count,Count2,Place):-
dif(X,Wanted),
inorder_finder(L,Wanted,Count,Count3,Place1),
Count4 is Count3+1,
inorder_finder(R,Wanted,Count4,Count2,Place2),
Place is max(Place1,Place2).
示例:
?- inorder_finder(t(t(t(nil,"d",t(nil,"g",nil)),"b",t(nil,"e",nil)),"a",t(t(nil,"f",t(nil,"h",nil)),"c",nil)),"c",0,_,P).
P = 8 ;
false.
?- inorder_finder(t(t(t(nil,"d",t(nil,"g",nil)),"b",t(nil,"e",nil)),"a",t(t(nil,"f",t(nil,"h",nil)),"c",nil)),"W",0,_,P).
P = -1.
?- inorder_finder(t(t(t(nil,"d",t(nil,"g",nil)),"b",t(nil,"e",nil)),"a",t(t(nil,"f",t(nil,"h",nil)),"c",nil)),"d",0,_,P).
P = 1 ;
false.