re-implement completing a tree using union-find