2022-01-30 15:03:34 +01:00
|
|
|
from avl import AVLTree
|
|
|
|
from node import Node, NodeType, Comparable, RotateFunction
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
import logging
|
2022-01-30 15:03:34 +01:00
|
|
|
from typing import TypeVar, Optional
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2022-01-30 15:03:34 +01:00
|
|
|
T = TypeVar("T", bound=Comparable)
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
|
2022-01-30 15:03:34 +01:00
|
|
|
class WAVLTree(AVLTree[T]):
|
|
|
|
def is_correct_node(
|
|
|
|
self, node: Optional[Node[T]], recursive: bool = True
|
|
|
|
) -> bool:
|
2021-01-04 10:46:12 +01:00
|
|
|
if not node:
|
|
|
|
return True
|
|
|
|
|
|
|
|
for child_rank in Node.differences(node):
|
|
|
|
if child_rank not in (1, 2):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if node.type == NodeType.LEAF:
|
|
|
|
return node.rank == 0
|
|
|
|
|
|
|
|
return not recursive or (
|
2022-01-30 15:03:34 +01:00
|
|
|
self.is_correct_node(node.left)
|
|
|
|
and self.is_correct_node(node.right)
|
2021-01-04 10:46:12 +01:00
|
|
|
)
|
|
|
|
|
2022-01-30 15:03:34 +01:00
|
|
|
# region DeleteRebalance
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2022-01-30 15:03:34 +01:00
|
|
|
def __fix_delete(
|
|
|
|
x: Optional[Node[T]],
|
|
|
|
y: Node[T],
|
|
|
|
z: Node[T],
|
|
|
|
reversed: bool,
|
|
|
|
rotate_left: RotateFunction[T],
|
|
|
|
rotate_right: RotateFunction[T],
|
|
|
|
) -> Optional[Node[T]]:
|
2021-01-04 10:46:12 +01:00
|
|
|
new_root = x
|
|
|
|
v = y.left
|
|
|
|
w = y.right
|
|
|
|
|
|
|
|
if reversed:
|
|
|
|
v, w = w, v
|
|
|
|
|
|
|
|
logger.debug(f"__fix_delete({x}, {y}, {z}, {reversed})")
|
|
|
|
|
|
|
|
w_diff = Node.difference(w, y)
|
|
|
|
logger.debug(f"w_diff = {w_diff}")
|
2022-01-30 15:03:34 +01:00
|
|
|
|
|
|
|
assert v
|
2021-01-04 10:46:12 +01:00
|
|
|
if w_diff == 1 and y.parent:
|
|
|
|
logger.debug(f"y.parent = {y.parent}")
|
|
|
|
new_root = rotate_left(y.parent)
|
|
|
|
|
|
|
|
y.promote()
|
|
|
|
z.demote()
|
|
|
|
|
|
|
|
if z.type == NodeType.LEAF:
|
|
|
|
z.demote()
|
|
|
|
elif w_diff == 2 and v.parent:
|
|
|
|
logger.debug(f"v.parent = {v.parent}")
|
|
|
|
rotate_right(v.parent)
|
|
|
|
new_root = rotate_left(v.parent)
|
|
|
|
|
|
|
|
v.promote().promote()
|
|
|
|
y.demote()
|
|
|
|
z.demote().demote()
|
|
|
|
|
|
|
|
return new_root
|
|
|
|
|
2022-01-30 15:03:34 +01:00
|
|
|
def __bottomup_delete(
|
|
|
|
self, x: Optional[Node[T]], parent: Optional[Node[T]]
|
|
|
|
) -> None:
|
2021-01-04 10:46:12 +01:00
|
|
|
x_diff = Node.difference(x, parent)
|
|
|
|
if x_diff != 3 or not parent:
|
|
|
|
return
|
|
|
|
|
|
|
|
y = parent.right if parent.left is x else parent.left
|
|
|
|
y_diff = Node.difference(y, parent)
|
|
|
|
|
|
|
|
while (
|
|
|
|
parent
|
|
|
|
and x_diff == 3
|
|
|
|
and y
|
|
|
|
and (y_diff == 2 or Node.differences(y) == (2, 2))
|
|
|
|
):
|
|
|
|
parent.demote()
|
|
|
|
if y_diff != 2:
|
|
|
|
y.demote()
|
|
|
|
|
|
|
|
x = parent
|
|
|
|
parent = x.parent
|
|
|
|
if not parent:
|
|
|
|
return
|
|
|
|
y = parent.right if parent.left is x else parent.left
|
|
|
|
|
|
|
|
x_diff = Node.difference(x, parent)
|
|
|
|
y_diff = Node.difference(y, parent)
|
|
|
|
|
|
|
|
if not parent:
|
|
|
|
return
|
|
|
|
|
|
|
|
rotating_around_root = parent.parent is None
|
2022-01-30 15:03:34 +01:00
|
|
|
new_root: Optional[Node[T]] = parent
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
parent_node_diffs = Node.differences(parent)
|
|
|
|
if parent_node_diffs in ((1, 3), (3, 1)):
|
|
|
|
if parent.left is x:
|
2022-01-30 15:03:34 +01:00
|
|
|
assert parent.right
|
2021-01-04 10:46:12 +01:00
|
|
|
new_root = WAVLTree.__fix_delete(
|
|
|
|
x,
|
|
|
|
parent.right,
|
|
|
|
parent,
|
|
|
|
False,
|
|
|
|
Node.rotate_left,
|
|
|
|
Node.rotate_right,
|
|
|
|
)
|
|
|
|
else:
|
2022-01-30 15:03:34 +01:00
|
|
|
assert parent.left
|
2021-01-04 10:46:12 +01:00
|
|
|
new_root = WAVLTree.__fix_delete(
|
|
|
|
x,
|
|
|
|
parent.left,
|
|
|
|
parent,
|
|
|
|
True,
|
|
|
|
Node.rotate_right,
|
|
|
|
Node.rotate_left,
|
|
|
|
)
|
|
|
|
|
|
|
|
if rotating_around_root:
|
|
|
|
self.root = new_root
|
|
|
|
|
2022-01-30 15:03:34 +01:00
|
|
|
def __delete_fixup(
|
|
|
|
self, y: Optional[Node[T]], parent: Optional[Node[T]] = None
|
|
|
|
) -> None:
|
2021-01-04 10:46:12 +01:00
|
|
|
logger.debug(f"[__delete_fixup] y = {y}, parent = {parent}")
|
|
|
|
|
|
|
|
z = y if y else parent
|
|
|
|
logger.debug(
|
2022-01-30 15:03:34 +01:00
|
|
|
f"[z.demote()] Node.differences({repr(z)}) == (2, 2) ~>*"
|
|
|
|
f"{Node.differences(z)} == (2, 2)"
|
2021-01-04 10:46:12 +01:00
|
|
|
)
|
2022-01-30 15:03:34 +01:00
|
|
|
assert z
|
2021-01-04 10:46:12 +01:00
|
|
|
if Node.differences(z) == (2, 2):
|
|
|
|
z.demote()
|
|
|
|
|
|
|
|
if parent:
|
|
|
|
for y in (parent.left, parent.right):
|
|
|
|
logger.debug(
|
2022-01-30 15:03:34 +01:00
|
|
|
f"[bottom-up delete] Node.difference({y}, {parent}) == 3"
|
|
|
|
f"~>* {Node.difference(y, parent)} == 3"
|
2021-01-04 10:46:12 +01:00
|
|
|
)
|
|
|
|
if Node.difference(y, parent) == 3:
|
|
|
|
self.__bottomup_delete(y, parent)
|
|
|
|
|
2022-01-30 15:03:34 +01:00
|
|
|
def _delete_rebalance(
|
|
|
|
self, node: Optional[Node[T]], parent: Optional[Node[T]]
|
|
|
|
) -> None:
|
2021-01-04 10:46:12 +01:00
|
|
|
while node or parent:
|
|
|
|
self.__delete_fixup(node, parent)
|
|
|
|
node, parent = parent, (parent.parent if parent else None)
|
|
|
|
|
2022-01-30 15:03:34 +01:00
|
|
|
# endregion DeleteRebalance
|