I was asked this question in an exam, but I'm not really convinced with the answer from the teacher, and I would like to ask what do you think about it.
A rotation on a red-black tree...
- preserves the black height of all nodes.
- preserves in-order ordering.
Which above the above statements are true:
A. (1) alone
B. (2) alone
C. Both (1) and (2)
D. Neither (1) nor (2).
The teacher claims a rotation doesn't preserve the black height, and the answer is B: it preserves in-order ordering only. However, I strongly believe that it preserves the black height of all nodes and the answer is C, not B.
Am I right or is my teacher right?
I agree with your Professor. I don't agree with you on the black height is preserved for all nodes, because the rotations (as well as node recolourations are part of group of operations designed to restore red-black properties)
Typically
Red-Black Tree Insertion violates the property of not having 2 red nodes in succession. Red-Black Tree Deletion violates the property of all black tree heights from the root are the same.
So any fixups are done on nodes in the direction of the root of the tree and there may be O((log2(h /2)) recolourations and up to 2 rotations for Inserts O(log2(h)) recolourations and up to 3 rotations for Deletes
It is only final part of the fixup that restores the full set of invariants for a Red-Black tree and any on-the-way partial fixups may still leave the tree needing further fixups until the offending violations are resolved.