-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
from .core import Node # noqa: F401 | ||
from .core import GenericTree # noqa: F401 | ||
from .core import BinaryNode # noqa: F401 | ||
from .core import BinaryTree # noqa: F401 | ||
from .core import TraversalType # noqa: F401 | ||
from .core import BinarySearchTree # noqa: F401 | ||
from .core import BSTree # noqa: F401 | ||
from .core import AVLTree # noqa: F401 | ||
from .core import AVLNode # noqa: F401 | ||
from .node import Node # noqa: F401 | ||
from .node import BinaryNode # noqa: F401 | ||
from .node import AVLNode # noqa: F401 | ||
|
||
from .tree import GenericTree # noqa: F401 | ||
from .tree import BinaryTree # noqa: F401 | ||
from .tree import TraversalType # noqa: F401 | ||
from .tree import BinarySearchTree # noqa: F401 | ||
from .tree import BSTree # noqa: F401 | ||
from .tree import AVLTree # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TypeVar, Generic, Optional, List, Any | ||
|
||
|
||
K = TypeVar("K") | ||
V = TypeVar("V") | ||
|
||
|
||
class Node(Generic[K, V]): | ||
def __init__(self, key: K, value: Optional[V] = None): | ||
self.key: K = key | ||
self.value: Optional[V] = value | ||
self.children: List[Any] = [] | ||
|
||
|
||
class BinaryNode(Node[K, V]): | ||
def __init__(self, key: K, value: Optional[V] = None): | ||
super().__init__(key, value) | ||
self.children: List[BinaryNode | None] = [None, None] | ||
|
||
@property | ||
def left(self): | ||
return self.children[0] | ||
|
||
@left.setter | ||
def left(self, node: Optional[BinaryNode]): | ||
self.children[0] = node | ||
|
||
@property | ||
def right(self): | ||
return self.children[1] | ||
|
||
@right.setter | ||
def right(self, node: Optional[BinaryNode]): | ||
self.children[1] = node | ||
|
||
@property | ||
def min_node(self): | ||
if self.left is None: | ||
return self | ||
return self.left.min_node | ||
|
||
@property | ||
def max_node(self): | ||
if self.right is None: | ||
return self | ||
return self.right.max_node | ||
|
||
|
||
class AVLNode(BinaryNode[K, V]): | ||
def __init__(self, key: K, value: Optional[V] = None): | ||
super().__init__(key, value) | ||
self.height = 1 | ||
self.left_height = 0 | ||
self.right_height = 0 | ||
|
||
@property | ||
def balance(self): | ||
return self.left_height - self.right_height |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters