The solution I posted on your previous question does work.
To recap, I suggested to maintain in each node:
- The largest and smallest element in the subtree below this node and
- the closest pair in that subtree.
On insert
and delete
, update the information in every node on the path from the inserted/removed leaf to the root and every node affected by a rotation.
Intuitively, this works since the information on the closest pair for each unaffected subtree is stored in the root of that subtree and our update will look at the roots of all maximal unaffected subtrees (and all nodes affected by the upadte). Thus, implicitly, our update will look at the complete tree, in order to find the new closest pair.
In order to show more formally that the update is correct for the root (the only node, where we actually need the annotation), we can show that the following stronger invariant holds:
After each operation the annotations in all the nodes are correct.
This is obvious for operations that don't change the tree. For insert
and delete
, we distinguish two classes of nodes:
- If the subtree below the node is unchanged, the annotations that were correct before still are.
- If the subtree below the node is changed, then the node must be on the path from the inserted/removed leaf to the root or affected by a rotation. In this case we explicitly update the information, so it will be correct.
That the update of each node will produce a correct annotation, can be shown by a simple induction, using the following arguments:
- (Anchor): The procedure is correct for leaves.
- (Step): If the information in both children is correct then the described procedure will produce the correct annotation for the parent node.