File tree 1 file changed +11
-9
lines changed
data_structures/disjoint_set
1 file changed +11
-9
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
- disjoint set
2
+ Disjoint set.
3
3
Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
4
4
"""
5
5
6
6
7
7
class Node :
8
- def __init__ (self , data ) :
8
+ def __init__ (self , data : int ) -> None :
9
9
self .data = data
10
+ self .rank : int
11
+ self .parent : Node
10
12
11
13
12
- def make_set (x ) :
14
+ def make_set (x : Node ) -> None :
13
15
"""
14
- make x as a set.
16
+ Make x as a set.
15
17
"""
16
18
# rank is the distance from x to its' parent
17
19
# root's rank is 0
18
20
x .rank = 0
19
21
x .parent = x
20
22
21
23
22
- def union_set (x , y ) :
24
+ def union_set (x : Node , y : Node ) -> None :
23
25
"""
24
- union two sets.
26
+ Union of two sets.
25
27
set with bigger rank should be parent, so that the
26
28
disjoint set tree will be more flat.
27
29
"""
@@ -37,9 +39,9 @@ def union_set(x, y):
37
39
y .rank += 1
38
40
39
41
40
- def find_set (x ) :
42
+ def find_set (x : Node ) -> Node :
41
43
"""
42
- return the parent of x
44
+ Return the parent of x
43
45
"""
44
46
if x != x .parent :
45
47
x .parent = find_set (x .parent )
@@ -57,7 +59,7 @@ def find_python_set(node: Node) -> set:
57
59
raise ValueError (f"{ node .data } is not in { sets } " )
58
60
59
61
60
- def test_disjoint_set ():
62
+ def test_disjoint_set () -> None :
61
63
"""
62
64
>>> test_disjoint_set()
63
65
"""
You can’t perform that action at this time.
0 commit comments