二叉树相关

本文详细探讨了二叉树的递归和非递归深度查找,包括广度优先遍历和深度优先遍历的前序、中序、后序遍历。此外,讲解了二叉搜索树的概念、判断及操作,平衡二叉树的定义和检测,以及LL、RR、LR和RL旋转与再平衡技巧。涉及AVL和红黑树的插入和删除操作,以及获取平衡因子和高度的方法。
摘要由CSDN通过智能技术生成

二叉树

二叉树的递归和非递归深度查找

// 递归
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值