2022-01-30 15:02:28 +01:00
|
|
|
from abc import abstractmethod
|
2021-01-04 10:46:12 +01:00
|
|
|
import enum
|
2022-05-15 18:07:50 +02:00
|
|
|
from typing import (
|
|
|
|
Iterable,
|
2022-05-22 21:07:40 +02:00
|
|
|
Iterator,
|
2022-05-15 18:07:50 +02:00
|
|
|
Optional,
|
|
|
|
Generic,
|
|
|
|
Tuple,
|
|
|
|
TypeVar,
|
|
|
|
Protocol,
|
|
|
|
)
|
2022-01-30 15:02:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Comparable(Protocol):
|
|
|
|
"""Protocol for annotating comparable types."""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __lt__(self: "Comparable", other: "Comparable") -> bool:
|
|
|
|
pass
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NodeType(enum.IntEnum):
|
|
|
|
LEAF = 0
|
|
|
|
UNARY = 1
|
|
|
|
BINARY = 2
|
|
|
|
|
|
|
|
|
2022-01-30 15:02:28 +01:00
|
|
|
T = TypeVar("T", bound=Comparable)
|
|
|
|
|
|
|
|
|
2022-05-22 21:07:40 +02:00
|
|
|
class Node(Generic[T], Iterable[T]):
|
2022-01-30 15:02:28 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
value: T,
|
|
|
|
left: "Optional[Node[T]]" = None,
|
|
|
|
right: "Optional[Node[T]]" = None,
|
|
|
|
parent: "Optional[Node[T]]" = None,
|
|
|
|
):
|
2021-01-04 10:46:12 +01:00
|
|
|
self.parent = parent
|
|
|
|
self.left = left
|
|
|
|
self.right = right
|
|
|
|
|
|
|
|
self.value = value
|
2022-01-30 15:02:28 +01:00
|
|
|
self.rank: int = 0
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
@property
|
2022-01-30 15:02:28 +01:00
|
|
|
def type(self) -> NodeType:
|
2021-01-04 10:46:12 +01:00
|
|
|
if self.left and self.right:
|
|
|
|
return NodeType.BINARY
|
|
|
|
|
|
|
|
if self.left or self.right:
|
|
|
|
return NodeType.UNARY
|
|
|
|
|
|
|
|
return NodeType.LEAF
|
|
|
|
|
2022-01-30 15:02:28 +01:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return (
|
|
|
|
f"Node(value={self.value}, rank={self.rank}, "
|
|
|
|
f"left={self.left}, right={self.right}, parent={self.parent})"
|
|
|
|
)
|
2021-01-04 10:46:12 +01:00
|
|
|
|
2022-01-30 15:02:28 +01:00
|
|
|
def __str__(self) -> str:
|
2021-01-04 10:46:12 +01:00
|
|
|
return f"Node(value={self.value}, rank={self.rank})"
|
|
|
|
|
2022-05-22 21:07:40 +02:00
|
|
|
def __iter__(self) -> Iterator[T]:
|
2022-05-15 18:07:50 +02:00
|
|
|
"""
|
|
|
|
Yields:
|
|
|
|
Keys from the subtree rooted at the node in an inorder fashion.
|
|
|
|
"""
|
|
|
|
if self.left:
|
|
|
|
yield from self.left
|
|
|
|
yield self.value
|
|
|
|
if self.right:
|
|
|
|
yield from self.right
|
|
|
|
|
2021-01-04 10:46:12 +01:00
|
|
|
@staticmethod
|
2022-01-30 15:02:28 +01:00
|
|
|
def height(node: "Optional[Node[T]]") -> int:
|
|
|
|
return (
|
|
|
|
1 + max(Node.height(node.left), Node.height(node.right))
|
|
|
|
if node
|
|
|
|
else -1
|
|
|
|
)
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2022-01-30 15:02:28 +01:00
|
|
|
def get_rank(node: "Optional[Node[T]]") -> int:
|
2021-01-04 10:46:12 +01:00
|
|
|
return -1 if not node else node.rank
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-30 15:02:28 +01:00
|
|
|
def difference(
|
|
|
|
node: "Optional[Node[T]]", parent: "Optional[Node[T]]" = None
|
|
|
|
) -> int:
|
2021-01-04 10:46:12 +01:00
|
|
|
if not parent:
|
|
|
|
parent = node.parent if node else None
|
2022-01-30 15:02:28 +01:00
|
|
|
return Node.get_rank(parent) - Node.get_rank(node)
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2022-01-30 15:02:28 +01:00
|
|
|
def differences(node: "Optional[Node[T]]") -> Tuple[int, int]:
|
|
|
|
node_rank = Node.get_rank(node)
|
2021-01-04 10:46:12 +01:00
|
|
|
(left, right) = (node.left, node.right) if node else (None, None)
|
|
|
|
|
2022-01-30 15:02:28 +01:00
|
|
|
return (
|
|
|
|
node_rank - Node.get_rank(left),
|
|
|
|
node_rank - Node.get_rank(right),
|
|
|
|
)
|
2021-01-04 10:46:12 +01:00
|
|
|
|
2022-01-30 15:02:28 +01:00
|
|
|
def promote(self) -> "Node[T]":
|
2021-01-04 10:46:12 +01:00
|
|
|
self.rank += 1
|
|
|
|
return self
|
|
|
|
|
2022-01-30 15:02:28 +01:00
|
|
|
def demote(self) -> "Node[T]":
|
2021-01-04 10:46:12 +01:00
|
|
|
self.rank -= 1
|
|
|
|
return self
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-30 15:02:28 +01:00
|
|
|
def find_parent_node(
|
|
|
|
value: T, node: "Node[T]", missing: bool = True
|
2022-05-22 21:07:40 +02:00
|
|
|
) -> "Optional[Node[T]]":
|
2022-01-30 15:02:28 +01:00
|
|
|
new_node: "Optional[Node[T]]" = node
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
while new_node and (missing or new_node.value != value):
|
|
|
|
node = new_node
|
2022-05-12 12:20:24 +02:00
|
|
|
if value < node.value:
|
|
|
|
new_node = node.left
|
|
|
|
elif node.value < value:
|
|
|
|
new_node = node.right
|
|
|
|
else:
|
|
|
|
return None
|
2021-01-04 10:46:12 +01:00
|
|
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-30 15:02:28 +01:00
|
|
|
def search(value: T, node: "Optional[Node[T]]") -> "Optional[Node[T]]":
|
2021-01-04 10:46:12 +01:00
|
|
|
while node and node.value != value:
|
|
|
|
node = node.left if value < node.value else node.right
|
|
|
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-30 15:02:28 +01:00
|
|
|
def minimum(node: "Node[T]") -> "Node[T]":
|
2021-01-04 10:46:12 +01:00
|
|
|
while node.left:
|
|
|
|
node = node.left
|
|
|
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-30 15:02:28 +01:00
|
|
|
def maximum(node: "Node[T]") -> "Node[T]":
|
2021-01-04 10:46:12 +01:00
|
|
|
while node.right:
|
|
|
|
node = node.right
|
|
|
|
|
|
|
|
return node
|