Refactoring and added QuickUnion
Refactoring