Reputation: 35
I am trying to search for ranges in a binary search tree. but this code returns false. I'm not sure where the problem is.
mytree1(node(5, node(3, nil, nil),
node(8, node(7, nil, nil),
node(9, nil, nil)))).
findExamsInRange(X,Y, T) :- find(X,Y,T).
find(X,Y, node(X, _, _)).
find(X,Y, node(N, L, _)) :- N > X,
between(X,Y,N),
find(X,Y,L), append(N, V).
find(X, node(N, _, R)) :- N < X,
between(X,Y,N),
find(X,Y,R), append(N, V).
Upvotes: 1
Views: 307
Reputation: 51
hey there timimatic ! findExamsInRange(X,Y, T) :- find(X,Y,T). First this one is just not necessary since both findExamsInRange and find seems to have the same aim. so why create a new predicate?
after going through your code... i see that you are appending the chosen nodes to a list called 'V'. but you are not passing it as a result anywhere through your main predicate (find). there are also many things that you need to fix ... you are not handeling the cases where you reach a leaf of a tree and have a node with two nils. you are having two different cases one you are going right and another you are going left... but matter of fact if your node is IN range you should go to both right and left. how about the cases that don't satisfy the range? you should think about those and include them... or you will get a 'false' for not having a case for them. find(X,Y, node(X, _, _)). this line of code is useless... i suggest you remove it. so to wrap up... i think it is best if you add an argument to your predicate to pass the list in ( as a start) and after that you need to add some cases for the nil cases that indicates the end of your tree.
Upvotes: 1