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