I have a problem with the rb-trees. according to wikipedia, rb-tree needs to follow the following:
As we know, an rb-tree needs to be balanced and has the height of O(log(n)). But, if we insert an increasing series of numbers (1,2,3,4,5...) and theoretically we will get a tree that will look like a list and will have the height of O(n) with all its nodes black, which doesn't contradict the rb-tree properties mentioned above. So, where am I wrong??
thanks.
Your example contradicts property number 5, so it's not a valid Red-Black tree.
The tree we have is:
b(1, nil, b(2, nil, b(3, nil, b(4, nil, b(5, nil, nil)))))
so to get to the last two leaves (the children of node 5
), we have to visit five black nodes (represented by each b
), to get to the leaf under node 4
we have to visit four black nodes, etc. That means that there are simple paths from the root to some of this descendants containing a different number of black nodes, which invalidates property 5.