-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search_tree.py
255 lines (221 loc) · 8.11 KB
/
binary_search_tree.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""
@version: python3.6
@author: Fieldhunter
@contact: 1677532160yuan@gmail.com
@time: 2020-05-03
"""
import functools
"""
In the binary search tree, the value of each node in the left subtree is
less than or equal to the value of this node, and the value of the right
subtree node is greater than the value of this node.
"""
class tree_Node():
def __init__(self, num):
self.data = num
self.left = None
self.right = None
class Binary_search_tree():
"""
Self__left and self.__right num are used to record the number of left and
right subtree nodes of the root node.
It is used for proper left and right rotation (refer to red black tree) to
improve the operation efficiency as much as possible.
"""
def __init__(self):
self.__head = None
self.__left_num = 0
self.__right_num = 0
def __left_rotate(self):
while self.__left_num < self.__right_num:
focus_node = self.__head
right_node = self.__head.right
right_node_son = right_node.left
focus_node.right = right_node_son
right_node.left = focus_node
self.__head = right_node
if right_node_son == None:
self.__left_num += 1
self.__right_num -= 1
else:
self.__left_num += 2
self.__right_num -= 2
def __right_rotate(self):
while self.__right_num < self.__left_num:
focus_node = self.__head
left_node = self.__head.left
left_node_son = left_node.right
focus_node.left = left_node_son
left_node.right = focus_node
self.__head = left_node
if left_node_son == None:
self.__left_num -= 1
self.__right_num += 1
else:
self.__left_num -= 2
self.__right_num += 2
def add_data(self, element):
new_node = tree_Node(element)
if self.__head == None:
self.__head = new_node
else:
pointer = self.__head
if element > self.__head.data:
self.__right_num += 1
else:
self.__left_num += 1
while pointer != None:
prev_pointer = pointer
if element > pointer.data:
pointer = pointer.right
pos = "right"
else:
pointer = pointer.left
pos = "left"
if pos == "right":
prev_pointer.right = new_node
else:
prev_pointer.left = new_node
# When the number of two subtrees differs by 5, rotate left and right
if self.__left_num - self.__right_num == 5:
self.__right_rotate()
elif self.__right_num - self.__left_num == 5:
self.__left_rotate()
def del_data(self, element):
prev_pointer = None
pos = None
pointer = self.__head
find = False
"""
convenient to reduce the number of left and
right subtree nodes of root node
"""
if pointer != None:
if pointer.data < element:
direction = "right"
else:
direction = "left"
while pointer != None and find == False:
if pointer.data == element:
find = True
else:
prev_pointer = pointer
if pointer.data < element:
pointer = pointer.right
pos = "right"
else:
pointer = pointer.left
pos = "left"
if find:
if pointer != self.__head:
if direction == "right":
self.__right_num -= 1
else:
self.__left_num -= 1
elif pointer.right != None:
self.__right_num -= 1
elif pointer.left != None:
self.__left_num -= 1
"""
Because the second step of deletion has the operation of
reusing the second step, so the second step is carried
out separately.
"""
self.__del_step(prev_pointer, pointer, pos)
print("Successful to del data")
else:
print("No data in need")
# When the number of two subtrees differs by 5, rotate left and right.
if self.__left_num - self.__right_num == 5:
self.__right_rotate()
elif self.__right_num - self.__left_num == 5:
self.__left_rotate()
def __del_step(self, prev_pointer, pointer, pos):
# when the node to be deleted has no child nodes
if pointer.left == None and pointer.right == None:
if pointer == self.__head:
self.__head == None
else:
if pos == "right":
prev_pointer.right = None
else:
prev_pointer.left = None
# when the node to be deleted has two child nodes
elif pointer.left != None and pointer.right != None:
min_node_prev = pointer
min_node = pointer.right
new_pointer = min_node.left
new_pos = "right"
while new_pointer != None:
new_pos = "left"
min_node_prev = min_node
min_node = new_pointer
new_pointer = new_pointer.left
new_node = tree_Node(min_node.data)
new_node.left = pointer.left
new_node.right = pointer.right
if self.__head == pointer:
self.__head = new_node
else:
if pos == "right":
prev_pointer.right = new_node
else:
prev_pointer.left = new_node
if min_node_prev == pointer:
min_node_prev = new_node
self.__del_step(min_node_prev, min_node, new_pos)
# when the node to be deleted has only one child
else:
if pointer == self.__head:
if self.__head.left != None:
self.__head = self.__head.left
else:
self.__head = self.__head.right
else:
if pos == "right":
if pointer.right != None:
prev_pointer.right = pointer.right
else:
prev_pointer.right = pointer.left
else:
if pointer.right != None:
prev_pointer.left = pointer.right
else:
prev_pointer.left = pointer.left
def find_data(self, element):
pointer = self.__head
find = False
while pointer != None and find == False:
if pointer.data == element:
find = True
elif pointer.data < element:
pointer = pointer.right
else:
pointer = pointer.left
if find:
print("find data")
else:
print("No data in need")
def inorder_traversal(self, pointer=self.__head):
if pointer != None:
self.inorder_traversal(pointer.left)
print(pointer.data, end=" ")
self.inorder_traversal(pointer.right)
elif pointer == self.__head:
print(None)
"""
Check if the code used to access the tree information,Decorator function.
The purpose of simply adding code is to prevent binary search tree from
being tampered with maliciously and to provide the API for developers.
"""
def __check_code(func):
@functools.wraps(func)
def check(self, code):
if code != 'adsf;{h3096j34ka`fd>&/edgb^45:6':
raise Exception('code is wrong!')
result = func(self, code)
return result
return check
@__check_code
def return_basic_information(self, code):
return self.__head, self.__left_num, self.__right_num