Python数据结构之二叉树——树的变种
- 二叉树:树的变种,主要节约树在存储时的空间浪费。
- 二叉树的公式
1. 总结点为 n,那么深度就是 2(n)-1。
2. 深度为n,总结点为 log2(n+1)。
3. 度为0的节点 n0 = n2+1
4. 深度在 k 时,最多有 2(k-1) 个元素。 - 二叉树的遍历方式
1. 前序遍历:根左右;
2. 中序遍历:左根右;
3. 后序遍历:左右跟; - 二叉树的增加和删除函数;
代码部分
- 二叉树节点定义
class Tree_Node:
def __init__(self, value):
self.data = value
self.left_node = None
self.right_node = None
- 二叉树的创建
def Create_Tree(ptr: Tree_Node, value: int):
new_node = Tree_Node(value=value)
if not ptr:
ptr = new_node
return ptr
else:
current = ptr
while current:
backup = current
if current.data > value:
current = current.left_node
else:
current = current.right_node
if backup.data > value:
backup.left_node = new_node
else:
backup.right_node = new_node
return ptr
- 前序遍历
def preorder(root_ptr):
if root_ptr:
print(root_ptr.data, end="\t")
preorder(root_ptr.left_node)
preorder(root_ptr.right_node)
- 中序遍历
def inorder(root_ptr):
if root_ptr:
inorder(root_ptr.left_node)
print(root_ptr.data, end="\t")
inorder(root_ptr.right_node)
- 后序遍历
def postorder(root_ptr):
if root_ptr:
postorder(root_ptr.left_node)
postorder(root_ptr.right_node)
print(root_ptr.data, end="\t")
- 增加结点
- 寻找节点是否存在
def find_ptr(root_ptr, find_value):
sums = 0
while True:
sums += 1
if root_ptr is None:
return None
elif root_ptr.data > find_value:
root_ptr = root_ptr.left_node
elif root_ptr.data < find_value:
root_ptr = root_ptr.right_node
else:
print("\n寻找次数", sums)
return root_ptr
- 添加节点
def add_node(ptr, value):
if find_ptr(ptr, value):
print("该节点已经存在了!")
else:
ptr = Create_Tree(ptr, value)
inorder(ptr)```
- 删除节点
- 如果是叶子结点,将这个节点的父亲节点指向None;
- 如果只要左节点或者右节点,就将它直接放在父亲节点的左节点;
- 如果拥有左右节点,就通过递归,将节点添加到
def del_node(root_ptr, value):
if not root_ptr:
return None
else:
if root_ptr.data > value:
root_ptr.left_node = del_node(root_ptr.left_node, value)
elif root_ptr.data < value:
root_ptr.right_node = del_node(root_ptr.right_node, value)
else:
if root_ptr.left_node is None or root_ptr.right_node is None:
root_ptr = root_ptr.left_node if root_ptr.left_node else root_ptr.right_node
else:
temp = root_ptr.right_node
while temp.left_node:
temp = temp.left_node
root_ptr.data = temp.data
return root_ptr
- 得出二叉树的深度
def depth(root_ptr):
if root_ptr is None:
return 0
else:
h1 = depth(root_ptr.left_node)
r1 = depth(root_ptr.right_node)
if h1 > r1:
return h1+1
else:
return r1+1
总结代码
class Tree_Node:
def __init__(self, value):
self.data = value
self.left_node = None
self.right_node = None
def Create_Tree(ptr: Tree_Node, value: int):
new_node = Tree_Node(value=value)
if not ptr:
ptr = new_node
return ptr
else:
current = ptr
while current:
backup = current
if current.data > value:
current = current.left_node
else:
current = current.right_node
if backup.data > value:
backup.left_node = new_node
else:
backup.right_node = new_node
return ptr
def find_ptr(root_ptr, find_value):
sums = 0
while True:
sums += 1
if root_ptr is None:
return None
elif root_ptr.data > find_value:
root_ptr = root_ptr.left_node
elif root_ptr.data < find_value:
root_ptr = root_ptr.right_node
else:
print("\n寻找次数", sums)
return root_ptr
def add_node(ptr, value):
if find_ptr(ptr, value):
print("该节点已经存在了!")
else:
ptr = Create_Tree(ptr, value)
inorder(ptr)
def depth(root_ptr):
if root_ptr is None:
return 0
else:
h1 = depth(root_ptr.left_node)
r1 = depth(root_ptr.right_node)
if h1 > r1:
return h1+1
else:
return r1+1
def del_node(root_ptr, value):
if not root_ptr:
return None
else:
if root_ptr.data > value:
root_ptr.left_node = del_node(root_ptr.left_node, value)
elif root_ptr.data < value:
root_ptr.right_node = del_node(root_ptr.right_node, value)
else:
if root_ptr.left_node is None or root_ptr.right_node is None:
root_ptr = root_ptr.left_node if root_ptr.left_node else root_ptr.right_node
else:
temp = root_ptr.right_node
while temp.left_node:
temp = temp.left_node
root_ptr.data = temp.data
return root_ptr
def preorder(root_ptr):
if root_ptr:
print(root_ptr.data, end="\t")
preorder(root_ptr.left_node)
preorder(root_ptr.right_node)
def inorder(root_ptr):
if root_ptr:
inorder(root_ptr.left_node)
print(root_ptr.data, end="\t")
inorder(root_ptr.right_node)
def postorder(root_ptr):
if root_ptr:
postorder(root_ptr.left_node)
postorder(root_ptr.right_node)
print(root_ptr.data, end="\t")
if __name__ == '__main__':
data_array = [6, 3, 5, 4, 7, 8, 9, 2]
root = None
for i in range(len(data_array)):
root = Create_Tree(root, data_array[i])
print("前序遍历: ")
preorder(root)
print("\n中序遍历: ")
inorder(root)
print("\n后序遍历: ")
postorder(root)
print("寻找节点: ", find_ptr(root, 8).data)
print("添加节点: 8")
add_node(root, 7)
print("添加节点: 99")
add_node(root, 99)
root = del_node(root, 4)
print()
inorder(root)
"""
数组中的数据: [0, 6, 3, 5, 4, 7, 8, 9, 2]
数组保存的二叉树数据: [0, 6, 3, 7, 2, 5, 0, 8, 0, 0, 4, 0, 0, 0, 0, 9]
"""
前序遍历:
6 3 2 5 4 7 8 9
中序遍历:
2 3 4 5 6 7 8 9
后序遍历:
2 4 5 3 9 8 7 6
寻找次数 3
寻找节点: 8
添加节点: 8
寻找次数 2
该节点已经存在了!
添加节点: 99
2 3 4 5 6 7 8 9 99
2 3 5 6 7 8 9 99
进程已结束,退出代码0