little-ds-toolkit
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -104,4 +104,5 @@ | ||
Heap.prototype.pop = function () { | ||
if (this.data.length === 0) return; | ||
var root = this.data[0]; | ||
if (typeof root === 'undefined') return; | ||
this.onMove(this.data[0], undefined, 0); | ||
@@ -108,0 +109,0 @@ |
@@ -24,3 +24,3 @@ function UnionFind(data) { | ||
var leader2 = that.find(); | ||
if (leader1 === leader2) return; // already in same group | ||
if (leader1 === leader2) return leader1; // already in same group | ||
@@ -31,11 +31,26 @@ if (leader1.rank === leader2.rank) { | ||
leader2.rank = leader1.rank + 1; | ||
return leader2; | ||
} | ||
if (leader1.rank > leader2.rank) { | ||
leader2.leader = leader1; // the shallow should get installed under the deep | ||
return leader1; | ||
} | ||
else { | ||
leader1.leader = leader2; | ||
} | ||
leader1.leader = leader2; | ||
return leader2; | ||
}; | ||
/* | ||
class methods | ||
*/ | ||
UnionFind.union = function (item1, item2) { | ||
item1 = item1 instanceof UnionFind ? item1 : new UnionFind(item1); | ||
item2 = item2 instanceof UnionFind ? item2 : new UnionFind(item2); | ||
return item1.union(item2); | ||
}; | ||
UnionFind.find = function (item) { | ||
item = item instanceof UnionFind ? item : new UnionFind(item); | ||
return item.find(); | ||
}; | ||
module.exports = UnionFind; |
{ | ||
"name": "little-ds-toolkit", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "A small collection of useful data structures", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -142,11 +142,18 @@ little-ds-toolkit | ||
```js | ||
item.union(item2); // almost Θ(1) (grows very slowly) | ||
item.union(item2); // almost O(1) (grows very slowly) | ||
``` | ||
and find: | ||
```js | ||
item.find(); // almost Θ(1) (grows very slowly) | ||
item.find(); // almost O(1) (grows very slowly) | ||
``` | ||
The find returns the element "leader". The important part is that 2 elements with the same leader belongs to the same group. | ||
Both "find" and "union" return the "leader" element. The important part is that 2 elements with the same leader belong to the same set. | ||
You can retrieve the original value with "item.data". | ||
The object contains 2 useful helper functions "union" and "find" that runs on both UnionFind instances or other values. | ||
```js | ||
// the arguments can be any value (or a UnionFind instance) | ||
var leader = UnionFind.find(item); | ||
var leader = UnionFind.union(item1, item2); | ||
``` | ||
lru-cache | ||
@@ -153,0 +160,0 @@ ========= |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39785
982
205