views:

135

answers:

7

Is there a built in binary search tree in .NET 4.0, or do I need to build this abstract data type from scratch?

+1  A: 

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 );
Trap
Thanx - this was new info for me - thanx
BennySkogberg
+1  A: 

The answer is: No.

There are implementations available though. Take a look at the following link:

Binary Tree in C#

Leniel Macaferi
Strange I didn't get that suggestion while asking my question on Binary Search Trees. Thanx Leniel!
BennySkogberg
+1  A: 

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.

btlog
My favorite search engine didn't provide me with the links I wanted to see. I got discussions and blogs instead of code. When I found the code, it was incomplete or proprietary. But Leniel's link got me where I wanted. Thanx for your effort btlog!
BennySkogberg
+1  A: 

Maybe these articles can help:

  1. Binary Trees and BSTs
  2. Building a Better Binary Search
Oliver
Thank, I've tried to read them before - but I felt they were to extensive for my purpose. However, it might not be such a bad idea to read them through now when I have the time :)
BennySkogberg
+1  A: 

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.

Dr Herbie
Nice one. Red-Black trees are a little different than ordinary BinarySearchTrees, but still a very nice algorithm! I'll bookmark that link right away, and save it on my XMarks account :)Thanx Dr Herbie.
BennySkogberg
+1  A: 

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.

herzmeister der welten
Thanx - this is just wat I looked for! There is, I understand now, a built in BinarySearchTree in .NET.
BennySkogberg
A: 

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();
        }
    }
}
BennySkogberg