chore(wavl): refactor

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-12 12:23:31 +02:00
parent 88d691225e
commit 6fb09cd89b
Signed by: mfocko
GPG key ID: 7C47D46246790496

18
wavl.py
View file

@ -48,7 +48,6 @@ class WAVLTree(AVLTree[T]):
w_diff = Node.difference(w, y) w_diff = Node.difference(w, y)
# assert v
if w_diff == 1 and y.parent: if w_diff == 1 and y.parent:
new_root = rotate_left(y.parent) new_root = rotate_left(y.parent)
if rotating_around_root: if rotating_around_root:
@ -59,7 +58,7 @@ class WAVLTree(AVLTree[T]):
if z.type == NodeType.LEAF: if z.type == NodeType.LEAF:
z.demote() z.demote()
elif w_diff == 2 and v and v.parent: elif w_diff == 2 and v.parent:
rotate_right(v.parent) rotate_right(v.parent)
new_root = rotate_left(v.parent) new_root = rotate_left(v.parent)
if rotating_around_root: if rotating_around_root:
@ -85,9 +84,9 @@ class WAVLTree(AVLTree[T]):
and y and y
and (y_diff == 2 or Node.differences(y) == (2, 2)) and (y_diff == 2 or Node.differences(y) == (2, 2))
): ):
parent.demote()
if y_diff != 2: if y_diff != 2:
y.demote() y.demote()
parent.demote()
x = parent x = parent
parent = x.parent parent = x.parent
@ -98,9 +97,6 @@ class WAVLTree(AVLTree[T]):
x_diff = Node.difference(x, parent) x_diff = Node.difference(x, parent)
y_diff = Node.difference(y, parent) y_diff = Node.difference(y, parent)
if not parent:
return
rotating_around_root = parent.parent is None rotating_around_root = parent.parent is None
parent_node_diffs = Node.differences(parent) parent_node_diffs = Node.differences(parent)
@ -128,8 +124,8 @@ class WAVLTree(AVLTree[T]):
Node.rotate_left, Node.rotate_left,
) )
def __delete_fixup( def _delete_rebalance(
self, y: Optional[Node[T]], parent: Optional[Node[T]] = None self, y: Optional[Node[T]], parent: Optional[Node[T]]
) -> None: ) -> None:
if Node.differences(y) == (2, 2): if Node.differences(y) == (2, 2):
y.demote() y.demote()
@ -144,10 +140,6 @@ class WAVLTree(AVLTree[T]):
for y in (parent.left, parent.right): for y in (parent.left, parent.right):
if Node.difference(y, parent) == 3: if Node.difference(y, parent) == 3:
self.__bottomup_delete(y, parent) self.__bottomup_delete(y, parent)
return
def _delete_rebalance(
self, node: Optional[Node[T]], parent: Optional[Node[T]]
) -> None:
self.__delete_fixup(node, parent)
# endregion DeleteRebalance # endregion DeleteRebalance