fix(rbt): improve naming and add docstrings

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-22 00:38:48 +02:00
parent 2b02eac10e
commit 7b5bf0ec3a
Signed by: mfocko
GPG key ID: 7C47D46246790496

18
rbt.py
View file

@ -10,12 +10,24 @@ T = TypeVar("T", bound=Comparable)
class Colour(enum.IntEnum): class Colour(enum.IntEnum):
"""
Represents colour of the edge or node.
"""
Red = 0 Red = 0
Black = 1 Black = 1
def is_double_black(x: Optional[Node]) -> bool: def has_double_black(x: Optional[Node]) -> bool:
return x and 2 in Node.differences(x) """
Checks for double black child of x.
Args:
x: Node to be checked.
Returns:
`true`, if `x` has a double black node, `false` otherwise.
"""
return x is not None and 2 in Node.differences(x)
class RBTree(RankedTree[T]): class RBTree(RankedTree[T]):
@ -143,7 +155,7 @@ class RBTree(RankedTree[T]):
if not node and not parent: if not node and not parent:
return return
while node != self.root and is_double_black(parent): while node != self.root and has_double_black(parent):
if node == parent.left: if node == parent.left:
node, parent = self._delete_rebalance_step( node, parent = self._delete_rebalance_step(
node, node,