77
88public import Mathlib.Data.Nat.Notation
99public 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.
1516See also `Lean.Data.RBTree` for red-black trees - this version allows more operations
1617to 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
3955universe u
4056
4157variable {α : Type u}
4258
43- instance : Inhabited (Tree α) :=
59+ instance : Inhabited (BinaryTree α) :=
4460 ⟨nil⟩
4561
4662/--
4763Do an action for every node of the tree.
4864Actions 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
0 commit comments