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 #
https://leanprover-community.github.io/archive/stream/113488-general/topic/tactic.20question.html
Equations
- instDecidableEqTree = decEqTree✝
Makes a Tree α
out of a red-black tree.
Equations
- Tree.ofRBNode Std.RBNode.nil = Tree.nil
- Tree.ofRBNode (Std.RBNode.node _color l key r) = Tree.node key (Tree.ofRBNode l) (Tree.ofRBNode r)
Instances For
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
Retrieves an element uniquely determined by a PosNum
from the tree,
taking the following path to get to the element:
bit0
- go to left childbit1
- go to right childPosNum.one
- retrieve from here
Equations
Instances For
Retrieves an element from the tree, or the provided default value
if the index is invalid. See Tree.get
.
Equations
- Tree.getOrElse n t v = Option.getD (Tree.get n t) v
Instances For
The number of internal nodes (i.e. not including leaves) of a binary tree
Equations
- Tree.numNodes Tree.nil = 0
- Tree.numNodes (Tree.node a a_1 a_2) = Tree.numNodes a_1 + Tree.numNodes a_2 + 1
Instances For
The number of leaves of a binary tree
Equations
- Tree.numLeaves Tree.nil = 1
- Tree.numLeaves (Tree.node a a_1 a_2) = Tree.numLeaves a_1 + Tree.numLeaves a_2
Instances For
The height - length of the longest path from the root - of a binary tree
Equations
- Tree.height Tree.nil = 0
- Tree.height (Tree.node a a_1 a_2) = max (Tree.height a_1) (Tree.height a_2) + 1
Instances For
The right child of the tree, or nil
if the tree is nil
Equations
- Tree.right x = match x with | Tree.nil => Tree.nil | Tree.node a _l r => r
Instances For
Equations
- Tree.«term_△_» = Lean.ParserDescr.trailingNode `Tree.term_△_ 65 66 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " △ ") (Lean.ParserDescr.cat `term 65))
Instances For
Equations
- Tree.unitRecOn t base ind = Tree.recOn t base fun _u => ind