View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Red-Black Trees-> How to determine node colors at all times?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1ClaudeFleming  Members

Posted 28 June 2011 - 12:42 PM

Hi,

Sorry if I'm a little dense, but I'm learning data structures so that I can become more acquainted with the true beauty of computer science. I have a question about red-black trees. How do you determine which nodes are red, and which nodes are black? I watched a demo of a red-black tree being created, and on the right branches of the tree, all the nodes were black (at least I think they were), and that threw me off. Also, what's up with the red nodes? Are they just created to make the black nodes stay in their proper place?

Thanks,
Lee

### #2apatriarca  Members

Posted 28 June 2011 - 01:45 PM

I don't know what book are you following, but Sedgewick have recently given some talks about left-leaning red-black trees (here for example) which I think may be interesting if you are interested in learning about these trees. The left leaning part of his variant is an additional constraints in the colors of the tree which simplifies the code.

### #3ClaudeFleming  Members

Posted 28 June 2011 - 10:23 PM

I know, I have read it. But I already have a good college textbook on the subject. The author says they are nothing more than either 2-3 trees or 2-3-4 trees arranged differently. I spent \$108.00 on two algorithm books simply because I enjoy the beauty of data organization so much, lol. I have the soul of an artist and the coordination of a troglodyte and the common sense of a bulldog. lol

### #4iMalc  Members

Posted 29 June 2011 - 12:35 AM

they are nothing more than either 2-3 trees or 2-3-4 trees arranged differently.

Exactly what I was going to say. QFE

If you first understand 2-3-4 trees, then you only need to understand that red-black is just a more convenient way of representing them. Learn how 2-3-4 trees are built first. The key is that they are built bottom up, not top down.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

### #5ClaudeFleming  Members

Posted 04 July 2011 - 09:36 PM

My new books have arrived. And in one of them, red-black trees are called left-leaning 2-3 trees. Why does the red link have to be left-leaning and why is it that the black-height remains constant so easily?

### #6iMalc  Members

Posted 05 July 2011 - 12:46 AM

You're actually describing an AA-Tree which is a specialised variant of the red-black tree. The general red-black tree can have red links on either side of a black node.

The black height remains constant because the tree is built bottom up, not top down. I.e. when you do an insert, you don't actually traverse directly to a leaf node and then do an insert. You actually insert into the root node, where if it were a 2-3 tree you'd then have a node with one more item in it, and if there were three items in the node then it is split.

If inserting the value 3 into a tree with one node containing items 1 and 2 i.e.
[1,2]
then you can think of it as two steps, first inserting the item so that the tree looks like this:
[1,2,3]
and then performing a split so that is looks like this:
[2]
/ \
[1] [3]

From there you just need to represent those 2-3 nodes as a binary tree where a node with more than on item is represented as a red link to the rest of the 2-3 node.

As was said, if you understand 2-3 trees, you will understand red-black trees.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.