Pointer-machine AVL insertion #
The insertion program searches with a parent cursor, initializes and attaches one new leaf on a miss, and balances ancestors by following parent links. Comparisons and height operations are scalar primitives. Ghost contexts describe the proof; the program uses only fixed registers.
Scalar sources for a new AVL leaf: query register zero, initialized height register one, and explicit client sources for all remaining fields.
Initialize a height-one leaf, choose its child link by comparison with the parent key, and allocate and attach it. A null parent installs the whole-tree root.
Balance the current ancestor, reconnect its new root, and move to its parent. The enclosing loop decides whether another ancestor exists.
Follow parent links, balancing and reconnecting every ancestor until the cursor is null.
On an unsuccessful search, create a leaf and repair its ancestors. A successful search halts without allocating or changing the tree.
Insert the query in data register zero into the AVL tree at cursor zero. Root register one tracks the whole tree. All extra leaf payload sources are explicit.
Linked-tree, register and scalar-payload outcome of set insertion. Existing records retain all scalar fields except the height cache; a new record receives its explicit client payload. This contract does not assert a full frame for records outside the tree.
The output is a complete linked tree.
Its keys and height caches match the existing functional AVL insertion exactly.
- distinct : SearchTree.PointerMachine.AddressesDistinct after
Distinct tree positions still occupy distinct records.
Register one tracks the final whole-tree root.
At most one record is allocated, and no record is removed.
- footprint : SearchTree.PointerMachine.addresses before ⊆ SearchTree.PointerMachine.addresses after ∧ SearchTree.PointerMachine.addresses after ⊆ Insert.insert s.heap.length (SearchTree.PointerMachine.addresses before)
Old tree addresses survive, and the only possible new address is the fresh heap end.
- data (i : Fin (SearchTree.PointerMachine.layout extra (regs + 1)).dataRegs) : i ≠ 1 → i ≠ 2 → t.data i = s.data i
Query and client scalar registers survive; registers one and two are scratch.
- payload (a : ℕ) (f : Fin (extra + 1)) : a < s.heap.length → f ≠ field → Option.map (fun (n : PointerMachine.Record M (SearchTree.PointerMachine.layout extra (regs + 1))) => n.data f) t.heap[a]? = Option.map (fun (n : PointerMachine.Record M (SearchTree.PointerMachine.layout extra (regs + 1))) => n.data f) s.heap[a]?
Every old record retains all non-height scalar fields, even outside the tree.
- freshPayload : s.heap.length < t.heap.length → ∀ (f : Fin (extra + 1)), f ≠ field → Option.map (fun (n : PointerMachine.Record M (SearchTree.PointerMachine.layout extra (regs + 1))) => n.data f) t.heap[s.heap.length]? = some (if f = 0 then s.data 0 else s.data (source f))
When allocated, the fresh record retains the query and specified client payload.
An insertion result may be restated from an earlier state with the same heap and the same scalar registers other than the two scratch registers.
The concrete finite program implements AVLTree.insert, preserving linked representation, address separation and payloads while allocating at most one record. Its instruction bound is linear in the original AVL height and logarithmic in the number of stored keys; the additive term charges every initialized scalar field. All counts include allocation, jumps and halt.