Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of Red-Black Tree in C#

I'm looking for an implementation of a Red-Black Tree in C#, with the following features:

  • Search, Insert and Delete in O(log n).
  • Members type should be generic.
  • Support in Comparer(T), for sorting T by different fields in it.
  • Searching in the tree should be with the specific field, so it won't accept T, but it'll accept the field type sorting it.
  • Searching shouldn't be only exact value. Should support searching the lower/higher one.

Thank you.

like image 752
Alon Gubkin Avatar asked Nov 09 '09 19:11

Alon Gubkin


People also ask

What is red-black tree with example?

A Red Black Tree is a category of the self-balancing binary search tree. It was created in 1972 by Rudolf Bayer who termed them "symmetric binary B-trees." A red-black tree is a Binary tree where a particular node has color as an extra attribute, either red or black.

How is node balancing implemented in red-black trees?

Red-black trees are a fairly simple and very efficient data structure for maintaining a balanced binary tree. The idea is to strengthen the representation invariant so a tree has height logarithmic in n. To help enforce the invariant, we color each node of the tree either red or black.

What is red-black tree used for?

4.3. Functional Programming. RB trees are used in functional programming to construct associative arrays. In this application, RB trees work in conjunction with 2-4 trees, a self-balancing data structure where every node with children has either two, three, or four child nodes.


2 Answers

You mostly just described SortedDictionary<T, U>, except for the next-lowest/next-highest value binary search, which you could implement on your own without much difficulty.

Are there specific reasons that SortedDictionary is insufficient for you?

like image 93
mqp Avatar answered Oct 17 '22 21:10

mqp


Rip the TreeSet from C5 collection libs.

like image 27
rama-jka toti Avatar answered Oct 17 '22 20:10

rama-jka toti