算法导论-基于 C# 的红黑树扩展数据结构实现

基于 C# 的红黑树扩展数据结构实现技术文档

简介

红黑树(Red-Black Tree)是一种自平衡二叉搜索树,通过重新着色和旋转操作,在插入和删除节点时保持平衡,从而保证查找、插入和删除操作的时间复杂度为 O ( log ⁡ n ) O(\log n) O(logn)。本技术文档详细介绍了如何在C#中实现红黑树,并扩展其功能以支持更多操作。

红黑树的特性
  1. 每个节点要么是红色,要么是黑色。
  2. 根节点是黑色。
  3. 每个叶子节点(NIL节点)是黑色。
  4. 如果一个节点是红色,则它的两个子节点都是黑色。
  5. 从一个节点到该节点的所有后代叶子节点的简单路径上,均包含相同数目的黑色节点。
代码实现
节点类定义
public enum Color {
    Red, Black }

public class RedBlackTreeNode<TKey, TValue>
{
   
    public TKey Key {
    get; set; }
    public TValue Value {
    get; set; }
    public Color NodeColor {
    get; set; }
    public RedBlackTreeNode<TKey, TValue> Left {
    get; set; }
    public RedBlackTreeNode<TKey, TValue> Right {
    get; set; }
    public RedBlackTreeNode<TKey, TValue> Parent {
    get; set; }

    public RedBlackTreeNode(TKey key, TValue value)
    {
   
        Key = key;
        Value = value;
        NodeColor = Color.Red;
        Left = null;
        Right = null;
        Parent = null;
    }
}
红黑树类定义
public class RedBlackTree<TKey, TValue> where TKey : IComparable<TKey>
{
   
    private RedBlackTreeNode<TKey, TValue> root;

    public void Insert(TKey key, TValue value)
    {
   
        RedBlackTreeNode<TKey, TValue> newNode = new RedBlackTreeNode<TKey, TValue>(key, value);
        if (root == null)
        {
   
            root = newNode;
            root.NodeColor = Color.Black;
        }
        else
        {
   
            Insert(root, newNode);
            FixInsert(newNode);
        }
    }

    private void Insert(RedBlackTreeNode<TKey, TValue> current, RedBlackTreeNode<TKey, TValue> newNode)
    {
   
        int compare = newNode.Key.CompareTo(current.Key);
        if (compare < 0)
        {
   
            if (current.Left == null)
            {
   
                current.Left = newNode;
                newNode.Parent = current;
            }
            else
            {
   
                Insert(current.Left, newNode);
            }
        }
        else
        {
   
            if (current.Right == null)
            {
   
                current.Right = newNode;
                newNode.Parent = current;
            }
            else
            {
   
                Insert(current.Right, newNode);
            }
        }
    }

    private void FixInsert(RedBlackTreeNode<TKey, TValue> node)
    {
   
        while (node != root && node.Parent.NodeColor == Color.Red)
        {
   
            if (node.Parent == node.Parent.Parent.Left)
            {
   
                RedBlackTreeNode<TKey, TValue> uncle = node.Parent.Parent.Right;
                if (uncle != null && uncle.NodeColor == Color.Red)
                {
   
                    node.Parent.NodeColor = Color.Black;
                    uncle.NodeColor = Color.Black;
                    node.Parent.Parent.NodeColor = Color.Red
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东城十三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值