C#数据结构与算法—红黑树

这篇博客介绍了红黑树的数据结构及其在2-3树中的应用。作者提供了一个C#实现的红黑树类`RBT1<E>`,包括插入元素的方法,并展示了如何保持红黑树的性质。此外,还展示了一个使用红黑树实现的`RBT1Set<E>`类,实现了集合的基本操作。博客涵盖了红黑树的旋转、颜色翻转等关键操作,以及如何在插入过程中平衡树。
摘要由CSDN通过智能技术生成

2—3树
在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述红黑树
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure3
{
    class RBT1<E> where E : IComparable<E>
    {
        private const bool Red = true;
        private const bool Black = false;
        private class Node
        {
            public E e;
            public Node left;
            public Node right;
            public bool color;

            public Node(E e)
            {
                this.e = e;
                left = null;
                right = null;
                color = Red; 
            }
        }
        private Node root;
        private int N;

        public RBT1()
        {
            root = null;
            N = 0;
        }
        public int Count { get { return N; } }
        public bool IsEmpty { get { return N == 0; } }
        private bool IsRed(Node node)
        {
            if (node == null)
                return Black;
            return node.color;
        }
 //左旋转
        private Node LeftRotate(Node node)
        {
            Node x = node.right;
            node.right = x.left;
            x.left = node;
            x.color = node.color;
            node.color = Red;
            return x;
        }

如果两个子路都是红色,则进行颜色的翻转

//颜色翻转
        private void FlipColors(Node node)
        {
            node.color = Red;
            node.left.color = Black;
            node.right.color = Black;
        }

递归添加元素

//递归方法
        public void Add(E e)
        {
            root = Add(root, e);
            root.color = Black;
        }
        //以node为根的树中添加元素,添加元素后返回根结点
        private Node Add(Node node, E e)
        {
            if (node == null)
            {
                N++;
                return new Node(e);//默认为红节点
            }
            if (e.CompareTo(node.e) < 0)
                node.left = Add(node.left, e);
            else if (e.CompareTo(node.e) > 0)
                node.right = Add(node.right, e);
            //如果出现右子节点是红色,左子节点是黑色 进行左旋转
            if(IsRed(node.right)&& !IsRed(node.left))
            {
                node = LeftRotate(node);
            }
            //如果出现连续的左子节点都为红色 进行右旋转
            if(IsRed(node.left)&&IsRed(node.left.left))
            {
                node=RightRoatate(node);
            }
            //如果出现左右节点都是红色,进行颜色翻转
            if(IsRed(node.left)&&IsRed(node.right))
            {
                FlipColors(node);
            }

            return node;
        }

在这里插入图片描述红黑树在集合里的体现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure3
{
    class RBT1Set<E>:ISet<E> where E:IComparable<E>
    {
        private RBT1<E> rbt;
        public RBT1Set()
        {
            rbt = new RBT1<E>();
        }

        public int Count { get { return rbt.Count; } }

        public bool IsEmpty { get { return rbt.IsEmpty; } }

        public void Add(E e)
        {
            rbt.Add(e);
        }

        public bool Contains(E e)
        {
            return rbt.Contains(e); 
        }

        public void Remove(E e)
        {
            Console.WriteLine("待实现"); 
        }
        public int MaxHeight()
        {
            return rbt.MaxHeight();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值