A binary search tree in which
The relation n > 2h/2 - 1 implies the bound h < 2 log 2(n + 1).
Discrepancies in which the sibling is red, are fixed by changes in color. Discrepancies in which the siblings are black, are fixed through AVL-like rotations.
Changes in color may propagate the problem up toward the root. On the other hand, at most one rotation is sufficient for fixing a discrepancy.
| LLr |
|
![]() if ‘A’ is the root, then it should be repainted to black |
| LRr |
|
if ‘A’ is the root, then it should be repainted to black
|
| LLb |
|
|
| LRb |
|
|
Discrepancies of type RRr, RLr, RRb, and RLb are handled in a similar manner.
| insert 1 |
|
![]() | |
| insert 2 |
![]() | ||
| insert 3 |
![]() RRb discrepancy |
|
![]() |
| insert 4 |
![]() RRr discrepancy |
|
|
| insert 5 |
![]() RRb discrepancy |
|
|
In the case of discrepancies which result from the addition of nodes, the correction mechanism may propagate the color problem (i.e., parent and child painted red) up toward the root, and stopped on the way by a single rotation. Here, in the case of discrepancies which result from the deletion of nodes, the discrepancy of a missing black node may propagate toward the root, and stopped on the way by an application of an appropriate rotation.
| Rb0 |
|
change of color, sending the deficiency up to the root of the subtree |
| Rb1 |
|
|
![]() |
| |
| Rb2 |
![]() |
|
| Rr0 |
![]() |
might result in LLb discrepancy of parent and child having both the red color |
| Rr1 |
|
![]() |
![]() |
| |
| Rr2 |
![]() |
|
Similar transformations apply to Lb0, Lb1, Lb2, Lr0, Lr1, and Lr2.
In each stage, the transformation should be conducted at a discrepancy that is farthest from the root. A discrepancy is assumed to be caused by a deletion, when the numbers of black nodes in the different paths are not equal. Otherwise, it is assumed to be caused by an insertion.
| LL |
![]() |
![]() |
| LR |
|
![]() |
| L |
|
|
A binary search tree is said to be splayed at a given node, if the node is moved to the root with the above, and symmetric, operations. Splay trees (applet), or or self-adjusting search trees, are trees which are splayed whenever their nodes are accessed. On the average, they are accessed and modified in O(log n) time.
Construct a binary search tree by introducing the following keys in the given order: 7, 6, 5, 4, 3, 2, 1. Then splay the tree at node 1, and show all the intermediate trees being created in the process. (In the LL and LR cases, 1 is represented by the node C. In the L case it is represented by the node B.)