fix tree algorithm