Refactoring the code with methods from WeightedQuickUnionUF