Skip to content

Commit a93bc91

Browse files
committed
improve(findset): add new member func 'initNode' to init a specify node in the findset
1 parent 5b8c25f commit a93bc91

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

Diff for: packages/findset/__test__/findset.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ describe('createfindset', function () {
3131
expect(findset.root(2)).toBe(findset.root(3))
3232

3333
for (let i = 4; i <= MAX_N; ++i) expect(findset.root(i)).toBe(i)
34+
35+
findset.initNode(2)
36+
findset.initNode(3)
37+
expect(findset.root(2)).toBe(2)
38+
expect(findset.root(3)).toBe(3)
3439
})
3540

3641
test('out of boundary', function () {
@@ -69,6 +74,13 @@ describe('createHeuristicfindset', function () {
6974
expect(findset.root(2)).toBe(findset.root(3))
7075

7176
for (let i = 4; i <= MAX_N; ++i) expect(findset.root(i)).toBe(i)
77+
78+
findset.initNode(2)
79+
findset.initNode(3)
80+
expect(findset.root(2)).toBe(2)
81+
expect(findset.size(2)).toBe(1)
82+
expect(findset.root(3)).toBe(3)
83+
expect(findset.size(3)).toBe(1)
7284
})
7385

7486
test('out of boundary', function () {

Diff for: packages/findset/src/enhanced.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@ import type { Findset } from './ordinary'
1414
* findset.merge(2)
1515
*/
1616
export interface EnhancedFindset extends Findset {
17-
/**
18-
*
19-
* @param x
20-
*/
21-
initNode(x: number): void
2217
/**
2318
* Size (number of the nodes) of the tree which x belongs.
2419
* @param x
2520
*/
2621
size(x: number): number
2722
/**
28-
*
23+
* Get the set which contains all nodes of the tree which x belongs.
2924
* @param x
3025
*/
3126
getSetOf(x: number): Readonly<Set<number>> | undefined

Diff for: packages/findset/src/heuristic.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function createHeuristicFindset(): HeuristicFindset {
2626
const count: number[] = []
2727

2828
let _size = 0
29-
return { init, root, merge, size }
29+
return { init, initNode, root, merge, size }
3030

3131
function init(N: number): void {
3232
_size = N
@@ -38,6 +38,11 @@ export function createHeuristicFindset(): HeuristicFindset {
3838
}
3939
}
4040

41+
function initNode(x: number): void {
42+
parent[x] = x
43+
count[x] = 1
44+
}
45+
4146
function root(x: number): number | never {
4247
if (x < 1 || x > _size)
4348
throw new RangeError(`Out of boundary [1, ${_size}]. x: ${x}`)

Diff for: packages/findset/src/ordinary.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
*/
1515
export interface Findset {
1616
/**
17-
* Initialize the findSet.
17+
* Initialize the findset .
1818
* @param N
1919
*/
2020
init(N: number): void
21+
/**
22+
* Initialize a specified node in the findset.
23+
* @param x
24+
*/
25+
initNode(x: number): void
2126
/**
2227
* Find the root element of x.
2328
* @param x
@@ -39,14 +44,18 @@ export interface Findset {
3944
export function createFindset(): Findset {
4045
const parent: number[] = []
4146
let _size = 0
42-
return { init, root, merge }
47+
return { init, initNode, root, merge }
4348

4449
function init(N: number): void {
4550
_size = N
4651
if (parent.length <= _size) parent.length = _size + 1
4752
for (let i = 1; i <= N; ++i) parent[i] = i
4853
}
4954

55+
function initNode(x: number): void {
56+
parent[x] = x
57+
}
58+
5059
function root(x: number): number {
5160
if (x < 1 || x > _size)
5261
throw new RangeError(`Out of boundary [1, ${_size}]. x: ${x}`)

0 commit comments

Comments
 (0)