Skip to content

Commit 843893e

Browse files
committedMay 4, 2018
Add disjoint set.
1 parent 2e76caa commit 843893e

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed
 

‎src/data-structures/disjoint-set/DisjointSet.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default class DisjointSet {
2525
}
2626

2727
/**
28+
* Find set representation node.
29+
*
2830
* @param {*} itemValue
2931
* @return {(string|null)}
3032
*/
@@ -42,6 +44,8 @@ export default class DisjointSet {
4244
}
4345

4446
/**
47+
* Union by rank.
48+
*
4549
* @param {*} valueA
4650
* @param {*} valueB
4751
* @return {DisjointSet}
@@ -62,7 +66,7 @@ export default class DisjointSet {
6266
const rootA = this.items[rootKeyA];
6367
const rootB = this.items[rootKeyB];
6468

65-
if (rootA.getAncestorsCount() < rootB.getAncestorsCount()) {
69+
if (rootA.getRank() < rootB.getRank()) {
6670
// If rootB's tree is bigger then make rootB to be a new root.
6771
rootB.addChild(rootA);
6872

‎src/data-structures/disjoint-set/DisjointSetItem.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,27 @@ export default class DisjointSetItem {
3939
}
4040

4141
/**
42+
* Rank basically means the number of all ancestors.
43+
*
4244
* @return {number}
4345
*/
44-
getAncestorsCount() {
46+
getRank() {
4547
if (this.getChildren().length === 0) {
4648
return 0;
4749
}
4850

49-
let count = 0;
51+
let rank = 0;
5052

5153
/** @var {DisjointSetItem} child */
5254
this.getChildren().forEach((child) => {
5355
// Count child itself.
54-
count += 1;
56+
rank += 1;
5557

5658
// Also add all children of current child.
57-
count += child.getAncestorsCount();
59+
rank += child.getRank();
5860
});
5961

60-
return count;
62+
return rank;
6163
}
6264

6365
/**

‎src/data-structures/disjoint-set/__test__/DisjointSetItem.test.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('DisjointSetItem', () => {
77
const itemC = new DisjointSetItem('C');
88
const itemD = new DisjointSetItem('D');
99

10-
expect(itemA.getAncestorsCount()).toBe(0);
10+
expect(itemA.getRank()).toBe(0);
1111
expect(itemA.getChildren()).toEqual([]);
1212
expect(itemA.getKey()).toBe('A');
1313
expect(itemA.getRoot()).toEqual(itemA);
@@ -17,11 +17,11 @@ describe('DisjointSetItem', () => {
1717
itemA.addChild(itemB);
1818
itemD.setParent(itemC);
1919

20-
expect(itemA.getAncestorsCount()).toBe(1);
21-
expect(itemC.getAncestorsCount()).toBe(1);
20+
expect(itemA.getRank()).toBe(1);
21+
expect(itemC.getRank()).toBe(1);
2222

23-
expect(itemB.getAncestorsCount()).toBe(0);
24-
expect(itemD.getAncestorsCount()).toBe(0);
23+
expect(itemB.getRank()).toBe(0);
24+
expect(itemD.getRank()).toBe(0);
2525

2626
expect(itemA.getChildren().length).toBe(1);
2727
expect(itemC.getChildren().length).toBe(1);
@@ -50,9 +50,9 @@ describe('DisjointSetItem', () => {
5050
expect(itemC.isRoot()).toBeFalsy();
5151
expect(itemD.isRoot()).toBeFalsy();
5252

53-
expect(itemA.getAncestorsCount()).toEqual(3);
54-
expect(itemB.getAncestorsCount()).toEqual(0);
55-
expect(itemC.getAncestorsCount()).toEqual(1);
53+
expect(itemA.getRank()).toEqual(3);
54+
expect(itemB.getRank()).toEqual(0);
55+
expect(itemC.getRank()).toEqual(1);
5656
});
5757

5858
it('should do basic manipulation with disjoint set item with custom key extractor', () => {
@@ -65,7 +65,7 @@ describe('DisjointSetItem', () => {
6565
const itemC = new DisjointSetItem({ key: 'C', value: 3 }, keyExtractor);
6666
const itemD = new DisjointSetItem({ key: 'D', value: 4 }, keyExtractor);
6767

68-
expect(itemA.getAncestorsCount()).toBe(0);
68+
expect(itemA.getRank()).toBe(0);
6969
expect(itemA.getChildren()).toEqual([]);
7070
expect(itemA.getKey()).toBe('A');
7171
expect(itemA.getRoot()).toEqual(itemA);
@@ -75,11 +75,11 @@ describe('DisjointSetItem', () => {
7575
itemA.addChild(itemB);
7676
itemD.setParent(itemC);
7777

78-
expect(itemA.getAncestorsCount()).toBe(1);
79-
expect(itemC.getAncestorsCount()).toBe(1);
78+
expect(itemA.getRank()).toBe(1);
79+
expect(itemC.getRank()).toBe(1);
8080

81-
expect(itemB.getAncestorsCount()).toBe(0);
82-
expect(itemD.getAncestorsCount()).toBe(0);
81+
expect(itemB.getRank()).toBe(0);
82+
expect(itemD.getRank()).toBe(0);
8383

8484
expect(itemA.getChildren().length).toBe(1);
8585
expect(itemC.getChildren().length).toBe(1);
@@ -108,8 +108,8 @@ describe('DisjointSetItem', () => {
108108
expect(itemC.isRoot()).toBeFalsy();
109109
expect(itemD.isRoot()).toBeFalsy();
110110

111-
expect(itemA.getAncestorsCount()).toEqual(3);
112-
expect(itemB.getAncestorsCount()).toEqual(0);
113-
expect(itemC.getAncestorsCount()).toEqual(1);
111+
expect(itemA.getRank()).toEqual(3);
112+
expect(itemB.getRank()).toEqual(0);
113+
expect(itemC.getRank()).toEqual(1);
114114
});
115115
});

0 commit comments

Comments
 (0)
Please sign in to comment.