mathlib3 documentation

data.tree

Binary tree #

THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.

Provides binary tree storage for values of any type, with O(lg n) retrieval. See also 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 tree unit, which is a binary tree without any additional data. We provide the notation a △ b for making a tree unit with children a and b.

References #

https://leanprover-community.github.io/archive/stream/113488-general/topic/tactic.20question.html

@[protected, instance]
def tree.decidable_eq (α : Type u) [a : decidable_eq α] :
@[protected, instance]
meta def tree.has_reflect (α : Type) [a : has_reflect α] [a_1 : reflected Type α] :
inductive tree (α : Type u) :

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

Instances for tree
def tree.repr {α : Type u} [has_repr α] :

Construct a string representation of a tree. Provides a has_repr instance.

Equations
@[protected, instance]
def tree.has_repr {α : Type u} [has_repr α] :
Equations
@[protected, instance]
def tree.inhabited {α : Type u} :
Equations
def tree.index_of {α : Type u} (lt : α α Prop) [decidable_rel lt] (x : α) :

Finds the index of an element in the tree assuming the tree has been constructed according to the provided decidable order on its elements. If it hasn't, the result will be incorrect. If it has, but the element is not in the tree, returns none.

Equations
def tree.get {α : Type u} :

Retrieves an element uniquely determined by a pos_num from the tree, taking the following path to get to the element:

Equations
def tree.get_or_else {α : Type u} (n : pos_num) (t : tree α) (v : α) :
α

Retrieves an element from the tree, or the provided default value if the index is invalid. See tree.get.

Equations
def tree.map {α : Type u} {β : Type u_1} (f : α β) :
tree α tree β

Apply a function to each value in the tree. This is the map function for the tree functor. TODO: implement traversable tree.

Equations
@[simp]
def tree.num_nodes {α : Type u} :

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

Equations
@[simp]
def tree.num_leaves {α : Type u} :

The number of leaves of a binary tree

Equations
@[simp]
def tree.height {α : Type u} :

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

Equations
theorem tree.num_leaves_pos {α : Type u} (x : tree α) :
theorem tree.height_le_num_nodes {α : Type u} (x : tree α) :
@[simp]
def tree.left {α : Type u} :
tree α tree α

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

Equations
@[simp]
def tree.right {α : Type u} :
tree α tree α

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

Equations
def tree.unit_rec_on {motive : tree unit Sort u_1} (t : tree unit) (base : motive tree.nil) (ind : Π (x y : tree unit), motive x motive y motive (tree.node unit.star() x y)) :
motive t

Recursion on tree unit; allows for a better induction which does not have to worry about the element of type α = unit

Equations