Andersson Trees
- Every tree keeps track of the left-level; that is, the number of
hops to the sentinel when considering only the left children.
- All sentinel nodes have a left-level of zero.
- All leaf nodes (nodes with no children) have a left-level of one.
- The left rule: The left-level of a tree must always be the left-level of its left child plus one.
- The right rule: The left-level of a tree may be the same as the left-level of its right child. However, it must be greater than the left-level of its right grandchild.
- After an insertion into the tree:
- Check to see if the left rule is violated. If so, perform a right rotation.
- Check to see if the right rule is violated. If so, perform a left rotation.
- When performing rotations, update the levels of the rotated nodes whenever a node acquires a new left node. Its new level will be the level of the new left node, plus one.
- Make sure that the invariant restoration is performed after every
insertion, including the recursive calls.
Removing a node
Removing a node from the tree requires more elaborate invariant restoration:
- If the difference between the level of a parent and either of its
children is greater than one, reduce the level of the parent by one.
- If this subtraction makes the level of the right child greater than
the level of its parent, set the level of the right child to the level of
its parent.
- Enforce the left rule on the parent, its right child, and its right grandchild, in that order.
- Then enforce the right rule on the parent and its right child, in that order.