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 reverse
function reverses the ordering 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.
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.
- balanced: ∀ {p : Prop} {x : Type u_1} {t : Batteries.RBNode x} {c : Batteries.RBColor} {n : Nat},
t.Balanced c n → Batteries.RBNode.RedRed p t n
A balanced tree has the red-red invariant.
- redred: ∀ {p : Prop} {x : Type u_1} {a : Batteries.RBNode x} {c₁ : Batteries.RBColor} {n : Nat} {b : Batteries.RBNode x}
{c₂ : Batteries.RBColor} {x_1 : x},
p →
a.Balanced c₁ n →
b.Balanced c₂ n → Batteries.RBNode.RedRed p (Batteries.RBNode.node Batteries.RBColor.red a x_1 b) n
A red node with balanced red children has the red-red invariant (if
p
is true).
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
- Batteries.RBNode.DelProp Batteries.RBColor.black t n = ∃ (n' : Nat), n = n' + 1 ∧ Batteries.RBNode.RedRed True t n'
- Batteries.RBNode.DelProp Batteries.RBColor.red t n = ∃ (c : Batteries.RBColor), t.Balanced 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.
The property of a map function f
which ensures the map
operation is valid.
- lt_mono : ∀ {x y : α}, Batteries.RBNode.cmpLT cmpα x y → Batteries.RBNode.cmpLT cmpβ (f x) (f y)
If
x < y
thenf x < f y
.
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 set.
This requires IsMonotone
on the function in order to preserve the order invariant.
If the function is not monotone, use RBSet.map
instead.
Equations
- Batteries.RBSet.mapMonotone f t = ⟨Batteries.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
- Batteries.RBMap.Imp.mapSnd f (a, b) = (a, f a b)
Instances For
Equations
- ⋯ = ⋯
O(n)
. Map a function on the values in the map.