completed method for constructing group tree