timimatic
timimatic

Reputation: 35

Prolog creating binary trees from list and implementing BFS for ranges

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

Upvotes: 2

Views: 2110

Answers (1)

nizarovich86
nizarovich86

Reputation: 51

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

Upvotes: 1

Related Questions