Documentation

CslibFrontier.DataStructure.AVLTree.PointerMachine.Representation

Linked representations of existing AVL trees #

Matches couples a ghost-labelled linked tree to the existing AVLTree: both its keys and its encoded cached heights agree, at every node. Heap contents remain governed by SearchTree.PointerMachine.Represents; no second AVL tree type is introduced.

recomputeHeight_mkNode proves that the concrete nine-instruction height update implements the cached-height constructor AVLTree.mkNode, preserving the matched children. It requires consistent child heights but permits an arbitrary old root cache.

Rebuilt is the contract of a routine that rebuilds the subtree under the cursor in place; the flat and the structured rebalancing programs both establish it.

Same tree shape, scalar keys, and encoded cached heights at every node. The height field must be distinct from key field zero when updating either independently.

theorem Cslib.DataStructure.AVLTree.PointerMachine.Matches.keys {M : PointerMachine.Model} {extra : ℕ} {encode : ℕ → M.Value} {field : Fin (extra + 1)} {tree : BinaryTree (SearchTree.PointerMachine.Label M extra)} {t : AVLTree M.Value} (h : Matches encode field tree t) :

A complete AVL match supplies the key-only representation used by search.

theorem Cslib.DataStructure.AVLTree.PointerMachine.Matches.shape_height {M : PointerMachine.Model} {extra : ℕ} {encode : ℕ → M.Value} {field : Fin (extra + 1)} {tree : BinaryTree (SearchTree.PointerMachine.Label M extra)} {t : AVLTree M.Value} (h : Matches encode field tree t) :

A full AVL match preserves the structural height of the underlying binary tree.

theorem Cslib.DataStructure.AVLTree.PointerMachine.Matches.heightValue {M : PointerMachine.Model} {extra : ℕ} {encode : ℕ → M.Value} {field : Fin (extra + 1)} {tree : BinaryTree (SearchTree.PointerMachine.Label M extra)} {t : AVLTree M.Value} (h : Matches encode field tree t) :

A matched root cache is exactly the encoded AVL cached height, with zero for an empty tree.

theorem Cslib.DataStructure.AVLTree.PointerMachine.recomputeHeight_mkNode {M : PointerMachine.Model} {extra regs : ℕ} (zero maxSucc : M.Op) (encode : ℕ → M.Value) (zero_spec : ∀ (a b : M.Value), M.evalOp zero a b = encode 0) (max_spec : ∀ (a b : ℕ), M.evalOp maxSucc (encode a) (encode b) = encode (max a b + 1)) (field : Fin (extra + 1)) (notKey : field ≠ 0) (x : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))) (label : SearchTree.PointerMachine.Label M extra) (l r : BinaryTree (SearchTree.PointerMachine.Label M extra)) (left right : AVLTree M.Value) (parent : PointerMachine.Pointer) (rep : SearchTree.PointerMachine.Represents x.heap parent (BinaryTree.node label l r)) (distinct : SearchTree.PointerMachine.AddressesDistinct (BinaryTree.node label l r)) (ml : Matches encode field l left) (mr : Matches encode field r right) (hl : left.HeightConsistent) (hr : right.HeightConsistent) (cursor : x.ptr 0 = some label.address) (pc : x.pc = 0) :
have value := encode (mkNode (label.data 0) left right).cachedHeight; have tree := BinaryTree.node { address := label.address, data := Function.update label.data field value } l r; ∃ (y : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))), PointerMachine.Executes (SearchTree.PointerMachine.recomputeHeight zero maxSucc field) x y 9 ∧ (y.heap = x.heap.modify label.address fun (n : PointerMachine.Record M (SearchTree.PointerMachine.layout extra (regs + 1))) => { ptr := n.ptr, data := Function.update n.data field value }) ∧ SearchTree.PointerMachine.Represents y.heap parent tree ∧ Matches encode field tree (mkNode (label.data 0) left right) ∧ SearchTree.PointerMachine.AddressesDistinct tree ∧ (∀ (i : Fin (SearchTree.PointerMachine.layout extra (regs + 1)).dataRegs), i ≠ 1 → i ≠ 2 → y.data i = x.data i) ∧ ∀ (i : Fin (SearchTree.PointerMachine.layout extra (regs + 1)).ptrRegs), i ≠ 2 → y.ptr i = x.ptr i

Nine actual instructions implement mkNode's height recomputation on an existing linked root. Its two children retain their complete AVL matches; only the root height field and the declared scratch registers change. Allocation and rebalancing are separate.

structure Cslib.DataStructure.AVLTree.PointerMachine.Rebuilt {M : PointerMachine.Model} {extra regs : ℕ} (encode : ℕ → M.Value) (field : Fin (extra + 1)) (s t : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))) (parent : PointerMachine.Pointer) (old tree : BinaryTree (SearchTree.PointerMachine.Label M extra)) (target : AVLTree M.Value) :

The subtree old under the cursor of s has been rebuilt in place as tree, which matches target: it is represented under the same parent, occupies the same addresses, the cursor is at its root, register six holds the former root, and the root register and every record outside it are unchanged.