Lemmas for Red-black trees #
The main theorem in this file is WF_def
, which shows that the RBNode.WF.mk
constructor
subsumes the others, by showing that insert
and erase
satisfy the red-black invariants.
The balance1
function preserves the ordering invariants.
The balance2
function preserves the ordering invariants.
The ins
function preserves the ordering invariants.
The insert
function preserves the ordering invariants.
- balanced: ∀ {p : Prop} {x : Type u_1} {t : Std.RBNode x} {c : Std.RBColor} {n : Nat},
Std.RBNode.Balanced t c n → Std.RBNode.RedRed p t n
A balanced tree has the red-red invariant.
- redred: ∀ {p : Prop} {x : Type u_1} {a : Std.RBNode x} {c₁ : Std.RBColor} {n : Nat} {b : Std.RBNode x} {c₂ : Std.RBColor}
{x_1 : x},
p →
Std.RBNode.Balanced a c₁ n →
Std.RBNode.Balanced b c₂ n → Std.RBNode.RedRed p (Std.RBNode.node Std.RBColor.red a x_1 b) n
A red node with balanced red children has the red-red invariant (if
p
is true).
The red-red invariant is a weakening of the red-black balance invariant which allows
the root to be red with red children, but does not allow any other violations.
It occurs as a temporary condition in the insert
and erase
functions.
The p
parameter allows the .redred
case to be dependent on an additional condition.
If it is false, then this is equivalent to the usual red-black invariant.
Instances For
When p
is false, the red-red case is impossible so the tree is balanced.
A red
node with the red-red invariant has balanced children.
The red-red invariant is monotonic in p
.
If t
has the red-red invariant, then setting the root to black yields a balanced tree.
The balance1
function repairs the balance invariant when the first argument is red-red.
The balance2
function repairs the balance invariant when the second argument is red-red.
The balance1
function does nothing if the first argument is already balanced.
The balance2
function does nothing if the second argument is already balanced.
insert #
The balance invariant of the ins
function.
The result of inserting into the tree either yields a balanced tree,
or a tree which is almost balanced except that it has a red-red violation at the root.
The insert
function is balanced if the input is balanced.
(We lose track of both the color and the black-height of the result,
so this is only suitable for use on the root of the tree.)
The setRed
function preserves the ordering invariants.
The balLeft
function preserves the ordering invariants.
The balancing properties of the balLeft
function.
The balRight
function preserves the ordering invariants.
The balancing properties of the balRight
function.
The append
function preserves the ordering invariants.
The balance properties of the append
function.
erase #
The invariant of the del
function.
- If the input tree is black, then the result of deletion is a red-red tree with black-height lowered by 1.
- If the input tree is red or nil, then the result of deletion is a balanced tree with some color and the same black-height.
Equations
- Std.RBNode.DelProp p t n = match p with | Std.RBColor.black => ∃ n', n = n' + 1 ∧ Std.RBNode.RedRed True t n' | Std.RBColor.red => ∃ c, Std.RBNode.Balanced t c n
Instances For
The DelProp
property is a strengthened version of the red-red invariant.
The del
function preserves the ordering invariants.
The erase
function preserves the ordering invariants.
The erase
function preserves the balance invariants.
The well-formedness invariant implies the ordering and balance properties.
The well-formedness invariant for a red-black tree is exactly the mk
constructor,
because the other constructors of WF
are redundant.
The map
function preserves the balance invariants.
- lt_mono : ∀ {x y : α}, Std.RBNode.cmpLT cmpα x y → Std.RBNode.cmpLT cmpβ (f x) (f y)
If
x < y
thenf x < f y
.
The property of a map function f
which ensures the map
operation is valid.
Instances
Sufficient condition for map
to preserve an All
quantifier.
The map
function preserves the order invariants if f
is monotone.
O(n)
. Map a function on every value in the tree.
This requires IsMonotone
on the function in order to preserve the order invariant.
Equations
- Std.RBSet.map f t = { val := Std.RBNode.map f t.val, property := (_ : Std.RBNode.WF cmpβ (Std.RBNode.map f t.val)) }
Instances For
Applies f
to the second component.
We extract this as a function so that IsMonotone (mapSnd f)
can be an instance.
Equations
- Std.RBMap.Imp.mapSnd f x = match x with | (a, b) => (a, f a b)
Instances For
O(n)
. Map a function on the values in the map.
Equations
- Std.RBMap.mapVal f t = Std.RBSet.map (Std.RBMap.Imp.mapSnd f) t