From 7b5bf0ec3a46c6d7be7977d8686a5bed1d773eed Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 22 May 2022 00:38:48 +0200 Subject: [PATCH] fix(rbt): improve naming and add docstrings Signed-off-by: Matej Focko --- rbt.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/rbt.py b/rbt.py index 3adbf0d..8a7b7ea 100644 --- a/rbt.py +++ b/rbt.py @@ -10,12 +10,24 @@ T = TypeVar("T", bound=Comparable) class Colour(enum.IntEnum): + """ + Represents colour of the edge or node. + """ Red = 0 Black = 1 -def is_double_black(x: Optional[Node]) -> bool: - return x and 2 in Node.differences(x) +def has_double_black(x: Optional[Node]) -> bool: + """ + 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]): @@ -143,7 +155,7 @@ class RBTree(RankedTree[T]): if not node and not parent: return - while node != self.root and is_double_black(parent): + while node != self.root and has_double_black(parent): if node == parent.left: node, parent = self._delete_rebalance_step( node,