do the very same you do in C++ or Java.
There are very high chances you'll got the right result, assuming that (in C++, to say), you wrote the minimal code required. That account usually for a recursive definition: something like
// pseudocode - Elem must implement operator<
struct BST<Elem> {
bool find(const Elem e) { return find(root, e) }
private:
bool find(Node n, Elem e) {
if (!n) return false;
if (n.payload() == e) return true;
return n.payload() < e ? find(n.left) : find(n.right);
}
}
The same search can be written manually unfolding the stack, by means of (duh) stack, then iterating
stack<Node> stack; stack.push(root);
while (!stack.isEmpty()) {
Node n = stack.pop();
// check found, return true
// push left or right...
}
In Prolog the stack is 'embedded' in the language: just state what's the solution 'form': assuming a tree like t(Payload, Left, Right) with a default like t(0,-,-) you can do
% note: untested
bst(t(Payload,_,_), Payload). % found
bst(t(Payload,L,R), Sought) :- Sought @< Payload -> bst(L, Sought) ; bst(R, Sought).
A note on C++: I've uploaded on github (at loqt) an example of a declarative, non intrusive C++ interface on a raw pointer model (Agraph_t* and friends). With that interface, very simple to implement now that we have lambda in C++:
// live code here
void lqXDotScene::dump(QString m) const {
qDebug() << m;
cg->depth_first([&](Gp t) { qDebug() << "graph" << gvname(t) << CVP(find_graph(t)); });
qDebug() << "nodes";
cg->for_nodes([&](Np n) {
qDebug() << "node" << gvname(n) << CVP(find_node(n));
cg->for_edges_out(n, [&](Ep e) {
qDebug() << "edge" << gvname(e) << CVP(find_edge(e)) << "to" << gvname(e->node) << CVP(find_node(e->node));
});
});
}
the line
cg->depth_first([&](Gp t) { qDebug() << "graph" << gvname(t) << CVP(find_graph(t));
does a full visit. find_graph, find_ etc are member function: lambda it's very powerful.