tree algorithm