mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-07 06:44:57 +08:00
Simplify the python code of bst and avl tree.
This commit is contained in:
@ -15,11 +15,7 @@ class AVLTree:
|
||||
|
||||
def __init__(self, root: TreeNode | None = None):
|
||||
"""构造方法"""
|
||||
self.__root = root
|
||||
|
||||
@property
|
||||
def root(self) -> TreeNode | None:
|
||||
return self.__root
|
||||
self.root = root
|
||||
|
||||
def height(self, node: TreeNode | None) -> int:
|
||||
"""获取节点高度"""
|
||||
@ -94,7 +90,7 @@ class AVLTree:
|
||||
|
||||
def insert(self, val) -> None:
|
||||
"""插入节点"""
|
||||
self.__root = self.__insert_helper(self.__root, val)
|
||||
self.root = self.__insert_helper(self.root, val)
|
||||
|
||||
def __insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:
|
||||
"""递归插入节点(辅助方法)"""
|
||||
@ -115,7 +111,7 @@ class AVLTree:
|
||||
|
||||
def remove(self, val: int) -> None:
|
||||
"""删除节点"""
|
||||
self.__root = self.__remove_helper(self.__root, val)
|
||||
self.root = self.__remove_helper(self.root, val)
|
||||
|
||||
def __remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:
|
||||
"""递归删除节点(辅助方法)"""
|
||||
@ -149,7 +145,7 @@ class AVLTree:
|
||||
|
||||
def search(self, val: int) -> TreeNode | None:
|
||||
"""查找节点"""
|
||||
cur = self.__root
|
||||
cur = self.root
|
||||
# 循环查找,越过叶节点后跳出
|
||||
while cur is not None:
|
||||
# 目标节点在 cur 的右子树中
|
||||
|
Reference in New Issue
Block a user