treebinary-treebinary-search-treered-black-treered-black-tree-insertion

How is this RBT considered balanced?


I've been playing with a RBT visualizer and don't understand how the following is considered height-balanced. The wikipedia article claims that if the RBT properties are satisfied, then the height of the farthest leaf is no greater than twice the height of height of the closest leaf. From my understanding, the below would violate this property, even if the RBT properties are satisfied (the depth of 1 is 1 and the depth of 6 is 3). Where is my logic flawed here? enter image description here


Solution

  • From the Wikipedia article:

    The leaf nodes of red–black trees ( NIL  in figure 1) do not contain keys or data. These "leaves" need not be explicit individuals in computer memory: a NULL pointer can —as in all binary tree data structures— encode the fact that there is no child node at this position in the (parent) node. Nevertheless, by their position in the tree, these objects are in relation to other nodes…

    The diagram you've included in your question has omitted these NIL nodes, but they are relevant for the determination of balanced. Using the rule you describe ("the path from the root to the farthest leaf is no more than twice as long as the path from the root to the nearest leaf"), the path from the root to the nearest leaf has a distance of two (not one), and the path from the root to the farthest leaf has a distance of four (not three).

    Since four is not more than twice two, this tree complies with the "balanced" rule.