Documentation

CslibFrontier.DataStructure.AVLTree.Side

AVL operations relative to a side #

Rotations and rebalancing come in mirror-image pairs. The definitions here name a child by its side instead, so that a statement about "the child on side right" covers both: nodeSide, mkNodeAt, rotateAt and balanceAt are AVLTree.node, mkNode, the rotation that promotes that child, and balance, with the distinguished child placed on the selected side.

Main results #

def Cslib.DataStructure.AVLTree.nodeSide {α : Type u_1} (right : Bool) (a : α) (h : ℕ) (child sibling : AVLTree α) :

A node with its distinguished child on the selected side.

def Cslib.DataStructure.AVLTree.mkNodeAt {α : Type u_1} (right : Bool) (a : α) (child sibling : AVLTree α) :

mkNode with the distinguished child on the selected side.

def Cslib.DataStructure.AVLTree.rotateAt {α : Type u_1} (right : Bool) (a : α) (child sibling : AVLTree α) :

The rotation that promotes the child on the selected side.

def Cslib.DataStructure.AVLTree.balanceAt {α : Type u_1} (right : Bool) (a : α) (child sibling : AVLTree α) :

balance with the distinguished child on the selected side.

theorem Cslib.DataStructure.AVLTree.rotateAt_nodeSide {α : Type u_1} (right : Bool) (a k : α) (h : ℕ) (outer inner sibling : AVLTree α) :
rotateAt right a (nodeSide right k h outer inner) sibling = mkNodeAt right k outer (mkNodeAt right a inner sibling)

Promoting a child, whose own children are outer on the same side and inner on the other, puts outer on that side and the old root, with inner and the sibling, on the other.

theorem Cslib.DataStructure.AVLTree.mkNodeAt_eq_nodeSide {α : Type u_1} (right : Bool) (a : α) (child sibling : AVLTree α) :
mkNodeAt right a child sibling = nodeSide right a (1 + max child.cachedHeight sibling.cachedHeight) child sibling

mkNodeAt is a node with the height of mkNode.

theorem Cslib.DataStructure.AVLTree.nodeSide_not {α : Type u_1} (right : Bool) (a : α) (h : ℕ) (child sibling : AVLTree α) :
nodeSide (!right) a h sibling child = nodeSide right a h child sibling

Exchanging the side and the two subtrees gives the same node.

theorem Cslib.DataStructure.AVLTree.HeightConsistent.of_nodeSide {α : Type u_1} {right : Bool} {a : α} {h : ℕ} {child sibling : AVLTree α} (hc : (nodeSide right a h child sibling).HeightConsistent) :

The children of a height-consistent node are height-consistent.

theorem Cslib.DataStructure.AVLTree.heightConsistent_mkNodeAt {α : Type u_1} (right : Bool) {a : α} {child sibling : AVLTree α} (hc : child.HeightConsistent) (hs : sibling.HeightConsistent) :
(mkNodeAt right a child sibling).HeightConsistent

mkNodeAt preserves height consistency.

theorem Cslib.DataStructure.AVLTree.balanceAt_heavy {α : Type u_1} (right : Bool) (a k : α) (h : ℕ) (outer inner sibling : AVLTree α) (heavy : sibling.cachedHeight + 1 < h) :
balanceAt right a (nodeSide right k h outer inner) sibling = if inner.cachedHeight ≤ outer.cachedHeight then rotateAt right a (nodeSide right k h outer inner) sibling else rotateAt right a (rotateAt (!right) k inner outer) sibling

At a node whose child on a side is at least two taller than its sibling, balance promotes that child, after first promoting its inner child when that one is the taller.

theorem Cslib.DataStructure.AVLTree.balanceAt_balanced {α : Type u_1} (right : Bool) (a : α) (child sibling : AVLTree α) (hc : ¬sibling.cachedHeight + 1 < child.cachedHeight) (hs : ¬child.cachedHeight + 1 < sibling.cachedHeight) :
balanceAt right a child sibling = mkNodeAt right a child sibling

At a node whose children differ in height by at most one, balance only recomputes the height.

theorem Cslib.DataStructure.AVLTree.insert_nodeSide {α : Type u_1} [LinearOrder α] (right : Bool) (a : α) (h : ℕ) (child sibling : AVLTree α) (x : α) (side : if right = true then a < x else x < a) :
insert x (nodeSide right a h child sibling) = balanceAt right a (insert x child) sibling

Inserting on the side of the distinguished child inserts into that child and rebalances.