Skip to content

[mypy] Added type annotations to disjoint_set.py #4814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions data_structures/disjoint_set/disjoint_set.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
"""
disjoint set
Disjoint set.
Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
"""


class Node:
def __init__(self, data):
def __init__(self, data: int) -> None:
self.data = data
self.rank: int
self.parent: Node


def make_set(x):
def make_set(x: Node) -> None:
"""
make x as a set.
Make x as a set.
"""
# rank is the distance from x to its' parent
# root's rank is 0
x.rank = 0
x.parent = x


def union_set(x, y):
def union_set(x: Node, y: Node) -> None:
"""
union two sets.
Union of two sets.
set with bigger rank should be parent, so that the
disjoint set tree will be more flat.
"""
Expand All @@ -37,9 +39,9 @@ def union_set(x, y):
y.rank += 1


def find_set(x):
def find_set(x: Node) -> Node:
"""
return the parent of x
Return the parent of x
"""
if x != x.parent:
x.parent = find_set(x.parent)
Expand All @@ -57,7 +59,7 @@ def find_python_set(node: Node) -> set:
raise ValueError(f"{node.data} is not in {sets}")


def test_disjoint_set():
def test_disjoint_set() -> None:
"""
>>> test_disjoint_set()
"""
Expand Down