Documentation

CslibFrontier.DataStructure.AVLTree.Erase

Rebalancing deletion for AVL binary search trees #

Deletion follows the search path to the target key. A matching node with an empty right child is replaced by its left child; otherwise its leftmost right-subtree key is detached via rebalancing splitMin and promoted, with balance restoring the ±1 sibling-height invariant at every ancestor along the root-to-detached-node path.

Unlike Cormen et al., Problem 13-3, which covers the AVL invariant, Fibonacci height bound, balance, and insert, this module extends the same balance combinator to successor-splice deletion so that AVLTree supports the full Cslib.DataStructure.OrderedSet.spec interface including Op.delete and extractMin.

Main definitions #

Main results #

References #

def Cslib.DataStructure.AVLTree.splitMin {α : Type u_1} (a : α) :
AVLTree α → AVLTree α → α × AVLTree α

Detach the leftmost key of .node a _ l r, rebalancing ancestors on the way back up.

def Cslib.DataStructure.AVLTree.glue {α : Type u_1} (l : AVLTree α) :
AVLTree α → AVLTree α

Join the two children of a deleted node by promoting the minimum of r (when nonempty) and rebalancing.

def Cslib.DataStructure.AVLTree.erase {α : Type u_1} [LinearOrder α] (x : α) :
AVLTree α → AVLTree α

Delete x from t, rebalancing ancestors along the search path.

@[simp]
theorem Cslib.DataStructure.AVLTree.elements_erase {α : Type u_1} [LinearOrder α] {t : AVLTree α} (ht : t.Ordered) (x : α) :

Deletion removes x from the represented Finset α (including when x was absent).

theorem Cslib.DataStructure.AVLTree.valid_erase {α : Type u_1} [LinearOrder α] {t : AVLTree α} (ht : t.Valid) (x : α) :
(erase x t).Valid

erase preserves full AVL validity (Ordered, HeightConsistent, and Balanced).

Deletion shrinks cached height by at most 1 and never increases height.

theorem Cslib.DataStructure.AVLTree.erase_eq_of_notMem {α : Type u_1} [LinearOrder α] {t : AVLTree α} (ht : t.Valid) {x : α} (hx : x ∉ t.elements) :
erase x t = t

Deleting an absent key from a valid AVL tree leaves the entire tree unchanged.

def Cslib.DataStructure.AVLTree.eraseVisits {α : Type u_1} [LinearOrder α] (x : α) (t : AVLTree α) :

Internal-node visits of erase: the downward search path plus leftmost splitMin descent in the matched node's right subtree. Rebalancing reads of O(1) off-path sibling/nephew cached heights at visited ancestors are not counted separately.

Deletion pays for the downward search path and possibly a splitMin descent on top.

Deletion visits at most t.cachedHeight internal path nodes.