2
我試圖在序言中創建一個二叉搜索樹,我可以搜索範圍的分數並返回該範圍內的主題;並且在搜索時也獲得訪問的節點。但我堅持嘗試創建二叉樹。任何幫助將不勝感激,謝謝!Prolog從列表中創建二叉樹並實現範圍的BFS
my_list([Mathematics, 100, English, 60, Physics, 90, Chemistry, 65, Biology, 80, Geography, 69, Yoruba, 50, FMathematics, 30, Agric, 80]).
remove_even_order([],[]).
remove_even_order([Head_ODD|[]],[]).
remove_even_order([Head_ODD,Head_EVEN|Tail],[Head_EVEN|ResultTail]) :- remove_even_order(Tail,ResultTail).
construct(L,T) :- construct(L,T,nil).
construct([],T,T).
construct([N|Ns],T,T0) :- add(N,T0,T1),construct(Ns,T,T1).
add(X, nil, node(X, nil, nil)).
add(X, node(Root, L, R),node(Root, L1, R)) :- X @< Root, add(X, L, L1).
add(X, node(Root, L, R),node(Root, L, R1)) :- X @> Root, add(X, R, R1).
show(T) :- show1(T, 0).
show1(nil, _).
show1(node(N, L, R), Indent) :-
Indent2 is Indent + 4,
show1(R, Indent2),
tab(Indent),
write_ln(N),
show1(L, Indent2).
main :-
my_list(Z),
remove_even_order(Z, R),
construct(R, T),
show(T).
的問題是,在列表中雙擊值(80)!...謝謝
我已經編輯了這個問題......但非常感謝幫助...;) – timimatic