Unbalanced binary search trees #
An ordinary binary search tree reuses Mathlib's BinaryTree. Keys strictly smaller than the
root occur on the left, larger keys on the right. Inserting a duplicate leaves the tree unchanged.
These are the textbook search and insertion algorithms of Cormen et al., Chapter 12.
Main definitions #
elements: the finite set represented by a tree.Ordered: the strict binary-search-tree invariant.contains,insert,minimum,successor: executable search-tree operations.
Main results #
contains_eq_true: membership is exactly finite-set membership.elements_insert,ordered_insert: insertion has set semantics and preserves the invariant.minimum_spec,successor_spec: queries return the least eligible key, ornone.
Trees are not balanced. Cost bounds in the companion module depend on their actual height.
References #
The finite set of keys stored at internal nodes.
Instances For
Strict subtree separation rules out duplicate keys.
Instances For
Follow a single search path; no subtree is scanned.
Instances For
Insert at the first empty child on the search path; equality preserves the existing node.
Instances For
Follow left children to find the least key.
Instances For
The least stored key strictly greater than x, including when x is absent.
Instances For
Search is correct for any shape satisfying the invariant.
Insertion adds exactly the requested key, even when it was already present.
The binary-search-tree invariant survives insertion.
Re-inserting a present key preserves the entire tree, not just its represented set.
A strict search tree stores each represented key in exactly one node.
Left descent returns the least element, with the empty case accounted for.
Successor is strict and does not require the query itself to belong to the tree.