二叉树
二叉树的递归和非递归深度查找
// 递归
def max_depth(root: BinaryTree):
if root is None:
return 0
return max(max_depth(root.left), max_depth(root.right)) + 1
// 非递归
def max_depth_not_recursion(root: BinaryTree):
if root is None:
return 0
dep: int = 0
queue = Queue()
queue.put(root)
while not queue.empty():
count: int = 0
current: int = queue.qsize()
while count < current:
tree: BinaryTree = queue.get()
if tree.left is not None:
queue.put(tree.left)
if tree.right is not None:
queue.put(tree.right)
count += 1
dep += 1
return dep
二叉树的广度优先遍历和深度优先遍历
前序中序后序遍历
- 前序遍历:根左右的顺序遍历
- 中序遍历:左根右的顺序遍历
- 后序遍历:左右根的顺序遍历
二叉搜索树(BST)
什么是二叉搜索树
它或者是一棵空树,或者是具有下列性质的二叉树:若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。
判断是否是二叉搜索树
def is_bst(root: BinaryTree, min_data: int, max_data: int):
if root is None:
return True
if root.data <= min_data or root.data >= max_data:
return False
left = is_bst(root.left, min_data, root.data)
right = is_bst(root.right, root.data, max_data)
return left and right
二叉搜索树查询
def search_bst(root: BinaryTree, data: int):
while root is not None:
tree_data = root.data
if tree_data == data:
return root
elif tree_data < data:
root = root.right
else:
root = root.left
return None
平衡二叉树
什么是平衡二叉树
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1
判断是否是平衡二叉树
def is_balanced(root: BinaryTree):
queue = Queue()
queue.put(root)
while not queue.empty():
tree: BinaryTree = queue.get()
if abs(max_depth_not_recursion(tree.left) - max_depth_not_recursion(tree.right)) > 1:
return False
if tree.left is not None:
queue.put(tree.left)
if tree.right is not None:
queue.put(tree.right)
return True
LL型
def left_left_rotate(root: BinaryTree):
old_left = root.left
root.left = old_left.right
old_left.right = root
root.height = get_height(root)
old_left.height = get_height(old_left)
return old_left
RR型
def right_right_rotate(root: BinaryTree):
old_right = root.right
root.right = old_right.left
old_right.left = root
root.height = get_height(root)
old_right.height = get_height(old_right)
return old_right
LR型
def left_right_rotate(root: BinaryTree):
root.left = right_right_rotate(root.left)
return left_left_rotate(root)
RL型
def right_left_rotate(root: BinaryTree):
root.right = left_left_rotate(root.right)
return right_right_rotate(root)
获取平衡因子
def get_balanced(root: BinaryTree):
if root is None:
return 0
return get_height(root.left) - get_height(root.right)
获取高度
def get_height(root: BinaryTree):
if root is None:
return 0
return max(height(root.left), height(root.right)) + 1
def height(root: BinaryTree):
if root is None:
return 0
return root.height
再平衡
def avl(root: BinaryTree):
balanced = get_balanced(root)
if 1 >= balanced >= -1:
return root
if balanced > 1:
if get_balanced(root.left) == 1:
return left_left_rotate(root)
else:
return left_right_rotate(root)
else:
if get_balanced(root.right) == 1:
return right_left_rotate(root)
else:
return right_right_rotate(root)
插入
def add(root: BinaryTree, data: int):
if root.data != data:
if root.data > data:
if root.left is None:
root.left = BinaryTree(data)
else:
root.left = add(root.left, data)
else:
if root.right is None:
root.right = BinaryTree(data)
else:
root.right = add(root.right, data)
root.height = get_height(root)
return avl(root)
删除
def delete(root: BinaryTree, data: int):
if root is None:
return None
if root.get_data() != data:
if root.get_data() > data:
root.left = delete(root.left, data)
else:
root.right = delete(root.right, data)
root.height = get_height(root)
return avl(root)
else:
if root.left is None and root.right is None:
return None
elif root.left is not None and root.right is None:
return root.left
elif root.left is None and root.right is not None:
return root.right
else:
balanced = get_balanced(root)
_new_value = None
if balanced >= 0:
_new_value = max_value(root.left)
root.left = delete(root.left, _new_value)
else:
_new_value = min_value(root.right)
root.right = delete(root.right, _new_value)
new_root = BinaryTree(_new_value)
new_root.left = root.left
new_root.right = root.right
new_root.height = get_height(new_root)
return new_root