feat: handle root change in rotate function
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
a8de124eeb
commit
3bba030d53
1 changed files with 7 additions and 2 deletions
9
node.py
9
node.py
|
@ -8,6 +8,7 @@ from typing import (
|
||||||
Tuple,
|
Tuple,
|
||||||
TypeVar,
|
TypeVar,
|
||||||
Protocol,
|
Protocol,
|
||||||
|
Any,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,7 +113,7 @@ class Node(Generic[T]):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def rotate_right(x: "Node[T]") -> "Node[T]":
|
def rotate_right(x: "Node[T]", tree: Any = None) -> "Node[T]":
|
||||||
parent = x.parent
|
parent = x.parent
|
||||||
y = x.left
|
y = x.left
|
||||||
# z = x.right
|
# z = x.right
|
||||||
|
@ -123,6 +124,8 @@ class Node(Generic[T]):
|
||||||
parent.left = y
|
parent.left = y
|
||||||
else:
|
else:
|
||||||
parent.right = y
|
parent.right = y
|
||||||
|
elif tree:
|
||||||
|
tree.root = y
|
||||||
|
|
||||||
x.left = y.right
|
x.left = y.right
|
||||||
if x.left:
|
if x.left:
|
||||||
|
@ -135,7 +138,7 @@ class Node(Generic[T]):
|
||||||
return y
|
return y
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def rotate_left(x: "Node[T]") -> "Node[T]":
|
def rotate_left(x: "Node[T]", tree: Any = None) -> "Node[T]":
|
||||||
parent = x.parent
|
parent = x.parent
|
||||||
# y = x.left
|
# y = x.left
|
||||||
z = x.right
|
z = x.right
|
||||||
|
@ -146,6 +149,8 @@ class Node(Generic[T]):
|
||||||
parent.left = z
|
parent.left = z
|
||||||
else:
|
else:
|
||||||
parent.right = z
|
parent.right = z
|
||||||
|
elif tree:
|
||||||
|
tree.root = z
|
||||||
|
|
||||||
x.right = z.left
|
x.right = z.left
|
||||||
if x.right:
|
if x.right:
|
||||||
|
|
Loading…
Reference in a new issue