Skip to content

Commit eaa4d40

Browse files
chore(Data/Tree/Basic): Rename Tree to BinaryTree (#39707)
Rename `Tree` to `BinaryTree`. This frees up the name `Tree` to cover n-ary trees in future. We only deprecate the `def`s, and move the theorems without deprecation. Co-authored-by: Eric Wieser <efw@google.com> Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
1 parent d90090f commit eaa4d40

7 files changed

Lines changed: 150 additions & 62 deletions

File tree

Mathlib/Combinatorics/Enumerative/Catalan/Tree.lean

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ open Finset
2828

2929
open Finset.antidiagonal (fst_le snd_le)
3030

31-
namespace Tree
31+
namespace BinaryTree
3232

3333
/-- Given two finsets, find all trees that can be formed with
3434
left child in `a` and right child in `b` -/
35-
abbrev pairwiseNode (a b : Finset (Tree Unit)) : Finset (Tree Unit) :=
35+
abbrev pairwiseNode (a b : Finset (BinaryTree Unit)) : Finset (BinaryTree Unit) :=
3636
(a ×ˢ b).map ⟨fun x => x.1 △ x.2, fun ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ => fun h => by simpa using h⟩
3737

3838
/-- A Finset of all trees with `n` nodes. See `mem_treesOfNodesEq` -/
39-
def treesOfNumNodesEq : ℕ → Finset (Tree Unit)
39+
def treesOfNumNodesEq : ℕ → Finset (BinaryTree Unit)
4040
| 0 => {nil}
4141
| n + 1 =>
4242
(antidiagonal n).attach.biUnion fun ijh =>
@@ -45,6 +45,12 @@ def treesOfNumNodesEq : ℕ → Finset (Tree Unit)
4545
· simp_wf; have := fst_le ijh.2; lia
4646
· simp_wf; have := snd_le ijh.2; lia
4747

48+
set_option linter.deprecated false in
49+
/-- **Alias** of `BinaryTree.treesOfNumNodesEq`. -/
50+
@[deprecated BinaryTree.treesOfNumNodesEq (since := "2026-06-07")]
51+
abbrev _root_.Tree.treesOfNumNodesEq : ℕ → Finset (Tree Unit) :=
52+
BinaryTree.treesOfNumNodesEq
53+
4854
@[simp]
4955
theorem treesOfNumNodesEq_zero : treesOfNumNodesEq 0 = {nil} := by rw [treesOfNumNodesEq]
5056

@@ -57,17 +63,17 @@ theorem treesOfNumNodesEq_succ (n : ℕ) :
5763
simp
5864

5965
@[simp]
60-
theorem mem_treesOfNumNodesEq {x : Tree Unit} {n : ℕ} :
66+
theorem mem_treesOfNumNodesEq {x : BinaryTree Unit} {n : ℕ} :
6167
x ∈ treesOfNumNodesEq n ↔ x.numNodes = n := by
62-
induction x using Tree.unitRecOn generalizing n <;> cases n <;>
68+
induction x using BinaryTree.unitRecOn generalizing n <;> cases n <;>
6369
simp [treesOfNumNodesEq_succ, *]
6470

65-
theorem mem_treesOfNumNodesEq_numNodes (x : Tree Unit) : x ∈ treesOfNumNodesEq x.numNodes :=
71+
theorem mem_treesOfNumNodesEq_numNodes (x : BinaryTree Unit) : x ∈ treesOfNumNodesEq x.numNodes :=
6672
mem_treesOfNumNodesEq.mpr rfl
6773

6874
@[simp, norm_cast]
6975
theorem coe_treesOfNumNodesEq (n : ℕ) :
70-
↑(treesOfNumNodesEq n) = { x : Tree Unit | x.numNodes = n } :=
76+
↑(treesOfNumNodesEq n) = { x : BinaryTree Unit | x.numNodes = n } :=
7177
Set.ext (by simp)
7278

7379
theorem treesOfNumNodesEq_card_eq_catalan (n : ℕ) : #(treesOfNumNodesEq n) = catalan n := by
@@ -81,4 +87,4 @@ theorem treesOfNumNodesEq_card_eq_catalan (n : ℕ) : #(treesOfNumNodesEq n) = c
8187
· simp_rw [Set.PairwiseDisjoint, Set.Pairwise, disjoint_left]
8288
aesop
8389

84-
end Tree
90+
end BinaryTree

Mathlib/Combinatorics/Enumerative/DyckWord.lean

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,9 @@ lemma strictMono_semilength : StrictMono semilength := fun p q pq ↦ by
474474

475475
end Order
476476

477-
section Tree
477+
section BinaryTree
478478

479-
open Tree
479+
open BinaryTree
480480

481481
/-- Convert a Dyck word to a binary rooted tree.
482482
@@ -485,7 +485,7 @@ which has index `p.firstReturn`, then let `x` be everything strictly between sai
485485
and `y` be everything strictly after said `D`. `p = x.nest + y` with `x, y` (possibly empty)
486486
Dyck words. `f(p) = f(x) △ f(y)`, where △ (defined in `Mathlib/Data/Tree/Basic.lean`) joins two
487487
subtrees to a new root node. -/
488-
def toTree (p : DyckWord) : Tree Unit :=
488+
def toTree (p : DyckWord) : BinaryTree Unit :=
489489
if p = 0 then nil else p.insidePart.toTree △ p.outsidePart.toTree
490490
termination_by p.semilength
491491
decreasing_by exacts [semilength_insidePart_lt ‹_›, semilength_outsidePart_lt ‹_›]
@@ -494,9 +494,9 @@ decreasing_by exacts [semilength_insidePart_lt ‹_›, semilength_outsidePart_l
494494
495495
`g(nil) = 0`. A nonempty tree with left subtree `l` and right subtree `r`
496496
is sent to `g(l).nest + g(r)`. -/
497-
def ofTree : Tree Unit → DyckWord
498-
| Tree.nil => 0
499-
| Tree.node _ l r => (ofTree l).nest + ofTree r
497+
def ofTree : BinaryTree Unit → DyckWord
498+
| BinaryTree.nil => 0
499+
| BinaryTree.node _ l r => (ofTree l).nest + ofTree r
500500

501501
lemma ofTree_toTree (p) : ofTree p.toTree = p := by
502502
by_cases h : p = 0
@@ -509,11 +509,11 @@ termination_by p.semilength
509509
decreasing_by exacts [semilength_insidePart_lt h, semilength_outsidePart_lt h]
510510

511511
lemma toTree_ofTree : ∀ t, (ofTree t).toTree = t
512-
| Tree.nil => by simp [ofTree, toTree]
513-
| Tree.node _ _ _ => by simp [ofTree, toTree, toTree_ofTree]
512+
| BinaryTree.nil => by simp [ofTree, toTree]
513+
| BinaryTree.node _ _ _ => by simp [ofTree, toTree, toTree_ofTree]
514514

515515
/-- Equivalence between Dyck words and rooted binary trees. -/
516-
@[simps] def equivTree : DyckWord ≃ Tree Unit where
516+
@[simps] def equivTree : DyckWord ≃ BinaryTree Unit where
517517
toFun := toTree
518518
invFun := ofTree
519519
left_inv := ofTree_toTree
@@ -547,7 +547,7 @@ theorem card_dyckWord_semilength_eq_catalan (n : ℕ) :
547547
rw [← Fintype.ofEquiv_card (equivTreesOfNumNodesEq n), ← treesOfNumNodesEq_card_eq_catalan]
548548
convert! Fintype.card_coe _
549549

550-
end Tree
550+
end BinaryTree
551551

552552
end DyckWord
553553

Mathlib/Data/Tree/Basic.lean

Lines changed: 92 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module
77

88
public import Mathlib.Data.Nat.Notation
99
public import Mathlib.Util.CompileInductive
10+
import Batteries.Tactic.Alias
1011

1112
/-!
1213
# Binary tree
@@ -15,8 +16,8 @@ Provides binary tree storage for values of any type, with O(lg n) retrieval.
1516
See also `Lean.Data.RBTree` for red-black trees - this version allows more operations
1617
to be defined and is better suited for in-kernel computation.
1718
18-
We also specialize for `Tree Unit`, which is a binary tree without any
19-
additional data. We provide the notation `a △ b` for making a `Tree Unit` with children
19+
We also specialize for `BinaryTree Unit`, which is a binary tree without any
20+
additional data. We provide the notation `a △ b` for making a `BinaryTree Unit` with children
2021
`a` and `b`.
2122
2223
## References
@@ -28,48 +29,79 @@ additional data. We provide the notation `a △ b` for making a `Tree Unit` with
2829

2930

3031
/-- A binary tree with values stored in non-leaf nodes. -/
31-
inductive Tree.{u} (α : Type u) : Type u
32-
| nil : Tree α
33-
| node (value : α) (left : Tree α) (right : Tree α) : Tree α
32+
inductive BinaryTree.{u} (α : Type u) : Type u
33+
| nil : BinaryTree α
34+
| node (value : α) (left : BinaryTree α) (right : BinaryTree α) : BinaryTree α
3435
deriving DecidableEq, Repr
35-
compile_inductive% Tree
36+
compile_inductive% BinaryTree
3637

37-
namespace Tree
38+
@[deprecated (since := "2026-06-07"), reducible]
39+
alias Tree := BinaryTree
40+
41+
set_option linter.deprecated false in
42+
/-- **Alias** of `BinaryTree.nil`. -/
43+
@[deprecated BinaryTree.nil (since := "2026-06-07")]
44+
abbrev Tree.nil.{u} {α : Type u} : Tree α := BinaryTree.nil
45+
46+
set_option linter.deprecated false in
47+
/-- **Alias** of `BinaryTree.node`. -/
48+
@[deprecated BinaryTree.node (since := "2026-06-07")]
49+
abbrev Tree.node.{u} {α : Type u}
50+
(value : α) (left : Tree α) (right : Tree α) : Tree α :=
51+
BinaryTree.node value left right
52+
53+
namespace BinaryTree
3854

3955
universe u
4056

4157
variable {α : Type u}
4258

43-
instance : Inhabited (Tree α) :=
59+
instance : Inhabited (BinaryTree α) :=
4460
⟨nil⟩
4561

4662
/--
4763
Do an action for every node of the tree.
4864
Actions are taken in node -> left subtree -> right subtree recursive order.
49-
This function is the `traverse` function for the `Traversable Tree` instance.
65+
This function is the `traverse` function for the `Traversable BinaryTree` instance.
5066
-/
51-
def traverse {m : Type* → Type*} [Applicative m] {α β} (f : α → m β) : Tree α → m (Tree β)
67+
def traverse
68+
{m : Type* → Type*} [Applicative m] {α β} (f : α → m β) :
69+
BinaryTree α → m (BinaryTree β)
5270
| .nil => pure nil
5371
| .node a l r => .node <$> f a <*> traverse f l <*> traverse f r
5472

55-
/-- Apply a function to each value in the tree. This is the `map` function for the `Tree` functor.
73+
set_option linter.deprecated false in
74+
/-- **Alias** of `BinaryTree.traverse`. -/
75+
@[deprecated BinaryTree.traverse (since := "2026-06-07")]
76+
abbrev _root_.Tree.traverse {m : Type* → Type*} [Applicative m] {α β} (f : α → m β)
77+
(t : Tree α) : m (Tree β) :=
78+
BinaryTree.traverse f t
79+
80+
/-- Apply a function to each value in the BinaryTree.
81+
This is the `map` function for the `BinaryTree` functor.
5682
-/
57-
def map {β} (f : α → β) : Tree α → Tree β
83+
def map {β} (f : α → β) : BinaryTree α → BinaryTree β
5884
| nil => nil
5985
| node a l r => node (f a) (map f l) (map f r)
6086

61-
theorem id_map (t : Tree α) : t.map id = t := by
87+
set_option linter.deprecated false in
88+
/-- **Alias** of `BinaryTree.map`. -/
89+
@[deprecated BinaryTree.map (since := "2026-06-07")]
90+
abbrev _root_.Tree.map {α β} (f : α → β) (t : Tree α) : Tree β := BinaryTree.map f t
91+
92+
theorem id_map (t : BinaryTree α) : t.map id = t := by
6293
induction t with
6394
| nil => rw [map]
6495
| node v l r hl hr => rw [map, hl, hr, id_eq]
6596

66-
theorem comp_map {β γ : Type*} (f : α → β) (g : β → γ) (t : Tree α) :
97+
theorem comp_map {β γ : Type*} (f : α → β) (g : β → γ) (t : BinaryTree α) :
6798
t.map (g ∘ f) = (t.map f).map g := by
6899
induction t with
69100
| nil => rw [map, map, map]
70101
| node v l r hl hr => rw [map, map, map, hl, hr, Function.comp_apply]
71102

72-
theorem traverse_pure (t : Tree α) {m : Type u → Type*} [Applicative m] [LawfulApplicative m] :
103+
theorem traverse_pure (t : BinaryTree α) {m : Type u → Type*}
104+
[Applicative m] [LawfulApplicative m] :
73105
t.traverse (pure : α → m α) = pure t := by
74106
induction t with
75107
| nil => rw [traverse]
@@ -78,58 +110,90 @@ theorem traverse_pure (t : Tree α) {m : Type u → Type*} [Applicative m] [Lawf
78110

79111
/-- The number of internal nodes (i.e. not including leaves) of a binary tree -/
80112
@[simp]
81-
def numNodes : Tree α → ℕ
113+
def numNodes : BinaryTree α → ℕ
82114
| nil => 0
83115
| node _ a b => a.numNodes + b.numNodes + 1
84116

117+
set_option linter.deprecated false in
118+
/-- **Alias** of `BinaryTree.numNodes`. -/
119+
@[deprecated BinaryTree.numNodes (since := "2026-06-07")]
120+
abbrev _root_.Tree.numNodes {α} (t : Tree α) : ℕ := BinaryTree.numNodes t
121+
85122
/-- The number of leaves of a binary tree -/
86123
@[simp]
87-
def numLeaves : Tree α → ℕ
124+
def numLeaves : BinaryTree α → ℕ
88125
| nil => 1
89126
| node _ a b => a.numLeaves + b.numLeaves
90127

128+
set_option linter.deprecated false in
129+
/-- **Alias** of `BinaryTree.numLeaves`. -/
130+
@[deprecated BinaryTree.numLeaves (since := "2026-06-07")]
131+
abbrev _root_.Tree.numLeaves {α} (t : Tree α) : ℕ := BinaryTree.numLeaves t
132+
91133
/-- The height - length of the longest path from the root - of a binary tree -/
92134
@[simp]
93-
def height : Tree α → ℕ
135+
def height : BinaryTree α → ℕ
94136
| nil => 0
95137
| node _ a b => max a.height b.height + 1
96138

97-
theorem numLeaves_eq_numNodes_succ (x : Tree α) : x.numLeaves = x.numNodes + 1 := by
139+
set_option linter.deprecated false in
140+
/-- **Alias** of `BinaryTree.height`. -/
141+
@[deprecated BinaryTree.height (since := "2026-06-07")]
142+
abbrev _root_.Tree.height {α} (t : Tree α) : ℕ := BinaryTree.height t
143+
144+
theorem numLeaves_eq_numNodes_succ (x : BinaryTree α) : x.numLeaves = x.numNodes + 1 := by
98145
induction x <;> simp [*, Nat.add_comm, Nat.add_assoc, Nat.add_left_comm]
99146

100-
theorem numLeaves_pos (x : Tree α) : 0 < x.numLeaves := by
147+
theorem numLeaves_pos (x : BinaryTree α) : 0 < x.numLeaves := by
101148
rw [numLeaves_eq_numNodes_succ]
102149
exact x.numNodes.zero_lt_succ
103150

104-
theorem height_le_numNodes : ∀ x : Tree α, x.height ≤ x.numNodes
151+
theorem height_le_numNodes : ∀ x : BinaryTree α, x.height ≤ x.numNodes
105152
| nil => Nat.le_refl _
106153
| node _ a b => Nat.succ_le_succ <|
107154
Nat.max_le.2 ⟨Nat.le_trans a.height_le_numNodes <| a.numNodes.le_add_right _,
108155
Nat.le_trans b.height_le_numNodes <| b.numNodes.le_add_left _⟩
109156

110157
/-- The left child of the tree, or `nil` if the tree is `nil` -/
111158
@[simp]
112-
def left : Tree α → Tree α
159+
def left : BinaryTree α → BinaryTree α
113160
| nil => nil
114161
| node _ l _r => l
115162

163+
set_option linter.deprecated false in
164+
/-- **Alias** of `BinaryTree.left`. -/
165+
@[deprecated BinaryTree.left (since := "2026-06-07")]
166+
abbrev _root_.Tree.left {α} (t : Tree α) : Tree α := BinaryTree.left t
167+
116168
/-- The right child of the tree, or `nil` if the tree is `nil` -/
117169
@[simp]
118-
def right : Tree α → Tree α
170+
def right : BinaryTree α → BinaryTree α
119171
| nil => nil
120172
| node _ _l r => r
121173

174+
set_option linter.deprecated false in
175+
/-- **Alias** of `BinaryTree.right`. -/
176+
@[deprecated BinaryTree.right (since := "2026-06-07")]
177+
abbrev _root_.Tree.right {α} (t : Tree α) : Tree α := BinaryTree.right t
178+
122179
/-- A node with `Unit` data -/
123-
scoped infixr:65 " △ " => Tree.node ()
180+
scoped infixr:65 " △ " => BinaryTree.node ()
124181

125-
/-- Induction principle for `Tree Unit`s -/
182+
/-- Induction principle for `BinaryTree Unit`s -/
126183
@[elab_as_elim]
127-
def unitRecOn {motive : Tree Unit → Sort*} (t : Tree Unit) (base : motive nil)
184+
def unitRecOn {motive : BinaryTree Unit → Sort*} (t : BinaryTree Unit) (base : motive nil)
128185
(ind : ∀ x y, motive x → motive y → motive (x △ y)) : motive t :=
129186
t.recOn base fun _u ↦ ind
130187

131-
theorem left_node_right_eq_self : ∀ {x : Tree Unit} (_hx : x ≠ nil), x.left △ x.right = x
188+
set_option linter.deprecated false in
189+
/-- **Alias** of `BinaryTree.unitRecOn`. -/
190+
@[deprecated BinaryTree.unitRecOn (since := "2026-06-07")]
191+
abbrev _root_.Tree.unitRecOn {motive : Tree Unit → Sort*} (t : Tree Unit) (base : motive nil)
192+
(ind : ∀ x y, motive x → motive y → motive (x △ y)) : motive t :=
193+
BinaryTree.unitRecOn t base ind
194+
195+
theorem left_node_right_eq_self : ∀ {x : BinaryTree Unit} (_hx : x ≠ nil), x.left △ x.right = x
132196
| nil, h => by trivial
133197
| node _ _ _, _ => rfl -- Porting note: `a △ b` no longer works in pattern matching
134198

135-
end Tree
199+
end BinaryTree

Mathlib/Data/Tree/Get.lean

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,54 @@ These definitions were moved from the main file to avoid a dependency on `Num`.
2222

2323
@[expose] public section
2424

25-
namespace Tree
25+
namespace BinaryTree
2626

2727
variable {α : Type*}
2828

2929
/-- Finds the index of an element in the tree assuming the tree has been
3030
constructed according to the provided decidable order on its elements.
3131
If it hasn't, the result will be incorrect. If it has, but the element
3232
is not in the tree, returns none. -/
33-
def indexOf (lt : α → α → Prop) [DecidableRel lt] (x : α) : Tree α → Option PosNum
33+
def indexOf (lt : α → α → Prop) [DecidableRel lt] (x : α) : BinaryTree α → Option PosNum
3434
| nil => none
3535
| node a t₁ t₂ =>
3636
match cmpUsing lt x a with
3737
| Ordering.lt => PosNum.bit0 <$> indexOf lt x t₁
3838
| Ordering.eq => some PosNum.one
3939
| Ordering.gt => PosNum.bit1 <$> indexOf lt x t₂
4040

41+
set_option linter.deprecated false in
42+
/-- **Alias** of `BinaryTree.indexOf`. -/
43+
@[deprecated BinaryTree.indexOf (since := "2026-06-07")]
44+
abbrev _root_.Tree.indexOf (lt : α → α → Prop) [DecidableRel lt] (x : α) : Tree α → Option PosNum :=
45+
BinaryTree.indexOf lt x
46+
4147
/-- Retrieves an element uniquely determined by a `PosNum` from the tree,
4248
taking the following path to get to the element:
4349
- `bit0` - go to left child
4450
- `bit1` - go to right child
4551
- `PosNum.one` - retrieve from here -/
46-
def get : PosNum → Tree α → Option α
52+
def get : PosNum → BinaryTree α → Option α
4753
| _, nil => none
4854
| PosNum.one, node a _t₁ _t₂ => some a
4955
| PosNum.bit0 n, node _a t₁ _t₂ => t₁.get n
5056
| PosNum.bit1 n, node _a _t₁ t₂ => t₂.get n
5157

58+
set_option linter.deprecated false in
59+
/-- **Alias** of `BinaryTree.get`. -/
60+
@[deprecated BinaryTree.get (since := "2026-06-07")]
61+
abbrev _root_.Tree.get (n : PosNum) (t : Tree α) : Option α :=
62+
BinaryTree.get n t
63+
5264
/-- Retrieves an element from the tree, or the provided default value
53-
if the index is invalid. See `Tree.get`. -/
54-
def getOrElse (n : PosNum) (t : Tree α) (v : α) : α :=
65+
if the index is invalid. See `BinaryTree.get`. -/
66+
def getOrElse (n : PosNum) (t : BinaryTree α) (v : α) : α :=
5567
(t.get n).getD v
5668

57-
end Tree
69+
set_option linter.deprecated false in
70+
/-- **Alias** of `BinaryTree.getOrElse`. -/
71+
@[deprecated BinaryTree.getOrElse (since := "2026-06-07")]
72+
abbrev _root_.Tree.getOrElse (n : PosNum) (t : Tree α) (v : α) : α :=
73+
BinaryTree.getOrElse n t v
74+
75+
end BinaryTree

0 commit comments

Comments
 (0)