Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f0494e

Browse files
committed
Update quickunion
1 parent 6e0da7f commit 3f0494e

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

src/sets/quickunion.js

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,64 @@
1-
/**
2-
* Checks whether path between two nodes exists.
3-
* The initialization has O(n) complexity.
4-
*
5-
* @constructor
6-
* @param {number} n Nodes count
7-
*
8-
*/
9-
function QuickUnion(n) {
10-
this._ids = [];
11-
for (var i = 0; i < n; i += 1) {
12-
this._ids[i] = i;
1+
(function (exports) {
2+
/**
3+
* Checks whether path between two nodes exists.
4+
* The initialization has O(n) complexity.
5+
*
6+
* @constructor
7+
* @param {number} n Nodes count
8+
*
9+
*/
10+
function QuickUnion(n) {
11+
this._ids = [];
12+
for (var i = 0; i < n; i += 1) {
13+
this._ids[i] = i;
14+
}
1315
}
14-
}
1516

16-
/**
17-
* Finds the root of given node.
18-
* Complexity O(n).
19-
*
20-
* @param {number} i The given node
21-
* @return {number} The root of the given node
22-
*/
23-
QuickUnion.prototype._root = function (i) {
24-
while (i !== this._ids[i]) i = this._ids[i];
25-
return i;
26-
};
17+
/**
18+
* Finds the root of given node.
19+
* Complexity O(n).
20+
*
21+
* @param {number} i The given node
22+
* @return {number} The root of the given node
23+
*/
24+
QuickUnion.prototype._root = function (i) {
25+
while (i !== this._ids[i]) i = this._ids[i];
26+
return i;
27+
};
2728

28-
/**
29-
* Unions two nodes.
30-
* Complexity O(n).
31-
*
32-
* @param {number} p The first node
33-
* @param {number} q The second node
34-
*/
35-
QuickUnion.prototype.union = function (p, q) {
36-
var pRoot = this._root(p),
37-
qRoot = this._root(q);
38-
this._ids[pRoot] = qRoot;
39-
};
29+
/**
30+
* Unions two nodes.
31+
* Complexity O(n).
32+
*
33+
* @param {number} p The first node
34+
* @param {number} q The second node
35+
*/
36+
QuickUnion.prototype.union = function (p, q) {
37+
var pRoot = this._root(p),
38+
qRoot = this._root(q);
39+
this._ids[pRoot] = qRoot;
40+
};
4041

41-
/**
42-
* Checks whether two nodes are connected.
43-
* Complexity O(n).
44-
*
45-
* @param {number} p The first node.
46-
* @param {number} q The second node.
47-
* @return {boolean} True/false depending on whether the nodes are connected.
48-
*/
49-
QuickUnion.prototype.connected = function (p, q) {
50-
return this._root(p) === this._root(q);
51-
};
42+
/**
43+
* Checks whether two nodes are connected.
44+
* Complexity O(n).
45+
*
46+
* @param {number} p The first node.
47+
* @param {number} q The second node.
48+
* @return {boolean} True/false depending on whether the nodes are connected.
49+
*/
50+
QuickUnion.prototype.connected = function (p, q) {
51+
return this._root(p) === this._root(q);
52+
};
5253

53-
//var union = new QuickUnion(10);
54-
//union.union(0, 1);
55-
//union.union(2, 1);
56-
//union.union(3, 4);
57-
//union.union(8, 9);
58-
//union.union(4, 8);
59-
//
60-
//console.log(union.connected(0, 9)); //expected false
61-
//console.log(union.connected(3, 9)); //expected true
54+
//var union = new QuickUnion(10);
55+
//union.union(0, 1);
56+
//union.union(2, 1);
57+
//union.union(3, 4);
58+
//union.union(8, 9);
59+
//union.union(4, 8);
60+
//
61+
//console.log(union.connected(0, 9)); //expected false
62+
//console.log(union.connected(3, 9)); //expected true
63+
exports.QuickUnion = QuickUnion;
64+
}(typeof exports === 'undefined' ? window : exports));

0 commit comments

Comments
 (0)