-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.py
66 lines (53 loc) · 1.92 KB
/
BST.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from typing import Any
class BSTNode:
def __init__(self, key: int, val: Any):
self.key = key
self.val = val
self.left: BST = BST()
self.right: BST = BST()
class BST:
def __init__(self):
self.root: BSTNode | None = None
def insert(self, key: int, val: Any = None) -> None:
if self.root is None:
self.root = BSTNode(key, val)
return
if key == self.root.key:
self.root.val = val
elif key < self.root.key:
self.root.left.insert(key, val)
else:
self.root.right.insert(key, val)
def search(self, key: int) -> Any:
if self.root is None:
return None
if key == self.root.key:
return self.root.val
elif key < self.root.key:
return self.root.left.search(key)
else:
return self.root.right.search(key)
def delete(self, key: int) -> None:
def _pre_node(node: BSTNode) -> BSTNode: # 要求 node 一定存在 pre node
node = node.left.root
while node.right.root is not None:
node = node.right.root
return node
def _remove(node: BSTNode) -> BSTNode | None:
if node.left.root is None:
return node.right.root
if node.right.root is None:
return node.left.root
pre = _pre_node(node)
pre.right.root = node.right.root
# no need to worry about the value of "node", it will get collected when no reference points to it.
# https://docs.python.org/zh-cn/3/library/sys.html#sys.getrefcount
return node.left.root
if self.root is None:
return
if key == self.root.key:
self.root = _remove(self.root)
elif key < self.root.key:
self.root.left.delete(key)
else:
self.root.right.delete(key)