A good book on algorithms would help. There's one I have from MIT press that devotes a lot of space to graphs (trees). I believe it's named "Algorithms" or something like that.
You could also ask in the general
Java forums, since running in a serlvet environment isn't going to make any difference.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.