The areas / V33. Data structures and computation
Mathlib.Data.Tree.Basic
32 declarations and 11 deprecated aliases. Mathlib docs · Source
Binary tree
Provides binary tree storage for values of any type, with O(lg n) retrieval.
See also Lean.Data.RBTree for red-black trees - this version allows more operations
to be defined and is better suited for in-kernel computation.
We also specialize for BinaryTree Unit, which is a binary tree without any
additional data. We provide the notation a △ b for making a BinaryTree Unit with children
a and b.
References
https://leanprover-community.github.io/archive/stream/113488-general/topic/tactic.20question.html
abbrev BinaryTree.brecOn.go E1 #
{α : Type u} →
{motive : BinaryTree α → Sort u_1} →
(t : BinaryTree α) → ((t : BinaryTree α) → BinaryTree.below t → motive t) → motive t ×' BinaryTree.below t
theorem BinaryTree.node.injEq E0 #
∀ {α : Type u} (value : α) (left right : BinaryTree α) (value_1 : α) (left_1 right_1 : BinaryTree α),
(BinaryTree.node value left right = BinaryTree.node value_1 left_1 right_1) =
(value = value_1 ∧ left = left_1 ∧ right = right_1)
theorem BinaryTree.brecOn.eq E1 #
∀ {α : Type u} {motive : BinaryTree α → Sort u_1} (t : BinaryTree α)
(F_1 : (t : BinaryTree α) → BinaryTree.below t → motive t),
BinaryTree.brecOn t F_1 = F_1 t (BinaryTree.brecOn.go t F_1).2
theorem BinaryTree.node.inj E1 #
∀ {α : Type u} {value : α} {left right : BinaryTree α} {value_1 : α} {left_1 right_1 : BinaryTree α},
BinaryTree.node value left right = BinaryTree.node value_1 left_1 right_1 →
value = value_1 ∧ left = left_1 ∧ right = right_1
def BinaryTree.ctorIdx E1 #
{α : Type u} → BinaryTree α → ℕ
theorem BinaryTree.traverse.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} (motive : BinaryTree α → Sort u_2) (x : BinaryTree α) (h_1 : Unit → motive BinaryTree.nil)
(h_2 : (a : α) → (l r : BinaryTree α) → motive (BinaryTree.node a l r)),
x = BinaryTree.nil →
(match x with
| BinaryTree.nil => h_1 ()
| BinaryTree.node a l r => h_2 a l r) ≍
h_1 ()
theorem BinaryTree.traverse.match_1.congr_eq_2 E1 #
∀ {α : Type u_1} (motive : BinaryTree α → Sort u_2) (x : BinaryTree α) (h_1 : Unit → motive BinaryTree.nil)
(h_2 : (a : α) → (l r : BinaryTree α) → motive (BinaryTree.node a l r)) (a : α) (l r : BinaryTree α),
x = BinaryTree.node a l r →
(match x with
| BinaryTree.nil => h_1 ()
| BinaryTree.node a l r => h_2 a l r) ≍
h_2 a l r
abbrev BinaryTree.ctorElimType E1 #
{α : Type u} → {motive : BinaryTree α → Sort u_1} → ℕ → Sort (max 1 u_1 (imax (u + 1) (u + 1) (u + 1) u_1))
inductive BinaryTree E3 #
Type u → Type u
A binary tree with values stored in non-leaf nodes.
ctor BinaryTree.nil E0 #
{α : Type u} → BinaryTree α
ctor BinaryTree.node E0 #
{α : Type u} → α → BinaryTree α → BinaryTree α → BinaryTree α
instance instReprBinaryTree E0 #
{α : Type u_1} → [Repr α] → Repr (BinaryTree α)
def instReprBinaryTree.repr E1 #
{α : Type u_1} → [Repr α] → BinaryTree α → ℕ → Std.Format
instance instDecidableEqBinaryTree E0 #
{α : Type u_1} → [DecidableEq α] → DecidableEq (BinaryTree α)
def instDecidableEqBinaryTree.decEq E1 #
{α : Type u_1} → [DecidableEq α] → (x x_1 : BinaryTree α) → Decidable (x = x_1)
instance BinaryTree.instInhabited E0 #
{α : Type u} → Inhabited (BinaryTree α)
def BinaryTree.traverse E1 #
{m : Type u_1 → Type u_2} →
[Applicative m] → {α : Type u_3} → {β : Type u_1} → (α → m β) → BinaryTree α → m (BinaryTree β)
Do an action for every node of the tree.
Actions are taken in node -> left subtree -> right subtree recursive order.
This function is the traverse function for the Traversable BinaryTree instance.
def BinaryTree.map E1 #
{α : Type u} → {β : Type u_1} → (α → β) → BinaryTree α → BinaryTree β
Apply a function to each value in the BinaryTree.
This is the map function for the BinaryTree functor.
theorem BinaryTree.id_map E1 #
∀ {α : Type u} (t : BinaryTree α), BinaryTree.map id t = t
theorem BinaryTree.comp_map E1 #
∀ {α : Type u} {β : Type u_1} {γ : Type u_2} (f : α → β) (g : β → γ) (t : BinaryTree α),
BinaryTree.map (g ∘ f) t = BinaryTree.map g (BinaryTree.map f t)
theorem BinaryTree.traverse_pure E1 #
∀ {α : Type u} (t : BinaryTree α) {m : Type u → Type u_1} [inst : Applicative m] [LawfulApplicative m],
BinaryTree.traverse pure t = pure t
def BinaryTree.numNodes E1 #
{α : Type u} → BinaryTree α → ℕ
The number of internal nodes (i.e. not including leaves) of a binary tree
def BinaryTree.numLeaves E1 #
{α : Type u} → BinaryTree α → ℕ
The number of leaves of a binary tree
def BinaryTree.height E1 #
{α : Type u} → BinaryTree α → ℕ
The height - length of the longest path from the root - of a binary tree
theorem BinaryTree.numLeaves_eq_numNodes_succ E1 #
∀ {α : Type u} (x : BinaryTree α), x.numLeaves = x.numNodes + 1
theorem BinaryTree.numLeaves_pos E1 #
∀ {α : Type u} (x : BinaryTree α), 0 < x.numLeaves
theorem BinaryTree.height_le_numNodes E1 #
∀ {α : Type u} (x : BinaryTree α), x.height ≤ x.numNodes
def BinaryTree.left E1 #
{α : Type u} → BinaryTree α → BinaryTree α
The left child of the tree, or nil if the tree is nil
def BinaryTree.right E1 #
{α : Type u} → BinaryTree α → BinaryTree α
The right child of the tree, or nil if the tree is nil
def BinaryTree.«term_△_» E1 #
Lean.TrailingParserDescr
A node with Unit data
def BinaryTree.unitRecOn E1 #
{motive : BinaryTree Unit → Sort u_1} →
(t : BinaryTree Unit) →
motive BinaryTree.nil → ((x y : BinaryTree Unit) → motive x → motive y → motive (BinaryTree.node () x y)) → motive t
Induction principle for BinaryTree Units
theorem BinaryTree.left_node_right_eq_self E1 #
∀ {x : BinaryTree Unit}, x ≠ BinaryTree.nil → BinaryTree.node () x.left x.right = x
Deprecated aliases
| Name | New name | Since |
|---|---|---|
Tree | BinaryTree | 2026-06-07 |
Tree.height | BinaryTree.height | 2026-06-07 |
Tree.left | BinaryTree.left | 2026-06-07 |
Tree.map | BinaryTree.map | 2026-06-07 |
Tree.nil | BinaryTree.nil | 2026-06-07 |
Tree.node | BinaryTree.node | 2026-06-07 |
Tree.numLeaves | BinaryTree.numLeaves | 2026-06-07 |
Tree.numNodes | BinaryTree.numNodes | 2026-06-07 |
Tree.right | BinaryTree.right | 2026-06-07 |
Tree.traverse | BinaryTree.traverse | 2026-06-07 |
Tree.unitRecOn | BinaryTree.unitRecOn | 2026-06-07 |