c#平衡二叉树

在 C# 中实现平衡二叉树可以使用 AVL 树或红黑树等算法。下面我将展示一个简单的 AVL 树的实现。

首先,我们需要定义一个节点类 AVLNode 用于表示 AVL 树的节点:

class AVLNode
{
    public int data;
    public int height;
    public AVLNode left;
    public AVLNode right;

    public AVLNode(int value)
    {
        data = value;
        height = 1;
        left = null;
        right = null;
    }
}

然后,我们创建一个 AVL 树类 AVLTree,实现插入、删除和平衡等操作:

class AVLTree
{
    private AVLNode root;

    public AVLTree()
    {
        root = null;
    }

    public int Height(AVLNode node)
    {
        if (node == null)
        {
            return 0;
        }
        return node.height;
    }

    public int GetBalanceFactor(AVLNode node)
    {
        if (node == null)
        {
            return 0;
        }
        return Height(node.left) - Height(node.right);
    }

    public AVLNode RotateRight(AVLNode y)
    {
        AVLNode x = y.left;
        AVLNode T2 = x.right;

        x.right = y;
        y.left = T2;

        y.height = 1 + Math.Max(Height(y.left), Height(y.right));
        x.height = 1 + Math.Max(Height(x.left), Height(x.right));

        return x;
    }

    public AVLNode RotateLeft(AVLNode x)
    {
        AVLNode y = x.right;
        AVLNode T2 = y.left;

        y.left = x;
        x.right = T2;

        x.height = 1 + Math.Max(Height(x.left), Height(x.right));
        y.height = 1 + Math.Max(Height(y.left), Height(y.right));

        return y;
    }

    public AVLNode Insert(AVLNode node, int value)
    {
        if (node == null)
        {
            return new AVLNode(value);
        }

        if (value < node.data)
        {
            node.left = Insert(node.left, value);
        }
        else if (value > node.data)
        {
            node.right = Insert(node.right, value);
        }
        else
        {
            // Duplicate values are not allowed in AVL tree
            return node;
        }

        node.height = 1 + Math.Max(Height(node.left), Height(node.right));

        int balance = GetBalanceFactor(node);

        if (balance > 1 && value < node.left.data)
        {
            return RotateRight(node);
        }

        if (balance < -1 && value > node.right.data)
        {
            return RotateLeft(node);
        }

        if (balance > 1 && value > node.left.data)
        {
            node.left = RotateLeft(node.left);
            return RotateRight(node);
        }

        if (balance < -1 && value < node.right.data)
        {
            node.right = RotateRight(node.right);
            return RotateLeft(node);
        }

        return node;
    }

    public void Insert(int value)
    {
        root = Insert(root, value);
    }

    public void InOrderTraversal(AVLNode node)
    {
        if (node != null)
        {
            InOrderTraversal(node.left);
            Console.Write(node.data + " ");
            InOrderTraversal(node.right);
        }
    }
}

在这个示例中,我们实现了 AVL 树的插入操作,并且包括了平衡因子的计算和旋转操作。实际上,平衡二叉树的实现还涉及删除、搜索和其他平衡操作等,这里只是一个简单的示例。在实际开发中,你可能需要根据需求进一步完善代码。

希望这个示例能够帮助你理解如何在 C# 中实现平衡二叉树。

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值