added merge tree algorithm