algorithmtreered-black-tree-insertion

What if uncle is black and parent is black when inserting a node to red black tree?


I know there are two cases which uncle is black in red black trees when inserting a new node. But in all the cases parent is red. if parent is black there is no violation. what shall I do in such situation in a red black tree?


Solution

  • If you end up adding a node into a red/black tree and its parent is black, you can just make the node red and call it a day. There’s no need for any fixup. If you look at the rules for red/black trees, this doesn’t cause any new violations because all root-null paths still go through the same number of black nodes.

    A different perspective: if you think of red/black trees as an isometry or 2-3-4 trees, then this rule corresponds to taking a leaf with one key in it and adding in another key, which doesn’t require any further fixup steps.