finish buildTreeDynamic and test the buildTree function, need more cases
to test