Fix Python code.

This commit is contained in:
krahets
2023-10-14 21:54:47 +08:00
parent a8c624fa5a
commit 2976d9ff56
10 changed files with 174 additions and 170 deletions

View File

@@ -16,15 +16,15 @@ class BinarySearchTree:
def __init__(self):
"""构造方法"""
# 初始化空树
self.__root = None
self._root = None
def get_root(self) -> TreeNode | None:
"""获取二叉树根节点"""
return self.__root
return self._root
def search(self, num: int) -> TreeNode | None:
"""查找节点"""
cur = self.__root
cur = self._root
# 循环查找,越过叶节点后跳出
while cur is not None:
# 目标节点在 cur 的右子树中
@@ -41,11 +41,11 @@ class BinarySearchTree:
def insert(self, num: int):
"""插入节点"""
# 若树为空,则初始化根节点
if self.__root is None:
self.__root = TreeNode(num)
if self._root is None:
self._root = TreeNode(num)
return
# 循环查找,越过叶节点后跳出
cur, pre = self.__root, None
cur, pre = self._root, None
while cur is not None:
# 找到重复节点,直接返回
if cur.val == num:
@@ -67,10 +67,10 @@ class BinarySearchTree:
def remove(self, num: int):
"""删除节点"""
# 若树为空,直接提前返回
if self.__root is None:
if self._root is None:
return
# 循环查找,越过叶节点后跳出
cur, pre = self.__root, None
cur, pre = self._root, None
while cur is not None:
# 找到待删除节点,跳出循环
if cur.val == num:
@@ -91,14 +91,14 @@ class BinarySearchTree:
# 当子节点数量 = 0 / 1 时, child = null / 该子节点
child = cur.left or cur.right
# 删除节点 cur
if cur != self.__root:
if cur != self._root:
if pre.left == cur:
pre.left = child
else:
pre.right = child
else:
# 若删除节点为根节点,则重新指定根节点
self.__root = child
self._root = child
# 子节点数量 = 2
else:
# 获取中序遍历中 cur 的下一个节点