Documentation

Mathlib.Data.Tree.Basic

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

inductive BinaryTree (α : Type u) :

A binary tree with values stored in non-leaf nodes.

Instances For
    def instDecidableEqBinaryTree.decEq {α✝ : Type u_1} [DecidableEq α✝] (x✝ x✝¹ : BinaryTree α✝) :
    Decidable (x✝ = x✝¹)
    Equations
    Instances For
      @[implicit_reducible]
      instance instReprBinaryTree {α✝ : Type u_1} [Repr α✝] :
      Equations
      def instReprBinaryTree.repr {α✝ : Type u_1} [Repr α✝] :
      BinaryTree α✝Std.Format
      Equations
      Instances For
        @[reducible, deprecated BinaryTree (since := "2026-06-07")]
        def Tree (α : Type u) :

        Alias of BinaryTree.


        A binary tree with values stored in non-leaf nodes.

        Equations
        Instances For
          @[reducible, inline, deprecated BinaryTree.nil (since := "2026-06-07")]
          abbrev Tree.nil {α : Type u} :
          Tree α

          Alias of BinaryTree.nil.

          Equations
          Instances For
            @[reducible, inline, deprecated BinaryTree.node (since := "2026-06-07")]
            abbrev Tree.node {α : Type u} (value : α) (left right : Tree α) :
            Tree α

            Alias of BinaryTree.node.

            Equations
            Instances For
              @[implicit_reducible]
              Equations
              def BinaryTree.traverse {m : Type u_1 → Type u_2} [Applicative m] {α : Type u_3} {β : Type u_1} (f : α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.

              Equations
              Instances For
                @[reducible, inline, deprecated BinaryTree.traverse (since := "2026-06-07")]
                abbrev Tree.traverse {m : Type u_1 → Type u_2} [Applicative m] {α : Type u_3} {β : Type u_1} (f : αm β) (t : Tree α) :
                m (Tree β)

                Alias of BinaryTree.traverse.

                Equations
                Instances For
                  def BinaryTree.map {α : Type u} {β : Type u_1} (f : αβ) :

                  Apply a function to each value in the BinaryTree. This is the map function for the BinaryTree functor.

                  Equations
                  Instances For
                    @[reducible, inline, deprecated BinaryTree.map (since := "2026-06-07")]
                    abbrev Tree.map {α : Type u_1} {β : Type u_2} (f : αβ) (t : Tree α) :
                    Tree β

                    Alias of BinaryTree.map.

                    Equations
                    Instances For
                      theorem BinaryTree.id_map {α : Type u} (t : BinaryTree α) :
                      map id t = t
                      theorem BinaryTree.comp_map {α : Type u} {β : Type u_1} {γ : Type u_2} (f : αβ) (g : βγ) (t : BinaryTree α) :
                      map (g f) t = map g (map f t)
                      theorem BinaryTree.traverse_pure {α : Type u} (t : BinaryTree α) {m : Type u → Type u_1} [Applicative m] [LawfulApplicative m] :
                      def BinaryTree.numNodes {α : Type u} :

                      The number of internal nodes (i.e. not including leaves) of a binary tree

                      Equations
                      Instances For
                        @[reducible, inline, deprecated BinaryTree.numNodes (since := "2026-06-07")]
                        abbrev Tree.numNodes {α : Type u_1} (t : Tree α) :

                        Alias of BinaryTree.numNodes.

                        Equations
                        Instances For

                          The number of leaves of a binary tree

                          Equations
                          Instances For
                            @[reducible, inline, deprecated BinaryTree.numLeaves (since := "2026-06-07")]
                            abbrev Tree.numLeaves {α : Type u_1} (t : Tree α) :

                            Alias of BinaryTree.numLeaves.

                            Equations
                            Instances For
                              def BinaryTree.height {α : Type u} :

                              The height - length of the longest path from the root - of a binary tree

                              Equations
                              Instances For
                                @[reducible, inline, deprecated BinaryTree.height (since := "2026-06-07")]
                                abbrev Tree.height {α : Type u_1} (t : Tree α) :

                                Alias of BinaryTree.height.

                                Equations
                                Instances For
                                  def BinaryTree.left {α : Type u} :

                                  The left child of the tree, or nil if the tree is nil

                                  Equations
                                  Instances For
                                    @[reducible, inline, deprecated BinaryTree.left (since := "2026-06-07")]
                                    abbrev Tree.left {α : Type u_1} (t : Tree α) :
                                    Tree α

                                    Alias of BinaryTree.left.

                                    Equations
                                    Instances For
                                      def BinaryTree.right {α : Type u} :

                                      The right child of the tree, or nil if the tree is nil

                                      Equations
                                      Instances For
                                        @[reducible, inline, deprecated BinaryTree.right (since := "2026-06-07")]
                                        abbrev Tree.right {α : Type u_1} (t : Tree α) :
                                        Tree α

                                        Alias of BinaryTree.right.

                                        Equations
                                        Instances For
                                          def BinaryTree.unitRecOn {motive : BinaryTree UnitSort u_1} (t : BinaryTree Unit) (base : motive nil) (ind : (x y : BinaryTree Unit) → motive xmotive ymotive (node () x y)) :
                                          motive t

                                          Induction principle for BinaryTree Units

                                          Equations
                                          Instances For
                                            @[reducible, inline, deprecated BinaryTree.unitRecOn (since := "2026-06-07")]
                                            abbrev Tree.unitRecOn {motive : Tree UnitSort u_1} (t : Tree Unit) (base : motive BinaryTree.nil) (ind : (x y : Tree Unit) → motive xmotive ymotive (BinaryTree.node () x y)) :
                                            motive t

                                            Alias of BinaryTree.unitRecOn.

                                            Equations
                                            Instances For