mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-20 21:49:48 +00:00
Add type annotations (#4814)
This commit is contained in:
parent
bcfca67faa
commit
abaa0d754b
@ -1,17 +1,19 @@
|
|||||||
"""
|
"""
|
||||||
disjoint set
|
Disjoint set.
|
||||||
Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
|
Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self, data):
|
def __init__(self, data: int) -> None:
|
||||||
self.data = data
|
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
|
# rank is the distance from x to its' parent
|
||||||
# root's rank is 0
|
# root's rank is 0
|
||||||
@ -19,9 +21,9 @@ def make_set(x):
|
|||||||
x.parent = x
|
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
|
set with bigger rank should be parent, so that the
|
||||||
disjoint set tree will be more flat.
|
disjoint set tree will be more flat.
|
||||||
"""
|
"""
|
||||||
@ -37,9 +39,9 @@ def union_set(x, y):
|
|||||||
y.rank += 1
|
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:
|
if x != x.parent:
|
||||||
x.parent = find_set(x.parent)
|
x.parent = find_set(x.parent)
|
||||||
@ -57,7 +59,7 @@ def find_python_set(node: Node) -> set:
|
|||||||
raise ValueError(f"{node.data} is not in {sets}")
|
raise ValueError(f"{node.data} is not in {sets}")
|
||||||
|
|
||||||
|
|
||||||
def test_disjoint_set():
|
def test_disjoint_set() -> None:
|
||||||
"""
|
"""
|
||||||
>>> test_disjoint_set()
|
>>> test_disjoint_set()
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user