Verified word-RAM routine: binary-search-tree insertion with node allocation in 13·height + 17 steps
ProvedCentralStrQ.bstInsert_specbinary-search-treesdata-structureshoare-logicverified-programmingword-ram
Setting. The registers sit in the consecutive cells :
| cell | role |
|---|---|
| slot | |
| cur | |
| key | |
| result | |
| tmp | |
| allocation pointer | |
| root cell |
Hypotheses (InsertHyp):
- and ;
- the memory stores a binary search tree at within ;
- and ;
- .
Conclusion. Started on with space usage , the program bstInsertStmt reaches its end within
steps. At the end the root pointer stores , and one of two cases holds:
- present: was already a key, , the allocation pointer is still , and the tree lies within ;
- inserted: was not a key, , a new node was allocated at , the allocation pointer is , and the tree lies within .
Every cell below , every cell in , the key cell, and every cell from on is unchanged. The space usage stays at most .
Preamble
import Mathlib import Definitions.Def_CentralStrQ_Machine import Definitions.Def_CentralStrQ_Hoare import Definitions.Def_CentralStrQ_Structured import Definitions.Def_CentralStrQ_Trees set_option autoImplicit false
Formal statement
namespace CentralStrQ
open WordRAM
/-- **Insertion into a binary search tree in `O(height)` steps.** With the registers in the
cells `g, …, g + 6` (root pointer in `g + 6`, allocation pointer in `g + 5`), the program inserts
the key `[g + 2]`: if it was present, `[g + 3] = 1` and the tree is unchanged; otherwise a new
node is allocated at `hi = [g + 5]`, `[g + 3] = 2`, and the allocation pointer moves to
`hi + 3`. In both cases the tree at the root is `t.insert k`, every cell outside the registers
and the node area is unchanged, and the space usage stays at most `Rb`. -/
theorem bstInsert_spec {w : Width} {g lo hi k Rb r₀ : ℕ} {t : KTree} {m₀ : Memory w}
(h : InsertHyp g lo hi k Rb t m₀) (hr₀ : r₀ ≤ Rb) :
STriple (bstInsertStmt (g + 6) g (g + 1) (g + 2) (g + 3) (g + 4) (g + 5))
(fun m r => m = m₀ ∧ r = r₀)
(fun m r => (((m (g + 3)).toNat = 1 ∧ k ∈ t.keys ∧ (m (g + 5)).toNat = hi ∧
TreeAt m lo hi (m (g + 6)).toNat (t.insert k)) ∨
((m (g + 3)).toNat = 2 ∧ k ∉ t.keys ∧ (m (g + 5)).toNat = hi + 3 ∧
TreeAt m lo (hi + 3) (m (g + 6)).toNat (t.insert k))) ∧
(∀ x, (x < g ∨ (g + 7 ≤ x ∧ x < lo) ∨ hi + 3 ≤ x ∨ x = g + 2) → m x = m₀ x) ∧
r ≤ Rb)
(t.height * 13 + 17) := by sorry
end CentralStrQ
Source
Binary search trees as in T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press 2009, Chapter 12 (search and insertion), stored in word-RAM memory and verified with the published structured Hoare logic Def_CentralStrQ_Structured (Hoare 1969; Floyd 1967). Infrastructure for the published deterministic word RAM Def_CentralStrQ_Machine (Kempa, Kociumaka, A Complete Characterization of Central String Queries in Compact Space, STOC 2027 submission, Section 3.2, p. 13; T. Hagerup, Sorting and searching on the word RAM, STACS 1998, https://doi.org/10.1007/BFb0028575). Hoare-style rules after C. A. R. Hoare, An axiomatic basis for computer programming, Commun. ACM 12(10), 1969, https://doi.org/10.1145/363235.363259.