Remove unnecessary traverse code and add DisjointSet data structure