Refactored code.

Move tree building code to QueryTree class
Tested for Java SE 1.7