6

I am trying to construct a red black tree out of only black nodes. I know it is possible getting it after some deletions but I am trying to construct one only via insertion orders. Is it possible? I couldn't find a way to do so , so far even after trying to use the simulator.

*** exept of a root node being added to an empty tree.

Raphael
  • 72,336
  • 29
  • 179
  • 389
user118972
  • 179
  • 1
  • 3
  • 1
    What have you tried? Have you tried working through some very small examples (say, exhaustively enumerating all possible insertion orders for trees of size 1, 2, and 3)? Have you tried to find a proof that it is impossible? Have you tried looking for suitable invariants that would imply it is impossible? – D.W. Aug 20 '15 at 17:01
  • Hello and thank you for your comment. I have tried to do so. I couldn't find any invariants though. Can I assume it is impossible since we always insert a red node and thus an only black nodes tree is impossible to construst without a deletion after an insertion? The balancing menipulations taken on the nodes ( rotations and recoloring) don't allow that "only black" situtaion ( not reffering a single node tree). – user118972 Aug 20 '15 at 17:09
  • I am quite convinced after trying a lot of diferent insertions that it isn't possible but I can't find the invariant. – user118972 Aug 20 '15 at 17:19
  • Hints: 1) Can you quickly name a tree shape that allows you to color everything black? 2) Given any (balanced) target shape, what is a simple insertion order to obtain exactly that shape? – Raphael Aug 20 '15 at 17:57
  • RBT have properties you must obey. Which of those could be violated? When? – Evil Aug 20 '15 at 18:12
  • I apologize but I still don't understand. We've only learned about RB trees and not B trees or 2-3 or AVL ( if that is what you meant by asking about a specific tree type). Exept for an only root tree I dont understand the meaning of the question : " a tree shape that allows you.." , A RB tree that is all black must be perfectly balanced , bit I guess that isn't what you asked. – user118972 Aug 20 '15 at 18:22
  • I think that it is exactly what Raphael asked you about. Moreover if you have perfectly balanced tree, there is a way to insert nodes in some order, can you think how? E.G. You have numbers from 0 to 6, how can you add them to avoid rotations? – Evil Aug 20 '15 at 19:10
  • So I tried this: In order to avoid rotations I have inserted by order ( 3, 1 , 5 , 0 , 2, 4, 6) I did succeed avoiding rotations but the lower level of the tree ( 0,2,4,6) nodes are all red. How does avoiding rotations help? By recoloring I always leave the lower node red and propagate the "problem" up. – user118972 Aug 20 '15 at 19:46
  • You have to know what is happening step by step. Now changing insertion order or introducing deletions would help, but first look at the properties. You have to maintain equal number of black nodes in all paths. Do you know how to push red node out and make it black? – Evil Aug 20 '15 at 21:17
  • Thank you for your comments. I am trying to build an only black -nodes tree without using a deletion I assume it isn't possible. I understand that via using a deletion I will be able to achieve an only black nodes -tree. Is there any sort of re- ordering the insertion that allows what I'm looking for? without any deletions. – user118972 Aug 20 '15 at 21:33

2 Answers2

3

Properties of Red Black Tree:

0) Every node is black or red. Ok, no problem.

1) Root is black. Ok, no problem.

2) All leaves (empty nodes) are black. Ok, no problem.

3) Every red node must have two black childs. Ok, no problem.

4) Every path from any node to leaf has equal number of black nodes. It seems problematic.

So now comes problems: When you insert element it's color is red. (In fact this not comes from properties but observation that adding red node does not violate $4$th rule, so it is easier to implement).

So for start you add one element, let me say 10. It is black root. Ok.

Now You add 13. It must be red, otherwise it violates rule 4.

Red Black Tree elements 10; 13

Now you add 19. Depending on implementation you can leave all black, or recolor.

Red Black Tree elements 10; 13; 19

Now adding 12 gives recoloring and comes red.

Red Black Tree elements 10; 13; 19; 12

And after several numbers

Red Black Tree elements 10; 13; 19; 12; 41; 43; 26; 10; 82

And now it starts nightmare, as you should propagate changes from one side of tree to another convincing somehow that you will change everything to black after insertion.

Basically I asked you about rules to obey, as it comes to mind that with $4$th can be fulfilled only in perfectly balanced trees.

So to answer your questions:

No you cannot with common implementations of RBT, but extending balance operation to recolor when it is possible - it does not violate rules, so it is possible. Also with extension that whenever there is $n = 2^k - 1$ nodes, restructure it to perfectly balanced tree and recolor everything to black, it would be also possible.

I did firstly answer "no" as purpose of balancing operation is to guarantee bound on worst case search / insert, and such extensions comes at price of time complexity. It does not violate rules, but is not implicitly implemented in standard RBT.

Given only root, you have it. Given three nodes inserted in order with implementation changing all to black (rather rare case and counterproductive), you can. But this is last one that is without deletions possible.

When you are about to insert element and want all tree to be black that would mean there is violated rule $4$ (so not possible) or you have to rebalance tree, which would recolor nodes, so it is not pure black anymore.

With deletions on the other hand ;)

Evil
  • 9,455
  • 11
  • 31
  • 52
  • Well, for $n=2^k$ you can. :) – Raphael Aug 21 '15 at 16:34
  • Well, not by only insertions ;) And recoloring is possible, but it needs traversing whole tree. Otherwise, if we implement such feature that perfectly balanced tree we switch all colors to black, then yes. But not for $n = 2^k$ as it must have one red node, but $n = 2^k -1$ – Evil Aug 21 '15 at 16:37
  • Right, wrong $n$. :D I understand the question to ask for the final tree being all black. Inserting the values in breadth-first order definitely gets the right shape; whether all nodes are black probably depends on the implementation (they all can be). – Raphael Aug 21 '15 at 16:40
  • BFS is just level ordering, ok I am mixing two things one is impementation dependence - and one is recoloring. $n = 2^k - 1$ nodes and then it can be all black, but none existing implementation has it. – Evil Aug 21 '15 at 16:57
-2

insert 1,2,3,4 then delete 4,thank you.

  • 1
    Please [edit] your answer to elaborate on it. Why does this meet the requirements of the question? What tree results? Can you provide your reasoning and justification for your answer? We are not looking for a one-line response. – D.W. Jul 07 '21 at 22:11