Started by Jun 28 2011 12:42 PM

,
5 replies to this topic

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

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

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.

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

Posted 29 June 2011 - 12:35 AM

Exactly what I was going to say. QFEthey are nothing more than either 2-3 trees or 2-3-4 trees arranged differently.

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 website dedicated to sorting algorithms

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?

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.

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.

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

My website dedicated to sorting algorithms