二叉树和二叉搜索树
二叉树遍历
class Bitree:
def __init__(self, data):
self.data = data
self.lchild = None
self.rchild = None
a = Bitree('A')
b = Bitree('B')
c = Bitree('C')
d = Bitree('D')
e = Bitree('E')
f = Bitree('F')
g = Bitree('G')
e.lchild = a
e.rchild = g
a.rchild = c
c.lchild = b
c.rchild = d
g.rchild = f
root = e
# 前序遍历
def per_order(tree):
if tree:
print(tree.data, end=',')
per_order(tree.lchild)
per_order(tree.rchild)
# per_order(root)
# E,A,C,B,D,G,F,
# 后序遍历
def post_order(tree):
if tree:
post_order(tree.lchild)
post_order(tree.rchild)
print(tree.data, end=',')
post_order(root)
# B,D,C,A,F,G,E,
#层次查找
from collections import deque
def leve_order(tree):
queue = deque
queue.append(tree)
while len(queue) > 0:
node = queue.popleft()
print(node.data, end=',')
if node.lchild:
queue.append(node.lchild)
if node.rchild:
queue.append(node.rchild)
二叉搜索树
二叉搜索树是二叉树满足以下性质:
如果y是x的左子节点,则有y.key<=x.key;
如果y是x的右子节点,则有y.key>=x.key;
class BistreeNode:
def __init__(self, data):
self.data = data
self.lchild = None
self.rchild = None
self.parent = None
class BST:
def __init__(self, li):
self.root = None
if li:
for i in li:
self.insert_no_Recursive(i)
def insert_no_Recursive(self, val):
if not self.root:
self.root = BistreeNode(val)
p = self.root
while True:
# 如果插入的值小于当前节点的值,那么就判断
# 如果有左子节点,就往当前节点的左子节点上跳,如果没有就把要插入的值放到当前节点的左子节点上
if val < p.data:
if p.lchild:
p = p.lchild
else:
p.lchild = BistreeNode(val)
p.lchild.parent = p
return
elif val > p.data:
if p.rchild:
p = p.rchild
else:
p.rchild = BistreeNode(val)
p.rchild.parent = p
return
else:
return
def insert_recursive(self, node, val):
if not node:
node = BistreeNode(val)
if val < node.data:
node.lchild = self.insert_recursive(node.lchild, val)
node.lchild.parent = node
elif val > node.data:
node.rchild = self.insert_recursive(node.rchild, val)
node.rchild.parent = node
return node
# 前序遍历
def per_order(self, tree):
if tree:
print(tree.data, end=',')
self.per_order(tree.lchild)
self.per_order(tree.rchild)
# 后序遍历
def post_order(self, tree):
if tree:
self.post_order(tree.lchild)
self.post_order(tree.rchild)
print(tree.data, end=',')
# 中序遍历
def mid_order(self,tree):
if tree:
self.mid_order(tree.lchild)
print(tree.data, end=',')
self.mid_order(tree.rchild)
def query(self, node, val):
if not node:
return False
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_recursiive(self, node, val):
if not node:
return False
while True:
if node.data < val:
if node.rchild:
node = node.rchild
else:
raise ValueError('val is not exist')
elif node.data > val:
if node.lchild:
node = node.lchild
else:
raise ValueError('val is not exist')
else:
return node
def __remove_node_1(self, node):
# 要删除的节点是叶子节点
if not node.parent:
self.root = None
if node.parent.lchild and node == node.parent.lchild:
node.parent.lchild = None
else:
node.parent.rchild = None
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
def __remove_node_22(self, node):
# 只有一个右叶子节点
if not node.parent:
self.root = node.rchild
node.rchild.parent = None
elif node == node.parent.rchild:
node.parent.rchild = node.rchild
node.rchild.parent = node.parent
else:
node.parent.lchild = node.rchild
node.rchild.parent = node.parent
def delete(self, val):
if self.root:
node = self.query_no_recursiive(self.root, val)
if not node:
return False
if not node.lchild and not node.rchild:
self.__remove_node_1(node)
elif not node.lchild:
self.__remove_node_22(node)
elif not node.rchild:
self.__remove_node_21(node)
else:
min_node = node.lchild
while min_node.lchild:
min_node = min_node.lchild
node.data = min_node.data
if not min_node.rchild:
self.__remove_node_21(min_node)
else:
self.__remove_node_1(min_node)
bst = BST([4,5,9,7,2,1,3,4,2,7,8])
bst.insert_recursive(bst.root, 0)
# print(bst.per_order(bst.root))
bst.per_order(bst.root)
# print(bst.query(bst.root, 5).data)
bst.delete(3)
print('')
bst.mid_order(bst.root)