Path operations; modify
and alter
#
This develops the necessary theorems to construct the modify
and alter
functions on RBSet
using path operations for in-place modification of an RBTree
.
path balance #
The balance invariant for a path. path.Balanced c₀ n₀ c n
means that path
is a red-black tree
with balance invariant c₀, n₀
, but it has a "hole" where a tree with balance invariant c, n
has been removed. The defining property is Balanced.fill
: if path.Balanced c₀ n₀ c n
and you
fill the hole with a tree satisfying t.Balanced c n
, then (path.fill t).Balanced c₀ n₀
.
- root
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
: Path.Balanced c₀ n₀ root c₀ n₀
The root of the tree is
c₀, n₀
-balanced by assumption. - redL
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
{y : RBNode α}
{n : Nat}
{parent : Path α}
{v : α}
: y.Balanced RBColor.black n →
Path.Balanced c₀ n₀ parent RBColor.red n → Path.Balanced c₀ n₀ (left RBColor.red parent v y) RBColor.black n
Descend into the left subtree of a red node.
- redR
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
{x : RBNode α}
{n : Nat}
{v : α}
{parent : Path α}
: x.Balanced RBColor.black n →
Path.Balanced c₀ n₀ parent RBColor.red n → Path.Balanced c₀ n₀ (right RBColor.red x v parent) RBColor.black n
Descend into the right subtree of a red node.
- blackL
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
{y : RBNode α}
{c₂ : RBColor}
{n : Nat}
{parent : Path α}
{v : α}
{c₁ : RBColor}
: y.Balanced c₂ n →
Path.Balanced c₀ n₀ parent RBColor.black (n + 1) → Path.Balanced c₀ n₀ (left RBColor.black parent v y) c₁ n
Descend into the left subtree of a black node.
- blackR
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
{x : RBNode α}
{c₁ : RBColor}
{n : Nat}
{v : α}
{parent : Path α}
{c₂ : RBColor}
: x.Balanced c₁ n →
Path.Balanced c₀ n₀ parent RBColor.black (n + 1) → Path.Balanced c₀ n₀ (right RBColor.black x v parent) c₂ n
Descend into the right subtree of a black node.
Instances For
The defining property of a balanced path: If path
is a c₀,n₀
tree with a c,n
hole,
then filling the hole with a c,n
tree yields a c₀,n₀
tree.
path.RootOrdered cmp v
is true if v
would be able to fit into the hole
without violating the ordering invariant.
Equations
Instances For
alter #
The alter
function preserves the ordering invariants.
A sufficient condition for ModifyWF
is that the new element compares equal to the original.
O(log n)
. In-place replace the corresponding to key k
.
This takes the element out of the tree while f
runs,
so it uses the element linearly if t
is unshared.
Equations
Instances For
O(log n)
. alterP cut f t
simultaneously handles inserting, erasing and replacing an element
using a function f : Option α → Option α
. It is passed the result of t.findP? cut
and can either return none
to remove the element or some a
to replace/insert
the element with a
(which must have the same ordering properties as the original element).
The element is used linearly if t
is unshared.
The AlterWF
assumption is required because f
may change
the ordering properties of the element, which would break the invariants.