-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.py
126 lines (106 loc) · 3.34 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class BST:
def __init__(self, value=None, left=None, right=None):
self.value = value
self.left = left
self.right = right
def insert(self, value: int):
if self.value is None:
self.value = value
return
if self.value == value:
return
if value < self.value:
if self.left is None:
self.left = BST(value=value)
return
self.left.insert(value)
return
if self.right is None:
self.right = BST(value=value)
return
self.right.insert(value)
def preorder(self, values: list):
if self.value is not None:
values.append(self.value)
#values.insert(0, self.value)
if self.left is not None:
self.left.preorder(values)
if self.right is not None:
self.right.preorder(values)
def inorder(self, values: list):
if self.left is not None:
self.left.inorder(values)
if self.value is not None:
values.append(self.value)
if self.right is not None:
self.right.inorder(values)
def postorder(self, values: list):
if self.left is not None:
self.left.postorder(values)
if self.right is not None:
self.right.postorder(values)
if self.value is not None:
values.append(self.value)
def get_min(self):
if self is None:
return
curr_node = self
while curr_node.left is not None:
curr_node = curr_node.left
return curr_node.value
def get_max(self):
if self is None:
return
curr_node = self
while curr_node.right is not None:
curr_node = curr_node.right
return curr_node.value
def delete(self, value):
if self.value is None:
return
if value < self.value:
self.left.delete(value)
elif value < self.value:
self.right.delete(value)
else:
if self.left is None:
self.value = None
return self.right
if self.right is None:
self.value = None
return self.left
inorder_succesor = self.right.get_min()
self.value = inorder_succesor.value
self.right = inorder_succesor.delete(value)
return self
if __name__ == '__main__':
#nums = [12, 6, 18, 19, 21, 11, 3, 5, 4, 24, 18]
nums = [22, 12, 30, 8, 20, 25, 40]
bst = BST()
for num in nums:
bst.insert(num)
funcs = (
(bst.preorder, [22, 12, 8, 20, 30, 25, 40]),
(bst.postorder, [8, 20, 12, 25, 40, 30, 22]),
(bst.inorder, [8, 12, 20, 22, 25, 30, 40])
)
for func, wanted in funcs:
vals = []
func(vals)
assert wanted == vals, f"{vals} {func.__name__}"
print(func.__name__, vals)
assert bst.get_min() == 8
assert bst.get_max() == 40
nums = [50, 30, 20, 40, 70, 60, 80]
bst = BST()
for num in nums:
bst.insert(num)
bst.delete(20)
funcs = (
(bst.inorder, [30, 40, 50, 60, 70, 80]),
)
for func, wanted in funcs:
vals = []
func(vals)
assert wanted == vals, f"{vals} {func.__name__}"
print(func.__name__, vals)