Is there a built in binary search tree in .NET 4.0, or do I need to build this abstract data type from scratch?
I'm not sure what exactly you mean with 'tree', but you can do binary searchs on the List class.
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 );
The answer is: No.
There are implementations available though. Take a look at the following link:
There is no implementation of a binary tree in .NET framework. I am unaware of what data structure libraries are available, but I am sure your favourite search engine will help with that.
The C5 collections library (see http://www.itu.dk/research/c5/) includes TreeDictionary<>
classes with balanced red-black binary trees. Note: I have not used this library yet, as the work I do needs nothing more that the standard .NET collections.
I think the SortedSet{T} class is what you're looking for.
From this CodeProject article:
It is implemented using a self-balancing red-black tree that gives a performance complexity of O(log n) for insert, delete, and lookup. 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.
Thanx to herzmeister der welten, I now know there are! 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();
}
}
}