add tree algorithm