8

Suppose I have a binary search tree which, initially, satisfies all of the red-black conditions and contains one node for every integer s in some set S. Next, I want to a new node; say a (which is not in S).

Is the result of this addition, after rebalancing, unique?

Put another way: is there only one way to rebalance a red-black tree after inserting a node?

I believe that they are not unique, although I offer no proof (and little confidence). I'm just wondering if someone more knowledgeable than myself might be so kind as to edify me?

Ryan
  • 925
  • 2
  • 6
  • 25

2 Answers2

8

They are not unique.

A simple proof would be to make a trivial algorithmic change, like check that we can change the root's colour, and provide a case where that is still valid, for example:

    1-B
   /  \
 0-R  2-R

add(3):

    1-B
   /  \
 0-B  2-B
        \
        3-R

But the new algorithm could quite as easily yield

    1-R
   /  \
 0-B  2-B
        \
        3-R

The root is a different colour but of course the trees are both still valid RB trees.

This may seem a little trivial, but you can extend the idea (if you want a proof that is less trivial) to check for more than just the root. You could check O(1) levels deep to make a non-trivial but valid change, which would generate two different algorithms with the same running times.

For example, check that the first 10 rows are full and black, and change the odd ones to red, would yield an additional constant work (i.e. O(1)), and a new algorithm.

I should note that this is simply a proof of non-uniqueness, not a bound on the amount of non-uniqueness. I.e. something trivial like this is enough to prove the point, but it leaves the door open for RB algorithms that differ in more fundamental ways.

davin
  • 44,863
  • 9
  • 78
  • 78
  • I'm sorry--I've been reading this and trying to wrap my head around it, but am having trouble. Is the crux of your example that we can change the algorithm and show that both algorithms generate valid red-black trees? What if we add the additional restriction that the root must always be black (although it really doesn't matter in terms of black height) – Ryan Jul 19 '11 at 17:46
  • 1
    @Ryan, that's why I explained that the idea can be extended beyond just the root. And yes, the idea is that we can generate two algorithms that in a certain situation will produce two different valid RB trees. – davin Jul 19 '11 at 17:52
  • oh, I see--when you say O(1) levels deep, do you mean relative to the root, or to the just-added node? (Perhaps it doesn't matter). Correct me if wrong, but what you're saying is: I might have two algorithms, where the only difference is that one makes a non-trivial but valid change which still maintains the red-black integrity. Maybe like changing both nodes at level 2 to black (since this won't alter the black height of any leaf). Therefore, I prove that the algorithms are not unique. – Ryan Jul 19 '11 at 17:55
  • @Ryan, `O(1)` relative to the root. You are correct, that is the idea, although like I noted at the end, that doesn't mean that that's the extent of variance in RB algorithms. There can still be more general differences. – davin Jul 19 '11 at 17:57
  • So, forgive my hasty generalization here, but, could one say that the invariant for insertion/deletion on any red-black algorithm is simply that the 4 (or 5) red-black properties are satisfied, regardless of how the tree is manipulated to do so? Maybe I've phrased it wrong (just trying to build more intuition into this) – Ryan Jul 19 '11 at 18:00
  • @Ryan, indeed, that would be a valid way to categorise all RB algorithms, which is flexible in that it allows optimisations and variance. – davin Jul 20 '11 at 07:26
2

No there are mutliple alorithms.

Let start with this 2 propositions:

  • The number of red-black trees with 4 internal nodes is 3
  • The number of red-black trees with 5 internal nodes is 8

Now, imagine there is a unique alorithm to add a node to a 4 internal node red-black Tree. Then there should be only 3 red-black Trees with 5 internal nodes, since the algorithm leads to one single result.

That's absurd as the number of red-black trees with 5 internal nodes is 8.

(cf PIGEONHOLE PRINCIPLE )

Therefore there are multiple "red-black" algorithm

Hope I understood what you meant.

Ricky Bobby
  • 7,490
  • 7
  • 46
  • 63
  • I like this approach, but I need to think about it a bit more (abstracting too many of the details away is confusing for me at this point). – Ryan Jul 19 '11 at 17:48
  • I see what you're saying now. However, if we add a random node to the 4-node RB tree, the result can only be of the form of the 8 RB trees with 5 internal nodes. The **question**, though, is: having fixed a, the node we'll add, are there multiple 5-internal node solutions for a **given** a? – Ryan Jul 19 '11 at 17:51
  • For a 4 node Tree they are at most 4 positions were you can add a node to create a 5 node tree. If there exist 8 5 nodes trees, then there is at list on way to add a node with more than 2 possible RB-output trees (pigeonHole principle) and therefore There exist one a that answers your question. But you cannot prove it for all 'a'. Actually davin proof only prove it in one situation, not for all added nodes (cf, second comment in davin proofs :"in certain situations" – Ricky Bobby Jul 20 '11 at 07:14
  • Aren't there 6 possible positions to add a node to a 4-internal node tree? – Ryan Jul 20 '11 at 16:39