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 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 : RBNode x✝}
{c : RBColor}
{n : Nat}
: t.Balanced c n → RedRed p t n
A balanced tree has the red-red invariant.
- redred
{p : Prop}
{x✝ : Type u_1}
{a : RBNode x✝}
{c₁ : RBColor}
{n : Nat}
{b : RBNode x✝}
{c₂ : RBColor}
{x : x✝}
: p → a.Balanced c₁ n → b.Balanced c₂ n → RedRed p (node RBColor.red a x b) n
A red node with balanced red children has the red-red invariant (if
p
is true).
Instances For
If t
has the red-red invariant, then setting the root to black yields a balanced tree.
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 balLeft
function preserves the ordering invariants.
The balRight
function preserves the ordering invariants.
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 well-formedness invariant for a red-black tree is exactly the mk
constructor,
because the other constructors of WF
are redundant.
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)