rb-tree #5
1 changed files with 7 additions and 7 deletions
14
rbt.py
14
rbt.py
|
@ -61,7 +61,7 @@ class RBTree(RankedTree[T]):
|
|||
# Case 1
|
||||
# ======
|
||||
# z’s uncle y is red
|
||||
pp.rank += 1
|
||||
pp.rank += Colour.Black
|
||||
z = pp
|
||||
elif z == right_child:
|
||||
# Case 2
|
||||
|
@ -105,33 +105,33 @@ class RBTree(RankedTree[T]):
|
|||
rotate_left: RotateFunction,
|
||||
rotate_right: RotateFunction,
|
||||
) -> Tuple[Optional[Node[T]], Optional[Node[T]]]:
|
||||
if Node.difference(w) == 0:
|
||||
if Node.difference(w) == Colour.Red:
|
||||
# Case 1
|
||||
# ======
|
||||
# x’s sibling w is red
|
||||
rotate_left(tree=self, x=parent)
|
||||
w = right(parent)
|
||||
|
||||
if Node.differences(w) == (1, 1):
|
||||
if Node.differences(w) == (Colour.Black, Colour.Black):
|
||||
# Case 2
|
||||
# ======
|
||||
# x’s sibling w is black, and both of w’s children are black
|
||||
parent.rank -= 1
|
||||
parent.rank -= Colour.Black
|
||||
x = parent
|
||||
else:
|
||||
# Case 3
|
||||
# ======
|
||||
# x’s sibling w is black,
|
||||
# w’s left child is red, and w’s right child is black
|
||||
if Node.difference(right(w), w) == 1:
|
||||
if Node.difference(right(w), w) == Colour.Black:
|
||||
rotate_right(tree=self, x=w)
|
||||
w = right(parent)
|
||||
|
||||
# Case 4
|
||||
# ======
|
||||
# x’s sibling w is black, and w’s right child is red
|
||||
parent.rank -= 1
|
||||
w.rank += 1
|
||||
parent.rank -= Colour.Black
|
||||
w.rank += Colour.Black
|
||||
rotate_left(tree=self, x=parent)
|
||||
x = self.root
|
||||
|
||||
|
|
Loading…
Reference in a new issue