简体   繁体   English

.NET 4.0 中有内置的二叉搜索树吗?

[英]Is there a built-in Binary Search Tree in .NET 4.0?

Is there a built-in binary search tree in .NET 4.0, or do I need to build this abstract data type from scratch? .NET 4.0 中是否有内置的二叉搜索树,还是我需要从头开始构建这种抽象数据类型?

Edit编辑

This is about the binary search tree specifically, and not abstract data type "trees" in general.这具体是关于二叉搜索树,而不是一般的抽象数据类型“树”。

I think the SortedSet<T> class in System.Collections.Generic is what you're looking for.我认为System.Collections.Generic中的SortedSet<T>类是您正在寻找的。

From this CodeProject article :从此CodeProject 文章

It is implemented using a self-balancing red-black tree that gives a performance complexity of O(log n) for insert, delete, and lookup.它是使用自平衡红黑树实现的,该树为插入、删除和查找提供了O(log n)的性能复杂度。 It is used to keep the elements in sorted order, to get the subset of elements in a particular range, or to get the Min or Max element of the set.它用于保持元素排序,获取特定范围内元素的子集,或获取集合的最小最大元素。

Source code https://github.com/dotnet/corefx/blob/master/src/System.Collections/src/System/Collections/Generic/SortedSet.cs源代码https://github.com/dotnet/corefx/blob/master/src/System.Collections/src/System/Collections/Generic/SortedSet.cs

Five years after I asked the question I realized that there is indeed a built in Binary Search Tree in .NET 4.0.在我提出这个问题五年后,我意识到 .NET 4.0 中确实有一个内置的二叉搜索树。 It has probably been added later on, and works as expected.它可能是稍后添加的,并且可以按预期工作。 It self-balances (traversing) after each insert which decrease performance on adding a large range of items.它在每次插入后进行自我平衡(遍历),这会降低添加大量项目时的性能。

The SortedDictionary<TKey, TValue> Class has the following remarks: SortedDictionary<TKey, TValue>类有以下备注:

The SortedDictionary generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. SortedDictionary 泛型类是具有 O(log n) 检索的二叉搜索树,其中 n 是字典中的元素数。 In this respect, it is similar to the SortedList generic class.在这方面,它类似于 SortedList 泛型类。 The two classes have similar object models, and both have O(log n) retrieval.这两个类具有相似的对象模型,并且都具有 O(log n) 检索。

No, .NET does not contain a Binary Search Tree .不,.NET 不包含二叉搜索树 It does contain a Red-Black Tree which is a specialized kind of Binary Search Tree in which each node is painted red or black and there are certain rules using these colours which keep the tree balanced and allows the tree to guarantee O(logn) search times.它确实包含一个红黑树,它是一种特殊的二叉搜索树,其中每个节点都被涂成红色或黑色,并且使用这些颜色有一些规则可以保持树的平衡并允许树保证O(logn)搜索次。 A standard Binary Search Tree cannot guarantee these search times.标准的二叉搜索树不能保证这些搜索时间。

The class is called a SortedSet<T> and was introduced in .NET 4.0.该类称为SortedSet<T>并在 .NET 4.0 中引入。 You can look at it's source code here .你可以在这里查看它的源代码。 Here is an example of it's use:这是它的使用示例:

// Created sorted set of strings.
var set = new SortedSet<string>();

// Add three elements.
set.Add("net");
set.Add("net");  // Duplicate elements are ignored.
set.Add("dot");
set.Add("rehan");

// Remove an element.
set.Remove("rehan");

// Print elements in set.
foreach (var value in set)
{
    Console.WriteLine(value);
}

// Output is in alphabetical order:
// dot
// net

可以在http://code.google.com/p/self-balancing-avl-tree/上找到一个C#平衡的AVL二叉树。它还实现了对数连接和拆分操作

The C5 collections library (see http://www.itu.dk/research/c5/ ) includes TreeDictionary<> classes with balanced red-black binary trees. C5 集合库(参见http://www.itu.dk/research/c5/ )包括具有平衡红黑二叉树的TreeDictionary<>类。 Note: I have not used this library yet, as the work I do needs nothing more that the standard .NET collections.注意:我还没有使用过这个库,因为我所做的工作只需要标准的 .NET 集合。

The answer is: No. 答案是不。

There are implementations available though. 虽然有可用的实现。 Take a look at the following link: 看看以下链接:

Binary Tree in C# C#中的二叉树

Thanx to herzmeister der welten , I now know there are!感谢 herzmeister der welten ,我现在知道有! I tried it and it really worked!我试过了,真的很管用!

namespace Tree
{
    public partial class Form1 : Form
    {
        private SortedSet<int> binTree = new SortedSet<int>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Insert(int no)
        {
            binTree.Add(no);
        }

        private void Print()
        {
            foreach (int i in binTree)
            {
                Console.WriteLine("\t{0}", i);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Insert(Int32.Parse(tbxValue.Text));
            tbxValue.Text = "";
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Print();
        }
    }
}

I'm not sure what exactly you mean with 'tree', but you can do binary searchs on the List class.我不确定您对“树”的确切含义,但您可以在 List 类上进行二进制搜索。

public int BinarySearch( T item );
public int BinarySearch( T item, IComparer<T> comparer );
public int BinarySearch( int index, int count, T item, IComparer<T> comparer );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM