Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的基本数据结构#_C#_.net_Data Structures - Fatal编程技术网

C# C语言中的基本数据结构#

C# C语言中的基本数据结构#,c#,.net,data-structures,C#,.net,Data Structures,我想知道人们如何在不使用基类库实现的情况下在C#中实现以下数据结构:- 链表 哈希表 二叉搜索树 红黑树 B-树 二项式堆 斐波那契堆 以及人们能想到的任何其他基本数据结构 我很好奇,因为我想提高对这些数据结构的理解,如果能在互联网上看到C版本而不是典型的C示例,那就太好了 关于这个问题有一系列的讨论。然而,我自己并没有真正读过这篇文章。我相信.NET的collections框架有一个坏的接口,不能很好地扩展 还有一个我正在调查的图书馆 出于上述原因,我已经有了一个为.NET实现我自己的集合

我想知道人们如何在不使用基类库实现的情况下在C#中实现以下数据结构:-

  • 链表
  • 哈希表
  • 二叉搜索树
  • 红黑树
  • B-树
  • 二项式堆
  • 斐波那契堆
以及人们能想到的任何其他基本数据结构

我很好奇,因为我想提高对这些数据结构的理解,如果能在互联网上看到C版本而不是典型的C示例,那就太好了

关于这个问题有一系列的讨论。然而,我自己并没有真正读过这篇文章。我相信.NET的collections框架有一个坏的接口,不能很好地扩展

还有一个我正在调查的图书馆


出于上述原因,我已经有了一个为.NET实现我自己的集合库的项目,但在第一个基准测试显示即使是一个简单的、非线程安全的通用
Vector
实现也比本机
List
慢之后,我停止了这个项目。由于我已经注意到不会产生任何低效的IL代码,这意味着.NET根本不适合(目前)编写内部数据结构的标准替换,NET framework必须使用一些幕后知识来优化内置集合类。

对于您提到的数据结构,我推荐两种资源: 首先,是.NETFramework源代码(信息可以在ScottGu的博客上找到)

另一个有用的资源是在Codeplex上找到的Wintellect的电源集合


希望这有帮助

这是一个通用的二进制搜索树。我唯一没有做的就是实现IEnumerable,这样就可以使用枚举器遍历树。然而,这应该是相当直截了当的

特别感谢Scott Mitchel的BSTTree文章,我将其用作delete方法的参考

节点类:

    class BSTNode<T> where T : IComparable<T>
    {
        private BSTNode<T> _left = null;
        private BSTNode<T> _right = null;        
        private T _value = default(T);

        public T Value
        {
            get { return this._value; }
            set { this._value = value; }
        }

        public BSTNode<T> Left
        {
            get { return _left; }
            set { this._left = value; }
        }

        public BSTNode<T> Right
        {
            get { return _right; }
            set { this._right = value; }
        }        
    }
