General Tree Search Algorithm in Java