12

Is there a built in AVL Tree in the .NET libraries?

I searched but didn't find any.

  • If there is, then where? what namespace?
  • If not, is there any good implementation for AVL Trees in C#?
  • If also not! then is there an easy way to get it done? I know how it works and have built one in native C++ before, but now I have no time and am afraid of bad performance if I did it myself.
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
  • 1
    What are you trying to achieve? Perhaps there is a built in collection type that will suit your needs. – Oded Jan 07 '12 at 09:33
  • I need a balanced hierarchy, a normal Binary tree is not fitting because it's not balanced, any suggestions would be most welcome though :) – Tamer Shlash Jan 07 '12 at 09:35
  • You are not explaining what you are going to _do_ with your data structure, just giving requirements _for_ a data structure. Giving higher level requirements is more helpful. – Oded Jan 07 '12 at 09:39
  • You're right, I need a hierarchial data structure in which each node (A bounding Shape, assume) contains it's child nodes, also the hierarchy should be balanced to get the best performance, else it might end up with a worst-case (linear tree for example). – Tamer Shlash Jan 07 '12 at 09:47
  • This comment trail is running in a circle. AVL-Tree is an implementation, the (std) library offers collections named and classified on functionality. SortedDictionary is based on a Tree but that could change... – H H Jan 07 '12 at 10:23

3 Answers3

16

You can use a System.Collections.Generic.SortedSet<T>. I think it is implemented using a red-black tree which is very similar to an AVL tree.

Josef Pfleger
  • 74,165
  • 16
  • 97
  • 99
3

A quick search found an implementation here. The code looks clean, but I haven't tried it.

If nothing else, you could do a quick performance test against SortedSet<T> (as suggested by @Josef) to see if there's any difference for your use case.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
1

a C# implementation can be found @ http://code.google.com/p/self-balancing-avl-tree/ . concat and split operations are also implemented.

cos
  • 97
  • 1