Skip to content

Commit abaa0d7

Browse files
authored
Add type annotations (#4814)
1 parent bcfca67 commit abaa0d7

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

data_structures/disjoint_set/disjoint_set.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
"""
2-
disjoint set
2+
Disjoint set.
33
Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
44
"""
55

66

77
class Node:
8-
def __init__(self, data):
8+
def __init__(self, data: int) -> None:
99
self.data = data
10+
self.rank: int
11+
self.parent: Node
1012

1113

12-
def make_set(x):
14+
def make_set(x: Node) -> None:
1315
"""
14-
make x as a set.
16+
Make x as a set.
1517
"""
1618
# rank is the distance from x to its' parent
1719
# root's rank is 0
1820
x.rank = 0
1921
x.parent = x
2022

2123

22-
def union_set(x, y):
24+
def union_set(x: Node, y: Node) -> None:
2325
"""
24-
union two sets.
26+
Union of two sets.
2527
set with bigger rank should be parent, so that the
2628
disjoint set tree will be more flat.
2729
"""
@@ -37,9 +39,9 @@ def union_set(x, y):
3739
y.rank += 1
3840

3941

40-
def find_set(x):
42+
def find_set(x: Node) -> Node:
4143
"""
42-
return the parent of x
44+
Return the parent of x
4345
"""
4446
if x != x.parent:
4547
x.parent = find_set(x.parent)
@@ -57,7 +59,7 @@ def find_python_set(node: Node) -> set:
5759
raise ValueError(f"{node.data} is not in {sets}")
5860

5961

60-
def test_disjoint_set():
62+
def test_disjoint_set() -> None:
6163
"""
6264
>>> test_disjoint_set()
6365
"""

0 commit comments

Comments
 (0)