View Full Version : Red-Black Trees

grapes911

Apr 22, 2006, 12:15 PM

I have to implement a red-black tree in C++ for a project, but I don't know much about red-black trees. I've been trying to do my research for a few days now, but I still feel pretty lost.

1. Here is what I know:

a. They are binary trees.

b. Every node is either red or black.

c. The root is black.

d. Every path from the root to a leaf contains the same number of black nodes.

e. Red nodes can only have black children.

f. Every leaf is NULL and black.

2. Are this properties correct? Are there any other properties that I need to know?

3. I'm assuming it is more common to use an array rather than pointers. Is this true?

4. What make a node red or black? And what advantage do I get out of having red and black nodes.

5. Anyone know of any good sites with good examples?

Mitthrawnuruodo

Apr 22, 2006, 12:30 PM

Introductions to Algorithms (http://mitpress.mit.edu/algorithms/) has a whole chapter (13) dedicated to Red-Black threes, but I'm afraid I cannot type in those 30 pages here... :o

Maybe they have the book at your local public library. They don't provide code, but even better, the algorithm in a very easy-to-understand notation.

I don't think we ever implemented the algorithm in either of my C++ or Java courses...

Edit: On the other hand, Google is a friend, as ever: http://www.cs.fiu.edu/~weiss/dsaa_c++/code/ ;)

cait-sith

Apr 22, 2006, 04:49 PM

Is this for a university course?

grapes911

Apr 22, 2006, 05:08 PM

Introductions to Algorithms (http://mitpress.mit.edu/algorithms/) has a whole chapter (13) dedicated to Red-Black threes, but I'm afraid I cannot type in those 30 pages here... :o

Maybe they have the book at your local public library. They don't provide code, but even better, the algorithm in a very easy-to-understand notation.

I don't think we ever implemented the algorithm in either of my C++ or Java courses...

Edit: On the other hand, Google is a friend, as ever: http://www.cs.fiu.edu/~weiss/dsaa_c++/code/ ;)

I have some books (none with very good examples). I'm having trouble just understanding the concept, let alone the code. Thank you though.

Is this for a university course?

Yep.

Mitthrawnuruodo

Apr 22, 2006, 07:37 PM

I have some books (none with very good examples). I'm having trouble just understanding the concept, let alone the code. Thank you though.Well, that's the brilliant thing about the above mentioned Algorithm book. It explains how the trees work, in detail, with illustrations... :)

Voidness

Apr 23, 2006, 03:30 AM

When I had an assignment to implement an AVL Tree last semester, I found this Java applet to be helpful in understanding the concept. It also has a mode for Red-Black Trees, which I didn't study:

http://webpages.ull.es/users/jriera/Docencia/AVL/AVL%20tree%20applet.htm

Also, Wikipedia has a detailed entry for Red-Black Trees:

http://en.wikipedia.org/wiki/Red-black_tree

Hope this helps.

Just curious, which language are you using? I used C++ with my AVL tree.

superbovine

Apr 23, 2006, 07:16 PM

http://www.ddj.com/184410531

one of the containers in the STL is a red-black tree (if remember right), anyway if you dig around you can probably find a good example from the stl.