-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_search_tree.py
411 lines (361 loc) · 10.8 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from node import Node
class NodeQ:
def __init__(self, data):
self.data = data
self.next = None
class Myqueue:
def __init__(self):
self.head = None
self.tail = None
def isEmpty(self):
return self.head == None
def DeQueue(self):
if self.isEmpty():
return
value = self.head.data
self.head = self.head.next
return value
def EnQueue(self, data):
node = NodeQ(data)
if self.isEmpty():
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
class ProductBSTree:
def __init__(self):
self.root: Node | None = None
def findFather(self, p):
if self.root == None:
return None
if self.root.data == p:
return None
cur = self.root
father = None
while cur != None:
if cur.data.Price == p:
return father
father = cur
if cur.data.Price < p:
cur = cur.right
else:
cur = cur.left
return None
def isEmpty(self):
return self.root == None
def insert(self, data):
node = Node(data)
if self.isEmpty():
self.root = node
return
cur = self.root
father = None
pcode_data = int(data._pcode[1:])
while cur != None:
pcode_cur = int(cur.data._pcode[1:])
if pcode_cur == pcode_data:
return
father = cur
if pcode_cur < pcode_data:
cur = cur.right
else:
cur = cur.left
if int(father.data._pcode[1:]) < int(data._pcode[1:]):
father.right = node
else:
father.left = node
# function
def insert2(self,node):
if self.isEmpty():
self.root = node
else:
curNode = self.root
while curNode:
if int(node.data._pcode[1:]) < int(curNode.data._pcode[1:]):
if curNode.left == None:
curNode.left = node
curNode = curNode.left
elif int(node.data._pcode[1:]) > int(curNode.data._pcode[1:]):
if curNode.right == None:
curNode.right = node
curNode = curNode.right
elif int(node.data._pcode[1:]) == int(curNode.data._pcode[1:]):
return f"{node.data._pcode} has existed"
def findNode(self, pcode): # tim node co key = p
cur = self.root
while cur != None:
if cur.data._pcode == pcode:
return cur.data
else:
if cur.data._pcode < pcode:
cur = cur.right
else:
cur = cur.left
return None # can not find the node has key = p
def visit(self, p):
if p == None:
return
else:
print(f"{p.data}", end=" ")
def preOrder(self, p):
if p == None:
return
else:
self.visit(p)
self.preOrder(p.left)
self.preOrder(p.right)
def postOrder(self, p):
if p == None:
return
else:
self.postOrder(p.left)
self.postOrder(p.right)
self.visit(p)
def inOrder(self, p):
if p == None:
return
self.inOrder(p.left)
self.visit(p)
self.inOrder(p.right)
def size(self):
count = -1
if self.root is not None:
if self.root.left is not None:
count += BST_size(root.left)
if self.root.right is not None:
count += BST_size(root.right)
return count
def breadth_first(self):
queue = Myqueue()
if self.root == None:
return
queue = Myqueue()
queue.EnQueue(self.root)
while not queue.isEmpty():
p = queue.DeQueue()
self.visit(p)
if p.left != None:
queue.EnQueue(p.left)
if p.right != None:
queue.EnQueue(p.right)
def deleteNode(root, key):
if root is None:
return root
if key < root.key:
root.left = deleteNode(root.left, key)
elif key > root.key:
root.right = deleteNode(root.right, key)
else:
if root.left is None:
temp = root.right
root = None
return temp
elif root.right is None:
temp = root.left
root = None
return temp
temp = minValueNode(root.right)
root.key = temp.key
root.right = deleteNode(root.right, temp.key)
return root
def count_nodes(self, node):
if node is None:
return 0
return 1 + self.count_nodes(node.left) + self.count_nodes(node.right)
def deleteByCopyLeft(self, p):
q = self.findNode(p)
if q == None or q.left == None:
return
cur = q.left
father = None
while cur.right != None:
father = cur
cur = cur.right
# cur = node ngoai cung ben phai cua nhanh ben trai
q.data = cur.data
if q.left.right == None:
q.left = q.left.left
else:
father.right = cur.left
def deleteLeft(self, data):
q = self.findNode(data)
if q == None:
return
father = self.findFather(data)
if father == None:
if q.right == None:
self.root = q.left
else:
self.root = q.right
return
if q.right == q.left == None:
if father.data < q.data:
father.right = None
else:
father.left = None
return
if q.right == None and q.left != None:
father.right = q.left
return
if q.left == None and q.right != None:
father.right = q.right
return
self.deleteByCopyLeft(data)
def deleteByCopy(self, p):
q = self.findNode(p)
b = self.findFather(p)
if self.root.data == p:
self.root = None
return
if q == None:
print(f"{p} k ton tai trong cay")
return
if q.right == None and q.left == None:
if b.data > q.data:
b.left = None
if b.data < q.data:
b.right = None
if q.left == None and q.right != None:
self.deleteByCopyRight(p)
if q.left != None and q.right == None:
self.deleteByCopyLeft(p)
else:
self.deleteByCopyLeft(p)
def deleteByCopyRight(self, p):
q = self.findNode(p)
if q == None or q.right == None:
return
cur = q.right
father = None
while cur.left != None:
father = cur
cur = cur.left
q.data = cur.data
if q.right.left == None:
q.right = q.right.right
else:
father.left = cur.right
def deleteByMergingLeft(self, key):
node = self.findNode(key)
if node == None or node.left == None:
return
father = self.findFather(key)
cur = node.left
while cur.right != None:
cur = cur.right
cur.right = node.right
if father == None:
self.root = self.root.left
else:
if father.left.data == key:
father.left = node.left
else:
father.right = node.left
def deleteByMergingRight(self, key):
node = self.findNode(key)
if node == None or node.right == None:
return
father = self.findFather(key)
cur = node.right
while cur.left != None:
cur = cur.left
cur.left = node.left
if father == None:
self.root = self.root.right
else:
if father.right.data == key:
father.right = node.right
else:
father.left = node.right
def deleteByMerging(self, key):
q = self.findNode(key)
if q == None:
return
father = self.findFather(key)
if father == None:
if q.right == None:
self.root = q.left
else:
self.root = q.right
return
if q.left == None:
father.right = q
return
if q.right == None:
father.left = q
return
self.deleteByMergingLeft(key)
def rightRotate(self, data):
p = self.findNode(data)
father = self.findFather(data)
if self.isEmpty() or p.left == None:
return
c = p.left
temp = c.right
c.right = p
p.left = temp
if father == None:
self.root = c
return
if father.data < data:
father.right = c
else:
father.left = c
return
# display
def invisit(self):
self.inOrder(self.root)
return
def postvisit(self):
self.postOrder(self.root)
def previsit(self):
self.preOrder(self.root)
def turnLeft(self, nodeList):
if self.isEmpty():
print("Nothing in the list")
return
nodeList = nodeList[::-1]
i = 0
for node in nodeList:
node.left = None
node.right = None
for node in nodeList:
if i == 0:
self.root = node
else:
self.insert2(node)
i += 1
def balanceTree(self):
self.turnLeft(self.inorderNode(self.root))
self.convertAVLtree(self.root)
def convertAVLtree(self, root):
nodes = []
self.storeBSTNodes(root, nodes)
n = len(nodes)
self.root = self.buildTree(nodes, 0, n-1)
nodeList = []
def inorderNode(self, curNode):
if self.isEmpty():
return None
if curNode == None:
return
else:
self.inorderNode(curNode.left)
self.nodeList.append(curNode)
self.inorderNode(curNode.right)
return self.nodeList
def storeBSTNodes(self, root, nodes):
if not root:
return
self.storeBSTNodes(root.left, nodes)
nodes.append(root)
self.storeBSTNodes(root.right, nodes)
def buildTree(self, nodes, start, end):
if start > end:
return None
mid = (start+end) // 2
node = nodes[mid]
node.left = self.buildTree(nodes, start, mid-1)
node.right = self.buildTree(nodes, mid+1, end)
return node