![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b30fba6f7b209940d36e5e2ef1e6b03d.png#pic_center)
class BiTreeNode:
def __init__(self,data):
self.data = data
self.lchild = None
self.rchild = None
self.parent = None
def __repr__(self):
return self.data
class BST:
def __init__(self,li = None):
self.root = None
if li:
for val in li:
self.insert_no_rec(val)
def insert(self,node,val):
if not node:
node = BiTreeNode(val)
elif val < node.data:
node.lchild = self.insert(node.lchild,val)
node.lchild.parent = node
elif val >node:
node.rchild = self.insert(node.rchild, val)
node.rchild.parent = node
return node
def insert_no_rec(self,val):
p = self.root
if not p:
self.root = BiTreeNode(val)
return
while 1:
if val <p.data:
if p.lchild:
p = p.lchild
else:
p.lchild = BiTreeNode(val)
p.lchild.parent=p
elif val>p.data:
if p.rchild:
p = p.rchild
else:
p.rchild = BiTreeNode(val)
p.rchild.parent=p
else:
return
def query(self,node,val):
if not node:
return None
if node.data < val:
return self.query(node.rchild,val)
elif node.data > val:
return self.query(node.lchild,val)
else:
return node
def query_no_rec(self,val):
p = self.root
while p:
if p.data <val:
p = p.rchild
elif p.data > val:
p = p.lchild
else:
return p
else:
return None
def pre_order(self, root):
if root:
print(root.data, end=',')
self.pre_order(root.lchild)
self.pre_order(root.rchild)
def in_order(self, root):
if root:
self.in_order(root.lchild)
print(root.data, end=',')
self.in_order(root.rchild)
def post_order(self, root):
if root:
self.post_order(root.lchild)
self.post_order(root.rchild)
print(root.data, end=',')
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/84dfc00c6ba2a1c34e2f19fe9f24ab92.png#pic_center)
def __remove_node_1(self,node):
if not node.parent:
self.root = None
if node == node.parent.lchild:
node.parent.lchild = None
else:
node.parent.rchild = None
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8e942f76b3150abe9c2d899a46a8c96d.png#pic_center)
def __remove_node_21(self,node):
if not node.parent:
self.root = node.lchild
node.lchild.parent= None
elif node ==node.parent.lchild:
node.parent.lchild = node.lchild
node.lchild.parent = node.parent
else:
node.parent.rchild = node.lchild
node.lchild.parent = node.parent
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/22c3c848555f5503df00ff91c529abbf.png#pic_center)
def __remove_node_22(self,node):
if not node.parent:
self.root = node.rchild
node.rchild.parent= None
elif node ==node.parent.lchild:
node.parent.lchild = node.rchild
node.rchild.parent = node.parent
else:
node.parent.rchild = node.rchild
node.rchild.parent = node.parent
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/04a69e0e7160998332a01c9b7b2c492d.png#pic_center)
def delete(self,val):
if self.root:
node = self.query_no_rec(val)
if not node :
print('not there exist')
return False
if not (node.lchild and node.rchild):
self.__remove_node_1(node)
elif not node.rchild:
self.__remove_node_21(node)
elif not node.lchild:
self.__remove_node_22()
else:
min_node = node.rchild
while min_node.lchild:
min_node = min_node.lchild
node.data = min_node.data
if min_node .rchild:
self.__remove_node_22(min_node)
else:
self.__remove_node_1(min_node)