working on treeapply function