Documentation

CslibFrontier.DataStructure.Rope.Balance

Ropes: local balance and the height bound #

Rope.balanceNode l r joins two well-formed ropes whose heights differ by at most two into one well-formed rope, using a single or a double rotation when one side is two levels taller. It is the constructor that join uses on its way back up the spine, following the AVL join of Blelloch, Ferizovic and Sun, Figure 2. The result represents l.toList ++ r.toList, and its height is between max l.height r.height and one more.

A well-formed rope of height h has at least Nat.fib (h + 2) leaves, since its sibling heights differ by at most one and its internal nodes have non-empty children. Each leaf holds at least one element, so the height is at most 2 * Nat.log 2 length. No + 1 is needed, because empty and a leaf both have height 0.

Main definitions #

Main results #

References #

Rotations #

def Cslib.DataStructure.Rope.balanceL {α : Type u} (l r : Rope α) :
Rope α

A node over l and r when l is two levels taller than r. A single right rotation is used when the outer grandchild ll is at least as tall as the inner one lr, and otherwise a double rotation through lr.

def Cslib.DataStructure.Rope.balanceR {α : Type u} (l r : Rope α) :
Rope α

A node over l and r when r is two levels taller than l: the mirror image of balanceL.

def Cslib.DataStructure.Rope.balanceNode {α : Type u} :
Rope α → Rope α → Rope α

A node over l and r that restores balance when their cached heights differ by two, and drops an empty side. It reads only cached heights of the children and grandchildren.

theorem Cslib.DataStructure.Rope.balanceNode_eq_mkNode {α : Type u} {l r : Rope α} (h₁ : l.height ≤ r.height + 1) (h₂ : r.height ≤ l.height + 1) :

balanceNode makes a plain node when the heights already differ by at most one.

The list model and the leaf chunks #

@[simp]

balanceNode keeps the leaf chunks of its arguments, in order.

@[simp]

balanceNode represents the concatenation of its arguments, with no hypotheses.

Well-formedness and the height effect #

theorem Cslib.DataStructure.Rope.isWF_balanceNode {α : Type u} {B : ℕ} {l r : Rope α} (hl : IsWF B l) (hr : IsWF B r) (h₁ : l.height ≤ r.height + 2) (h₂ : r.height ≤ l.height + 2) :

balanceNode over well-formed ropes whose heights differ by at most two is well-formed.

theorem Cslib.DataStructure.Rope.height_balanceNode {α : Type u} {B : ℕ} {l r : Rope α} (hl : IsWF B l) (hr : IsWF B r) (h₁ : l.height ≤ r.height + 2) (h₂ : r.height ≤ l.height + 2) :

The height of balanceNode is between max l.height r.height and one more. When one side is empty, the result is the other side, so the lower bound is an equality.

The height bound #

theorem Cslib.DataStructure.Rope.leaves_le_length {α : Type u} {B : ℕ} {t : Rope α} (hc : t.CacheCorrect) (hk : Chunked B t) :

A chunked rope has at most as many leaves as elements.

theorem Cslib.DataStructure.Rope.fib_height_le_leaves {α : Type u} {B : ℕ} {t : Rope α} (hw : IsWF B t) (hne : t.isEmpty = false) :

A non-empty well-formed rope of height h has at least Nat.fib (h + 2) leaves.

theorem Cslib.DataStructure.Rope.height_le_two_mul_log {α : Type u} {B : ℕ} {t : Rope α} (hw : IsWF B t) :

A well-formed rope has height at most 2 * Nat.log 2 length.