2

I am trying to create a binary search tree in prolog that i can search for a range of scores and return the subjects in that range; and also get the nodes visited while searching. but i am stuck trying to create the binary tree. Any help would be appreciated, thanks!

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

the problem was with the double values(80) in the list!...thanks

1 Answer 1

1

your list has 2 80s... and you don't have a case/method to handle this in your construct predicate. hope this helps ;)

Sign up to request clarification or add additional context in comments.

1 Comment

i already edited the question...but thanks alot for the help...;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.