Search code examples
algorithmred-black-tree

What does it mean for a red black tree to have same number of black nodes on all paths?


I don't understand what this condition means: "For each node, all paths from the node to descendant leaves contain the same number of black nodes"


Solution

  • Let's take an example tree:

    enter image description here

    The statement says "For every node...", so let's choose one such node as example, the root, node 13.

    It speaks of "descendent leaves". These leaves are the nodes with values 6, 11, 15, 22 and 27. They are descendents of our chosen node 13 and they are leaves, so "descendent leaves".

    "all paths from the node to descendant leaves" are thus the following paths:

    • 13→8→1→6
    • 13→8→11
    • 13→17→15
    • 13→17→25→22
    • 13→17→25→27

    Now count the black nodes on each of these paths:

    • 13→8→1→6 has 2 black nodes (13 and 1)
    • 13→8→11 has 2 black nodes (13 and 11)
    • 13→17→15 has 2 black nodes (13 and 15)
    • 13→17→25→22 has 2 black nodes (13 and 25)
    • 13→17→25→27 has 2 black nodes (13 and 25)

    So indeed, we see that "all paths from the node to descendant leaves contain the same number of black nodes": exactly 2 for our example tree and selected node.

    You can repeat this exercise for another node, for instance node 8. Then the "descendent leaves" are only nodes 6 and 11.

    Now the results are:

    • 8→1→6 has 1 black node (1)
    • 8→11 has 1 black node (11)

    And again, the statement is true. Actually, when the statement is true for the root, it is true for all nodes.