Positional deletion from red-black position trees: the functional algorithm
DefinitionCentralStrQ_PosDeleteThis bundle defines deletion of a segment from the position trees of Def_CentralStrQ_PosTrees. A position tree is a Batteries red-black tree RBTree.RBNode (ℕ × ℕ) of segments in order, and wsum is its total length.
-
posDel t iis Batteries' deletion stepRBNode.del, with the search by position instead of by key. At a node with left length and segment length :- if , it goes left;
- if , it removes the node, replacing it by Batteries'
appendof its two subtrees; - otherwise it goes right with position .
A subtree that lost a black node is repaired by Batteries'
balLeft/balRight, and otherwise the node is rebuilt red, exactly as inRBNode.del. -
posDelete t iadditionally blackens the root, as Batteries'RBNode.erase. -
segDelete L iis the same deletion in a list of segments. It removes the first segment whose span contains the -based position , and removes nothing for or past the end.
This is the functional specification for a verified word-RAM deletion program, the counterpart of the positional insertion of Def_CentralStrQ_PosInsert.
import Mathlib
import Definitions.Def_CentralStrQ_PosTrees
set_option autoImplicit false
namespace CentralStrQ
/-!
# Positional deletion from position trees: the functional algorithm
Deletion of a segment from the position trees of `Def_CentralStrQ_PosTrees` (Batteries'
red-black trees `RBTree.RBNode (ℕ × ℕ)` of segments `(ℓ, v)` in order).
* `posDel t i` / `posDelete t i`: Batteries' deletion (`RBNode.del` / `RBNode.erase`, with
`balLeft`, `balRight` and `append`) with the search by position: at a node with left length
`L` and length `ℓ` the descent goes left if `i ≤ L`, removes the node if `L < i ≤ L + ℓ`, and
goes right with `i − L − ℓ` otherwise.
* `segDelete L i`: the same deletion in a list of segments (the first segment whose span
contains the `1`-based position `i`; nothing for `i = 0` or past the end).
-/
/-- The deletion step at a position (as Batteries' `RBNode.del`, searching by position): remove
the segment whose span contains the position `i` (`1`-based). At a node with left length
`L = wsum a` and segment length `ℓ = y.1`: go left if `i ≤ L`, remove the node (Batteries'
`append` of its subtrees) if `i ≤ L + ℓ`, otherwise go right with the position `i − L − ℓ`; a
subtree that loses a black node is repaired by `balLeft` / `balRight`. -/
def posDel : RBTree.RBNode (ℕ × ℕ) → ℕ → RBTree.RBNode (ℕ × ℕ)
| .nil, _ => .nil
| .node _ a y b, i =>
if i ≤ wsum a then
match a.isBlack with
| .black => RBTree.RBNode.balLeft (posDel a i) y b
| .red => .node .red (posDel a i) y b
else if i ≤ wsum a + y.1 then RBTree.RBNode.append a b
else
match b.isBlack with
| .black => RBTree.RBNode.balRight a y (posDel b (i - wsum a - y.1))
| .red => .node .red a y (posDel b (i - wsum a - y.1))
/-- **Positional deletion** from a red-black position tree (as Batteries' `RBNode.erase`: the
root is blackened after the deletion step). -/
def posDelete (t : RBTree.RBNode (ℕ × ℕ)) (i : ℕ) : RBTree.RBNode (ℕ × ℕ) :=
(posDel t i).setBlack
/-- Remove from a list of segments the first segment whose span contains the position `i`
(`1`-based; nothing is removed for `i = 0` or past the end). -/
def segDelete : List (ℕ × ℕ) → ℕ → List (ℕ × ℕ)
| [], _ => []
| y :: ys, i => if i = 0 then y :: ys else if i ≤ y.1 then ys else y :: segDelete ys (i - y.1)
end CentralStrQ