Height-balanced AVL binary search trees #
This module defines executable height-cached AVL binary search trees over any LinearOrder α,
proves preservation of strict key ordering, cached heights, and the AVL balance invariant across
rotations and insertion, and derives the logarithmic height bound
t.cachedHeight ≤ 2 * Nat.log 2 ((elements t).card + 1).
Each tree projects to BinaryTree α via toBinaryTree, reusing key-set semantics, strict ordering,
and query contracts from Cslib.DataStructure.SearchTree. Following
Cormen et al., Problem 13-3, every node caches its subtree height so balance checks
height differences in O(1) without rescanning subtrees. Heights use BinaryTree.height's
node-count convention (0 for .nil, 1 for a singleton leaf, i.e., h_CLRS + 1 on nonempty
trees), under which the minimum node count of a balanced tree of cached height h satisfies the
exact Fibonacci lower bound Nat.fib (h + 2) ≤ t.numNodes + 1.
Main definitions #
Cslib.DataStructure.AVLTree: binary search tree caching subtree heights at internal nodes.toBinaryTree,cachedHeight,numNodes,elements: structural projection, O(1) height read, node count, and representedFinset α.Ordered,HeightConsistent,Balanced,Valid: strict BST ordering, cached-height accuracy,±1sibling-height balance, and combined AVL validity.mkNode,rotateRight,rotateLeft,balance: height-maintaining constructor, single rotations, and single/double rebalancing for sibling heights differing by at most2.contains,minimum,successor,insert: executable search queries and rebalancing insertion.searchVisits,minimumVisits,successorVisits: downward search-path internal-node visit counts matchingCslib.DataStructure.SearchTree.
Main results #
cachedHeight_eq_height: underHeightConsistent,cachedHeightequalstoBinaryTree.height.fib_height_le_numNodes_add_one: Fibonacci node-count lower boundNat.fib (t.cachedHeight + 2) ≤ t.numNodes + 1.Valid.cachedHeight_le_two_mul_log: logarithmic height boundt.cachedHeight ≤ 2 * Nat.log 2 ((elements t).card + 1).contains_eq_true,minimum_spec,successor_spec: query correctness underOrdered.elements_balance,ordered_balance,heightConsistent_balance,balanced_and_height_balance,valid_and_height_balance: semantic and structural contracts forbalance, shared with deletion inAVLTree.Erase.elements_insert,valid_insert,cachedHeight_insert_le,insert_eq_self: set semantics, AVL validity preservation, height-growth bound, and duplicate idempotence forinsert.searchVisits_le_cachedHeight,minimumVisits_le_cachedHeight,successorVisits_le_cachedHeight: path-visit upper bounds byt.cachedHeight.
References #
Read the cached subtree height in O(1) without traversing children.
Forget cached heights to obtain the underlying BinaryTree α.
Number of internal nodes in t.
The empty tree has cached height 0.
An internal node's cachedHeight returns its stored height field.
Forgetting heights maps .nil to BinaryTree.nil.
Forgetting heights maps .node to BinaryTree.node recursively.
Every internal node's cachedHeight equals 1 + max l.cachedHeight r.cachedHeight.
Every internal node's children have cached heights differing by at most 1.
Under HeightConsistent, cachedHeight agrees with BinaryTree.height of toBinaryTree.
A labelled binary tree has at most one decoration by consistent cached heights.
Fibonacci node-count lower bound (Cormen et al., Problem 13-3(a)): any
height-consistent, AVL-balanced tree t satisfies
Nat.fib (t.cachedHeight + 2) ≤ t.numNodes + 1.
Construct a node computing its cached height in O(1) from its children's cached heights.
mkNode sets cachedHeight to 1 + max l.cachedHeight r.cachedHeight.
mkNode projects to BinaryTree.node under toBinaryTree.
mkNode preserves HeightConsistent whenever both children are height-consistent.
Rebalance a node whose children are height-balanced and differ in height by at most 2
(Cormen et al., Problem 13-3(b)). Inspects only O(1) cached child/grandchild
heights.
Single right rotation preserves HeightConsistent.
Single left rotation preserves HeightConsistent.
balance preserves HeightConsistent.
Main structural contract of balance: when l and r are height-consistent, balanced, and
differ in height by at most 2, balance a l r is balanced and its height lies in
[max l.cachedHeight r.cachedHeight, 1 + max l.cachedHeight r.cachedHeight] (equaling
1 + max l.cachedHeight r.cachedHeight whenever l and r already differ by at most 1).
The finite set of keys stored in t.
Strict binary-search-tree ordering on t.
Complete AVL validity invariant: strict key ordering, accurate cached heights, and ±1
sibling-height balance.
Extract strict BST ordering from Valid.
Extract cached-height accuracy from Valid.
Extract ±1 sibling-height balance from Valid.
The empty tree is a valid AVL tree.
The empty tree stores no keys.
An internal node's key set is its root key together with both subtrees' key sets.
A strict AVL tree stores each represented key in exactly one node.
Logarithmic height bound (Cormen et al., Problem 13-3(a)): every valid AVL tree t
has height at most 2 * Nat.log 2 ((elements t).card + 1) (in particular 0 when empty).
Executable membership search on AVLTree α without allocating toBinaryTree.
Executable minimum query on AVLTree α without allocating toBinaryTree.
Executable strict-successor query on AVLTree α without allocating toBinaryTree.
AVLTree.contains agrees with SearchTree.contains on toBinaryTree.
AVLTree.minimum agrees with SearchTree.minimum on toBinaryTree.
AVLTree.successor agrees with SearchTree.successor on toBinaryTree.
Leftmost descent returns the least stored key, or none when empty.
successor x t returns the least stored key strictly greater than x, or none.
mkNode stores a together with the keys of l and r.
Single right rotation preserves the represented key set.
Single left rotation preserves the represented key set.
balance preserves the represented key set unconditionally.
Single right rotation preserves strict BST ordering.
Single left rotation preserves strict BST ordering.
Full contract of balance on valid children: if l and r are valid AVL trees separated by
a whose heights differ by at most 2, then balance a l r is valid, and its height satisfies
the bounds of balanced_and_height_balance.
Insert x into an AVL tree, rebalancing ancestors along the search path
(Cormen et al., Problem 13-3(c)). Duplicate insertion leaves the node unchanged.
Insertion adds x to the represented Finset α.
insert preserves full AVL validity (Ordered, HeightConsistent, and Balanced).
insert increases the cached height by at most 1 (and never decreases it).
When l and r already differ in height by at most 1, balance performs no rotation.
Re-inserting an already-present key into a valid AVL tree leaves the entire tree unchanged.
Internal-node visits of contains and insert along the downward search path.
Internal-node visits of successor.
Search and insertion visit at most t.cachedHeight downward-path internal nodes.
Leftmost minimum descent visits at most t.cachedHeight internal nodes.
Strict-successor search visits at most t.cachedHeight internal nodes.