Skip to content

Commit 007f1a9

Browse files
author
AndreyGeonya
committed
add quickunion jsdoc
1 parent 98b5b4a commit 007f1a9

2 files changed

Lines changed: 53 additions & 38 deletions

File tree

‎src/sets/quickfind.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
*
88
* var QuickFind = require('path-to-algorithms/src/sets/quickfind').QuickFind;
99
*
10-
* var find = new QuickFind(10);
11-
* find.union(0, 1);
12-
* find.union(2, 1);
13-
* find.union(3, 4);
14-
* find.union(8, 9);
15-
* find.union(4, 8);
10+
* var qfind = new QuickFind(10);
11+
* qfind.union(0, 1);
12+
* qfind.union(2, 1);
13+
* qfind.union(3, 4);
14+
* qfind.union(8, 9);
15+
* qfind.union(4, 8);
1616
*
17-
* console.log(find.connected(0, 9)); // false
18-
* console.log(find.connected(3, 9)); // true
17+
* console.log(qfind.connected(0, 9)); // false
18+
* console.log(qfind.connected(3, 9)); // true
1919
*
2020
* @public
2121
* @module sets/quickfind

‎src/sets/quickunion.js‎

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,69 @@
1+
/**
2+
* Keeps track of a set of elements partitioned into a
3+
* number of disjoint (nonoverlapping) subsets.
4+
* Allows to check whether the path between two nodes exists.
5+
*
6+
* @example
7+
*
8+
* var QuickUnion = require('path-to-algorithms/' +
9+
* 'src/sets/quickunion').QuickUnion;
10+
*
11+
* var qunion = new QuickUnion(10);
12+
* qunion.union(0, 1);
13+
* qunion.union(2, 1);
14+
* qunion.union(3, 4);
15+
* qunion.union(8, 9);
16+
* qunion.union(4, 8);
17+
*
18+
* console.log(qunion.connected(0, 9)); // false
19+
* console.log(qunion.connected(3, 9)); // true
20+
*
21+
* @public
22+
* @module sets/quickunion
23+
*/
24+
125
(function (exports) {
226
'use strict';
327

428
/**
5-
* Checks whether path between two nodes exists.
6-
* The initialization has O(n) complexity.
29+
* Initialization.<br><br>
30+
* Complexity: O(N).
731
*
32+
* @public
833
* @constructor
9-
* @param {number} n Nodes count
10-
*
34+
* @param {Numner} size Count of the nodes.
1135
*/
12-
function QuickUnion(n) {
36+
exports.QuickUnion = function (n) {
1337
this._ids = [];
1438
for (var i = 0; i < n; i += 1) {
1539
this._ids[i] = i;
1640
}
17-
}
41+
};
1842

1943
/**
20-
* Finds the root of given node.
21-
* Complexity O(n).
22-
*
23-
* @param {number} i The given node
24-
* @return {number} The root of the given node
44+
* Finds the root of given node.<br><br>
45+
* Complexity: O(N).
46+
* @private
47+
* @param {Number} i The given node.
48+
* @return {Number} Root of the given node.
2549
*/
26-
QuickUnion.prototype._root = function (i) {
50+
exports.QuickUnion.prototype._root = function (i) {
2751
while (i !== this._ids[i]) {
2852
i = this._ids[i];
2953
}
3054
return i;
3155
};
3256

3357
/**
34-
* Unions two nodes.
35-
* Complexity O(n).
58+
* Connects two nodes - p and q.<br><br>
59+
* Complexity: O(N).
3660
*
37-
* @param {number} p The first node
38-
* @param {number} q The second node
61+
* @public
62+
* @method
63+
* @param {Number} p The first node.
64+
* @param {Number} q The second node.
3965
*/
40-
QuickUnion.prototype.union = function (p, q) {
66+
exports.QuickUnion.prototype.union = function (p, q) {
4167
var pRoot = this._root(p);
4268
var qRoot = this._root(q);
4369
this._ids[pRoot] = qRoot;
@@ -51,18 +77,7 @@
5177
* @param {number} q The second node.
5278
* @return {boolean} True/false depending on whether the nodes are connected.
5379
*/
54-
QuickUnion.prototype.connected = function (p, q) {
80+
exports.QuickUnion.prototype.connected = function (p, q) {
5581
return this._root(p) === this._root(q);
5682
};
57-
58-
//var union = new QuickUnion(10);
59-
//union.union(0, 1);
60-
//union.union(2, 1);
61-
//union.union(3, 4);
62-
//union.union(8, 9);
63-
//union.union(4, 8);
64-
//
65-
//console.log(union.connected(0, 9)); //expected false
66-
//console.log(union.connected(3, 9)); //expected true
67-
exports.QuickUnion = QuickUnion;
68-
}(typeof exports === 'undefined' ? window : exports));
83+
})(typeof window === 'undefined' ? module.exports : window);

0 commit comments

Comments
 (0)