working on printing the tree and optimize function