genequery based on binary tree in database