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
|
|
|
|
2022-01-30 15:03:34 +01:00
|
|
|
def __fix_delete(
|
2022-05-07 15:42:37 +02:00
|
|
|
self,
|
2022-01-30 15:03:34 +01:00
|
|
|
x: Optional[Node[T]],
|
|
|
|
y: Node[T],
|
|
|
|
z: Node[T],
|
|
|
|
reversed: bool,
|
2022-05-07 15:42:37 +02:00
|
|
|
rotating_around_root: bool,
|
2022-01-30 15:03:34 +01:00
|
|
|
rotate_left: RotateFunction[T],
|
|
|
|
rotate_right: RotateFunction[T],
|
2022-05-07 15:42:37 +02:00
|
|
|
) -> None:
|
2021-01-04 10:46:12 +01:00
|
|
|
new_root = x
|
|
|
|
v = y.left
|
|
|
|
w = y.right
|
|
|
|
|
|
|
|
if reversed:
|
|
|
|
v, w = w, v
|
|
|
|
|
|
|
|
w_diff = Node.difference(w, y)
|
2022-01-30 15:03:34 +01:00
|
|
|
|
2021-01-04 10:46:12 +01:00
|
|
|
if w_diff == 1 and y.parent:
|
|
|
|
new_root = rotate_left(y.parent)
|
2022-05-07 15:42:37 +02:00
|
|
|
if rotating_around_root:
|
|
|
|
self.root = new_root
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
y.promote()
|
|
|
|
z.demote()
|
|
|
|
|
|
|
|
if z.type == NodeType.LEAF:
|
|
|
|
z.demote()
|
2022-05-12 12:23:31 +02:00
|
|
|
elif w_diff == 2 and v.parent:
|
2021-01-04 10:46:12 +01:00
|
|
|
rotate_right(v.parent)
|
|
|
|
new_root = rotate_left(v.parent)
|
2022-05-07 15:42:37 +02:00
|
|
|
if rotating_around_root:
|
|
|
|
self.root = new_root
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
v.promote().promote()
|
|
|
|
y.demote()
|
|
|
|
z.demote().demote()
|
|
|
|
|
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))
|
|
|
|
):
|
|
|
|
if y_diff != 2:
|
|
|
|
y.demote()
|
2022-05-12 12:23:31 +02:00
|
|
|
parent.demote()
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
rotating_around_root = parent.parent is None
|
|
|
|
|
|
|
|
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
|
2022-05-07 15:42:37 +02:00
|
|
|
self.__fix_delete(
|
2021-01-04 10:46:12 +01:00
|
|
|
x,
|
|
|
|
parent.right,
|
|
|
|
parent,
|
|
|
|
False,
|
2022-05-07 15:42:37 +02:00
|
|
|
rotating_around_root,
|
2021-01-04 10:46:12 +01:00
|
|
|
Node.rotate_left,
|
|
|
|
Node.rotate_right,
|
|
|
|
)
|
|
|
|
else:
|
2022-01-30 15:03:34 +01:00
|
|
|
assert parent.left
|
2022-05-07 15:42:37 +02:00
|
|
|
self.__fix_delete(
|
2021-01-04 10:46:12 +01:00
|
|
|
x,
|
|
|
|
parent.left,
|
|
|
|
parent,
|
|
|
|
True,
|
2022-05-07 15:42:37 +02:00
|
|
|
rotating_around_root,
|
2021-01-04 10:46:12 +01:00
|
|
|
Node.rotate_right,
|
|
|
|
Node.rotate_left,
|
|
|
|
)
|
|
|
|
|
2022-05-12 12:23:31 +02:00
|
|
|
def _delete_rebalance(
|
|
|
|
self, y: Optional[Node[T]], parent: Optional[Node[T]]
|
2022-01-30 15:03:34 +01:00
|
|
|
) -> None:
|
2022-05-07 15:42:37 +02:00
|
|
|
if Node.differences(y) == (2, 2):
|
|
|
|
y.demote()
|
|
|
|
parent = y.parent
|
|
|
|
elif Node.differences(parent) == (2, 2):
|
|
|
|
parent.demote()
|
|
|
|
parent = parent.parent
|
2021-01-04 10:46:12 +01:00
|
|
|
|
2022-05-07 15:42:37 +02:00
|
|
|
if not parent:
|
|
|
|
return
|
|
|
|
|
|
|
|
for y in (parent.left, parent.right):
|
|
|
|
if Node.difference(y, parent) == 3:
|
|
|
|
self.__bottomup_delete(y, parent)
|
2022-05-12 12:23:31 +02:00
|
|
|
return
|
2021-01-04 10:46:12 +01:00
|
|
|
|
2022-01-30 15:03:34 +01:00
|
|
|
# endregion DeleteRebalance
|