Union Find
Last updated
Last updated
// Do not edit the class below except for
// the constructor and the createSet, find,
// and union methods. Feel free to add new
// properties and methods to the class.
class UnionFind {
constructor() {
// Write your code here.
this.arr = [];
this.weights = [];
}
createSet(value) {
this.arr[value] = value;
this.weights[value] = 0;
return null;
}
// find(value) {
// let currentValue = value;
// while (this.arr[currentValue] !== undefined) {
// if (this.arr[currentValue] === currentValue) {
// return currentValue;
// }
// currentValue = this.arr[currentValue];
// }
// return null;
// }
find(value) {
if (value === undefined)
return null;
if (this.arr[value] === value)
return value;
return this.arr[value] = this.find(this.arr[value]);
}
union(valueOne, valueTwo) {
// Write your code here.
const root1 = this.find(valueOne);
const root2 = this.find(valueTwo);
if (root1 === root2) return null;
if (root1 === null || root2 === null) return null;
// this.arr[root1] = root2;
console.log(
{
root1,
root2,
}
)
/ using weights
const weight1 = this.weights[root1];
const weight2 = this.weights[root2];
if (weight1 >= weight2) {
this.arr[root1] = root2;
} else {
this.arr[root2] = root1;
}
return null;
}
}
// Do not edit the line below.
exports.UnionFind = UnionFind;