-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbstree.py
More file actions
84 lines (75 loc) · 1.89 KB
/
bstree.py
File metadata and controls
84 lines (75 loc) · 1.89 KB
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
from btree import Tree
class BinarySearchTree(object):
def __init__(self, base):
self.base = base
def find(self, data):
'''
查找
'''
p = self.base
while p and p.data != data:
p = p.ltree if p.data > data else p.rtree
return p
def insert(self, data):
'''
插入
'''
if not self.base:
self.base = Tree(data=data)
return
pp = None
p = self.base
while p:
pp = p
p = p.ltree if p.data > data else p.rtree
new_p = Tree(data=data)
if pp.data > data:
pp.ltree = new_p
else:
pp.rtree = new_p
def delete(self, data):
'''
删除
'''
p = self.base
pp = None
while p and (p.data != data):
pp = p
p = p.ltree if p.data > data else p.rtree
if not p: return
if p.ltree and p.rtree:
successor = p.rtree
successor_pp = p
while successor.ltree:
successor_pp = successor
successor = successor.ltree
p.data = successor.da
pp, p = successor_pp, successor
child = p.ltree if p.ltree else p.rtree
if not pp:
self.base = child
elif pp.ltree == p:
pp.ltree = p
else:
pp.rtree = child
if __name__ == '__main__':
tree = Tree(data=3)
bst = BinarySearchTree(tree)
bst.insert(3)
bst.insert(4)
bst.insert(5)
bst.insert(8)
bst.insert(10)
bst.insert(13)
bst.insert(33)
bst.insert(44)
bst.insert(1)
bst.insert(2)
target = bst.find(8)
print("--------------------")
print(target.data)
print(target.ltree)
print(target.rtree.data)
print("++++++++++++++++++++")
bst.delete(8)
print(bst.find(8))