# 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 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 #

inductive Tree (α : Type u) :

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

Instances For
instance instDecidableEqTree :
{α : Type u_1} → [inst : ] → DecidableEq (Tree α)
Equations
• instDecidableEqTree = decEqTree✝
instance instReprTree :
{α : Type u_1} → [inst : Repr α] → Repr (Tree α)
Equations
• instReprTree = { reprPrec := reprTree✝ }
instance Tree.instInhabitedTree {α : Type u} :
Equations
• Tree.instInhabitedTree = { default := Tree.nil }
def Tree.ofRBNode {α : Type u} :
Tree α

Makes a Tree α out of a red-black tree.

Equations
Instances For
def Tree.indexOf {α : Type u} (lt : ααProp) [] (x : α) :
Tree α

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
• One or more equations did not get rendered due to their size.
• Tree.indexOf lt x Tree.nil = none
Instances For
def Tree.get {α : Type u} :
PosNumTree α

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

• bit0 - go to left child
• bit1 - go to right child
• PosNum.one - retrieve from here
Equations
Instances For
def Tree.getOrElse {α : Type u} (n : PosNum) (t : Tree α) (v : α) :
α

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

Equations
Instances For
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
Instances For
def Tree.numNodes {α : Type u} :
Tree α

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

Equations
Instances For
def Tree.numLeaves {α : Type u} :
Tree α

The number of leaves of a binary tree

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

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

Equations
Instances For
theorem Tree.numLeaves_eq_numNodes_succ {α : Type u} (x : Tree α) :
theorem Tree.numLeaves_pos {α : Type u} (x : Tree α) :
theorem Tree.height_le_numNodes {α : Type u} (x : Tree α) :
def Tree.left {α : Type u} :
Tree αTree α

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

Equations
• = match x with | Tree.nil => Tree.nil | Tree.node a l _r => l
Instances For
def Tree.right {α : Type u} :
Tree αTree α

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

Equations
• = match x with | Tree.nil => Tree.nil | Tree.node a _l r => r
Instances For

A node with Unit data

Equations
Instances For
def Tree.unitRecOn {motive : Sort u_1} (t : ) (base : motive Tree.nil) (ind : (x y : ) → motive xmotive ymotive ()) :
motive t
Equations
Instances For
theorem Tree.left_node_right_eq_self {x : } (_hx : x Tree.nil) :
Tree.node () () () = x