added code to rebalance the tree