Prolog 序言:在二叉搜索树中搜索

Prolog 序言:在二叉搜索树中搜索,prolog,Prolog,我需要将列表转换为二叉搜索树,然后在该树中搜索年龄范围,并返回包含这些值的列表,还返回检查次数,以便构建输出列表 我花了两天的时间尝试这样做,但结果总是错误的 这是我最后一个借助MBRACH获得的代码: my_list( [[30,'john'], [58,'alex'], [14,'randy'], [65,'shawn'], [67,'jack']] ). construct(L,T) :- construct(L,T,nil). construct([],T,T). const

我需要将列表转换为二叉搜索树,然后在该树中搜索年龄范围,并返回包含这些值的列表,还返回检查次数,以便构建输出列表

我花了两天的时间尝试这样做,但结果总是错误的

这是我最后一个借助MBRACH获得的代码:

 my_list( [[30,'john'], [58,'alex'], [14,'randy'], [65,'shawn'], [67,'jack']] ).

 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).

 findInRange(R1, R2, T, S, N) :- find(R1, R2, T, S, N),!.

 find(_R1,_R2, nil, [], 0).
 find(R1, R2, node([Age,Name],L,R), S, N) :-
    R1 =< Age,R2 >= Age, % is the age OK (in range), if it is check left and 
    find(R1, R2, L, LL, NL),
    find(R1,R2,R,LR,NR),
    append([[Age,Name]],LL,X),
    append(X,LR,S),
    N is NL+NR+1.
 find(R1, R2, node([Age,Name],L,R), [], 0) :-
    Age > R2;Age<R1.               % if the age is greater than R2, return []
 find(R1, R2, node([Age,Name],L,R), LL, N) :-
    R1 < Age, % if the age is bigger than R1 search the left tree return LL
    find(R1,R2,L,LL,NL),
    N is NL+1.
 find(R1, R2, node([Age,Name],L,R), LR, N) :-
    R2 > Age, % if the age smaller than R1 search the right tree return LR
    find(R1,R2,R,LR,NR),
    N is NR+1.
它应该返回[14,'randy']]和支票数量


为什么它返回空列表且N=0?

我认为这会奏效。我运行了您发布的查询,得到了
S=[[30,john],[14,randy]]

my_list( [[30,'john'], [58,'alex'], [14,'randy'], [65,'shawn'], [67,'jack']] ).

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).

findInRange(R1, R2, T, S, N) :- find(R1, R2, T, S, N),!.

find(_R1,_R2, nil, [], 0).
find(R1, R2, node([Age,Name],L,R), S, N) :-
            R1 =< Age,R2 >= Age, % is the age OK (in range), if it is check left and right side
            find(R1, R2, L, LL, NL),
            find(R1,R2,R,LR,NR),
            append([[Age,Name]| LL],LR,S),
            N is NL+NR+1.

find(R1, R2, node([Age,Name],L,R), LL, N) :-
            Age > R2, % if the age is bigger than R2 search the left tree return LL
            find(R1,R2,L,LL,NL),
            N is NL+1.
find(R1, R2, node([Age,Name],L,R), LR, N) :-
            R1 > Age, % if the age smaller than R1 search the right tree return LR
            find(R1,R2,R,LR,NR),
            N is NR+1.
my_list([30,'john'],[58,'alex'],[14,'randy'],[65,'shawn'],[67,'jack'])。
构造(L,T):-构造(L,T,nil)。
构造([],T,T)。
构造([N | Ns],T,T0):-添加(N,T0,T1),构造(Ns,T,T1)。
添加(X,nil,node(X,nil,nil))。
添加(X,节点(根,L,R),节点(根,L1,R)):-X@<根,添加(X,L,L1)。
添加(X,节点(根,L,R),节点(根,L,R1)):-X@>根,添加(X,R,R1)。
FindRange(R1,R2,T,S,N):-查找(R1,R2,T,S,N),!。
查找(_R1,_R2,nil,[],0)。
查找(R1、R2、节点([年龄、名称]、L、R)、S、N):-
R1==Age%,表示年龄正常(在范围内),如果是,请检查左侧和右侧
查找(R1、R2、L、LL、NL),
查找(R1、R2、R、LR、NR),
附加([[年龄,姓名]| LL],LR,S),
N是NL+NR+1。
查找(R1,R2,节点([年龄,名称],L,R),LL,N):-
年龄>R2%,如果年龄大于R2,则搜索左树返回LL
查找(R1、R2、L、LL、NL),
N是NL+1。
查找(R1、R2、节点([年龄、名称]、L、R)、LR、N):-
R1>年龄,如果年龄小于R1,则搜索右树返回LR
查找(R1、R2、R、LR、NR),
N是NR+1。

我认为,如果运行上面显示的代码,它不会返回false。如果您尝试它,您会得到什么?它会给我一个可评估的警告“find”(\u G8472,\u G8473,node([\u G8482,\u G8485],\u G8479,\u G8480),\u G8475,\u G8476):\u G8482'不存在,它返回false。如果您运行上面的代码,它会生成一个错误,因为它不是有效的Prolog语法。你确定你正在运行的是你在上面的问题中遇到的吗?试试这个查询:my_list(Z),construct(Z,T)。T的值是多少?重复的:为什么我得到的支票数N=5?我想它一定比那个小?我们在树中有5个节点,所以我们比较每个节点的范围,看它是否合适?有5个检查是很自然的?所以二叉搜索树中的检查数将类似于一个列表?我以列表的形式输入输入,得到5个检查,这有意义吗?谢谢你的帮助,但我以列表的形式输入,得到5个检查,这有意义吗?
my_list( [[30,'john'], [58,'alex'], [14,'randy'], [65,'shawn'], [67,'jack']] ).

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).

findInRange(R1, R2, T, S, N) :- find(R1, R2, T, S, N),!.

find(_R1,_R2, nil, [], 0).
find(R1, R2, node([Age,Name],L,R), S, N) :-
            R1 =< Age,R2 >= Age, % is the age OK (in range), if it is check left and right side
            find(R1, R2, L, LL, NL),
            find(R1,R2,R,LR,NR),
            append([[Age,Name]| LL],LR,S),
            N is NL+NR+1.

find(R1, R2, node([Age,Name],L,R), LL, N) :-
            Age > R2, % if the age is bigger than R2 search the left tree return LL
            find(R1,R2,L,LL,NL),
            N is NL+1.
find(R1, R2, node([Age,Name],L,R), LR, N) :-
            R1 > Age, % if the age smaller than R1 search the right tree return LR
            find(R1,R2,R,LR,NR),
            N is NR+1.