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

Started by
4 comments, last by iMalc 12 years, 9 months ago
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
Advertisement
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.
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

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
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?
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

This topic is closed to new replies.

Advertisement