Documentation

CslibFrontier.DataStructure.AVLTree.Basic

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 #

Main results #

References #

inductive Cslib.DataStructure.AVLTree (α : Type u_1) :
Type u_1

A binary search tree storing cached subtree heights at internal nodes.

  • nil {α : Type u_1} : AVLTree α

    Empty tree of height 0.

  • node {α : Type u_1} (key : α) (cachedHeight : ℕ) (left right : AVLTree α) : AVLTree α

    Internal node storing key, cached subtree height cachedHeight, and children.

Instances For
    @[instance_reducible]
    def Cslib.DataStructure.instDecidableEqAVLTree.decEq {α✝ : Type u_1} [DecidableEq α✝] (x✝ x✝¹ : AVLTree α✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    instance Cslib.DataStructure.instReprAVLTree {α✝ : Type u_1} [Repr α✝] :
    Repr (AVLTree α✝)

    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.

    @[simp]

    The empty tree has cached height 0.

    @[simp]
    theorem Cslib.DataStructure.AVLTree.cachedHeight_node {α : Type u_1} (a : α) (h : ℕ) (l r : AVLTree α) :
    (node a h l r).cachedHeight = h

    An internal node's cachedHeight returns its stored height field.

    @[simp]

    Forgetting heights maps .node to BinaryTree.node recursively.

    @[simp]

    The empty tree has 0 internal nodes.

    @[simp]
    theorem Cslib.DataStructure.AVLTree.numNodes_node {α : Type u_1} (a : α) (h : ℕ) (l r : AVLTree α) :
    (node a h l r).numNodes = 1 + l.numNodes + r.numNodes

    An internal node has 1 + l.numNodes + r.numNodes nodes.

    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.

    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.

    def Cslib.DataStructure.AVLTree.mkNode {α : Type u_1} (a : α) (l r : AVLTree α) :

    Construct a node computing its cached height in O(1) from its children's cached heights.

    mkNode preserves HeightConsistent whenever both children are height-consistent.

    def Cslib.DataStructure.AVLTree.rotateRight {α : Type u_1} (a : α) :
    AVLTree α → AVLTree α → AVLTree α

    Single right rotation around a and a nonempty left child.

    def Cslib.DataStructure.AVLTree.rotateLeft {α : Type u_1} (a : α) (l : AVLTree α) :
    AVLTree α → AVLTree α

    Single left rotation around a and a nonempty right child.

    def Cslib.DataStructure.AVLTree.balance {α : Type u_1} (a : α) (l r : AVLTree α) :

    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.

    theorem Cslib.DataStructure.AVLTree.balance_ne_nil {α : Type u_1} (a : α) (l r : AVLTree α) :

    Rebalancing never returns the empty tree.

    Single right rotation preserves HeightConsistent.

    Single left rotation 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.

    theorem Cslib.DataStructure.AVLTree.Valid.ordered {α : Type u_1} [LinearOrder α] {t : AVLTree α} (ht : t.Valid) :

    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.

    @[simp]

    The empty tree stores no keys.

    @[simp]
    theorem Cslib.DataStructure.AVLTree.elements_node {α : Type u_1} [LinearOrder α] (a : α) (h : ℕ) (l r : AVLTree α) :

    An internal node's key set is its root key together with both subtrees' key sets.

    theorem Cslib.DataStructure.AVLTree.mem_elements_node {α : Type u_1} [LinearOrder α] {x a : α} {h : ℕ} {l r : AVLTree α} :
    x ∈ (node a h l r).elements ↔ x = a ∨ x ∈ l.elements ∨ x ∈ r.elements

    Membership in an internal node's key set splits across the root key and its two subtrees.

    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.

    def Cslib.DataStructure.AVLTree.successor {α : Type u_1} [LinearOrder α] (x : α) :
    AVLTree α → Option α

    Executable strict-successor query on AVLTree α without allocating toBinaryTree.

    theorem Cslib.DataStructure.AVLTree.contains_eq_true {α : Type u_1} [LinearOrder α] {t : AVLTree α} (ht : t.Ordered) (x : α) :

    Membership search agrees with finite-set membership on any ordered tree.

    Leftmost descent returns the least stored key, or none when empty.

    theorem Cslib.DataStructure.AVLTree.successor_spec {α : Type u_1} [LinearOrder α] {t : AVLTree α} (ht : t.Ordered) (x : α) :
    OrderedSet.LeastResult ({x_1 ∈ t.elements | x < x_1}) (successor x t)

    successor x t returns the least stored key strictly greater than x, or none.

    @[simp]
    theorem Cslib.DataStructure.AVLTree.elements_mkNode {α : Type u_1} [LinearOrder α] (a : α) (l r : AVLTree α) :

    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.

    @[simp]
    theorem Cslib.DataStructure.AVLTree.elements_balance {α : Type u_1} [LinearOrder α] (a : α) (l r : AVLTree α) :

    balance preserves the represented key set unconditionally.

    theorem Cslib.DataStructure.AVLTree.ordered_rotateRight {α : Type u_1} [LinearOrder α] {a : α} {h : ℕ} {l r : AVLTree α} (ho : (node a h l r).Ordered) :

    Single right rotation preserves strict BST ordering.

    theorem Cslib.DataStructure.AVLTree.ordered_rotateLeft {α : Type u_1} [LinearOrder α] {a : α} {h : ℕ} {l r : AVLTree α} (ho : (node a h l r).Ordered) :

    Single left rotation preserves strict BST ordering.

    theorem Cslib.DataStructure.AVLTree.ordered_balance {α : Type u_1} [LinearOrder α] {a : α} {h : ℕ} {l r : AVLTree α} (ho : (node a h l r).Ordered) :

    balance preserves strict key ordering.

    theorem Cslib.DataStructure.AVLTree.valid_and_height_balance {α : Type u_1} [LinearOrder α] (a : α) {l r : AVLTree α} (hl : l.Valid) (hr : r.Valid) (hla : ∀ y ∈ l.elements, y < a) (har : ∀ y ∈ r.elements, a < y) (hlr : l.cachedHeight ≤ r.cachedHeight + 2) (hrl : r.cachedHeight ≤ l.cachedHeight + 2) :

    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.

    def Cslib.DataStructure.AVLTree.insert {α : Type u_1} [LinearOrder α] (x : α) :
    AVLTree α → AVLTree α

    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.

    @[simp]

    Insertion adds x to the represented Finset α.

    theorem Cslib.DataStructure.AVLTree.valid_insert {α : Type u_1} [LinearOrder α] {t : AVLTree α} (ht : t.Valid) (x : α) :

    insert preserves full AVL validity (Ordered, HeightConsistent, and Balanced).

    insert increases the cached height by at most 1 (and never decreases it).

    theorem Cslib.DataStructure.AVLTree.balance_eq_node {α : Type u_1} [LinearOrder α] {a : α} {h : ℕ} {l r : AVLTree α} (hh : h = 1 + max l.cachedHeight r.cachedHeight) (hlr : l.cachedHeight ≤ r.cachedHeight + 1) (hrl : r.cachedHeight ≤ l.cachedHeight + 1) :
    balance a l r = node a h l r

    When l and r already differ in height by at most 1, balance performs no rotation.

    theorem Cslib.DataStructure.AVLTree.insert_eq_self {α : Type u_1} [LinearOrder α] {t : AVLTree α} (ht : t.Valid) {x : α} (hx : x ∈ t.elements) :
    insert x t = t

    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 minimum along the left spine.

    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.