Archive

Archive for the ‘Data Structures’ Category

Binary trees in C#

June 24, 2011 Leave a comment

Great C# implementation of a binary tree by Roni Schuetz.

As simple to use as this:

    var x = new BinaryTree<int>();
    x.Add(5);
    x.Add(7);
    Console.WriteLine(x.Count);

Or this:

    var y = new BinaryTree<Taxicab>();
    y.Add(new Taxicab() { number = 3 });
    y.Add(new Taxicab() { number = 1729 });
    Console.WriteLine(y.Count);

    class Taxicab:IComparable<Taxicab>
    {
        public int number { get; set; }
        public int frequency { get; set; }
        public int CompareTo(Taxicab other)
        {
            if (this.number < other.number)
                return -1;
            else if (this.number == other.number)
                return 0;
            else
                return 1;
        }
    }

-Krip

Advertisements