Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a resultant red-black tree after insertion unique?

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?

like image 229
Ryan Avatar asked Jul 19 '11 16:07

Ryan


People also ask

Does red-black tree allow duplicates?

R-B trees aren't really designed for data structures which support duplicates, but rather sets.

When inserting a new entry into a red-black tree what condition might happen?

Solution: When the first element is inserted it is inserted as a root node and as root node has black colour so it acquires the colour black. The new element is always inserted with a red colour and as 21 > 3 so it becomes the part of the right subtree of the root node.

What is the color of newly inserted node in red-black tree?

In a red black tree, every new node must be inserted with the color red. The insertion operation in red black tree is similar to insertion operation in binary search tree. But it is inserted with a color property. After every insertion operation, we need to check all the properties of red black tree.

Would inserting a new node to a red-black tree and then immediately deleting it change the tree?

Deleting the same node immediately after inserting will not always result in the original tree.


2 Answers

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.

like image 108
davin Avatar answered Oct 25 '22 16:10

davin


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.

like image 38
Ricky Bobby Avatar answered Oct 25 '22 15:10

Ricky Bobby