类BSTNode,其中T:IComparable
{
私有节点_left=null;
私有节点_right=null;
私有T_值=默认值(T);
公共价值
{
获取{返回此值。_值;}
设置{this.\u value=value;}
}
左公共节点
{
获取{return\u left;}
设置{this._left=value;}
}
公共节点权
{
获取{return\u right;}
设置{this.\u right=value;}
}        
}
和实际的树类:

    class BinarySearchTree<T> where T : IComparable<T>
    {
        private BSTNode<T> _root = null;
        private int _count = 0;

        public virtual void Clear()
        {
            _root = null;
            _count = 0;
        }

        public virtual int Count
        {
            get { return _count; }
        }

        public virtual void Add(T value)
        {
            BSTNode<T> newNode = new BSTNode<T>();
            int compareResult = 0;

            newNode.Value = value;

            if (_root == null)
            {
                this._count++;
                _root = newNode;
            }
            else
            {
                BSTNode<T> current = _root;
                BSTNode<T> parent = null;

                while (current != null)
                {
                    compareResult = current.Value.CompareTo(newNode.Value);

                    if (compareResult > 0)
                    {
                        parent = current;
                        current = current.Left;
                    }
                    else if (compareResult < 0)
                    {
                        parent = current;
                        current = current.Right;
                    }
                    else
                    {
                        // Node already exists
                        throw new ArgumentException("Duplicate nodes are not allowed.");
                    }
                }

                this._count++;

                compareResult = parent.Value.CompareTo(newNode.Value);
                if (compareResult > 0)
                {
                    parent.Left = newNode;
                }
                else
                {
                    parent.Right = newNode;
                }
            }
        }

        public virtual BSTNode<T> FindByValue(T value)
        {
            BSTNode<T> current = this._root;

            if (current == null)
                return null;   // Tree is empty.
            else
            {
                while (current != null)
                {
                    int result = current.Value.CompareTo(value);
                    if (result == 0)
                    {
                        // Found the corrent Node.
                        return current;
                    }
                    else if (result > 0)
                    {
                        current = current.Left;
                    }
                    else
                    {
                        current = current.Right;
                    }
                }

                return null;
            }
        }

        public virtual void Delete(T value)
        {

            BSTNode<T> current = this._root;
            BSTNode<T> parent = null;

            int result = current.Value.CompareTo(value);

            while (result != 0 && current != null)
            {
                if (result > 0)
                {
                    parent = current;
                    current = current.Left;
                }
                else if (result < 0)
                {
                    parent = current;
                    current = current.Right;
                }

                result = current.Value.CompareTo(value);
            }

            if (current == null)
                throw new ArgumentException("Cannot find item to delete.");



            if (current.Right == null)
            {
                if (parent == null)
                    this._root = current.Left;
                else
                {
                    result = parent.Value.CompareTo(current.Value);
                    if (result > 0)
                    {
                        parent.Left = current.Left;
                    }
                    else if (result < 0)
                    {
                        parent.Right = current.Left;
                    }
                }
            }
            else if (current.Right.Left == null)
            {
                if (parent == null)
                    this._root = current.Right;
                else
                {
                    result = parent.Value.CompareTo(current.Value);
                    if (result > 0)
                    {
                        parent.Left = current.Right;
                    }
                    else if (result < 0)
                    {
                        parent.Right = current.Right;
                    }
                }
            }
            else
            {

                BSTNode<T> furthestLeft = current.Right.Left;
                BSTNode<T> furthestLeftParent = current.Right;

                while (furthestLeft.Left != null)
                {
                    furthestLeftParent = furthestLeft;
                    furthestLeft = furthestLeft.Left;
                }

                furthestLeftParent.Left = furthestLeft.Right;

                furthestLeft.Left = current.Left;
                furthestLeft.Right = current.Right;

                if (parent != null)
                {
                    result = parent.Value.CompareTo(current.Value);
                    if (result > 0)
                    {
                        parent.Left = furthestLeft;
                    }
                    else if (result < 0)
                    {
                        parent.Right = furthestLeft;
                    }
                }
                else
                {
                    this._root = furthestLeft;
                }
            }

            this._count--;
        }
    }
}
类二进制搜索树,其中T:IComparable
{
私有节点_root=null;
私有整数_计数=0;
公共虚拟空清除()
{
_root=null;
_计数=0;
}
公共虚拟整数计数
{
获取{return\u count;}
}
公共虚拟空添加(T值)
{
BSTNode newNode=新BSTNode();
int compareResult=0;
newNode.Value=Value;
如果(_root==null)
{
这个._count++;
_根=新节点;
}
其他的
{
BSTNode当前=_根;
bstnodeparent=null;
while(当前!=null)
{
compareResult=current.Value.CompareTo(newNode.Value);
如果(比较结果>0)
{
父项=当前;
电流=电流。左;
}
else if(比较结果<0)
{
父项=当前;
电流=电流。右;
}
其他的
{
//节点已存在
抛出新ArgumentException(“不允许重复节点”);
}
}
这个._count++;
compareResult=parent.Value.CompareTo(newNode.Value);
如果(比较结果>0)
{
parent.Left=newNode;
}
其他的
{
parent.Right=newNode;
}
}
}
公共虚拟节点FindByValue(T值)
{
BSTNode current=此。\ u根;
如果(当前==null)
return null;//树为空。
其他的
{
while(当前!=null)
{
int结果=当前.Value.CompareTo(值);
如果(结果==0)
{
//找到了相应的节点。
回流;
}
否则,如果(结果>0)
{
电流=电流。左;
}
其他的
{
电流=电流。右;
}
}
返回null;
}
}
公共虚拟无效删除(T值)
{
BSTNode current=此。\ u根;
bstnodeparent=null;
int结果=当前.Value.CompareTo(值);
while(结果!=0&¤t!=null)
{
如果(结果>0)
{
父项=当前;
电流=电流。左;
}
否则如果(结果<0)
{
父项=当前;
电流=电流。右;
}
结果=当前.Value.CompareTo(值);
}
如果(当前==null)
抛出新的ArgumentException(“Cann