Binary search tree
DefinitionMembership in a binary tree#✓fully proved
A key \(x\) belongs to a binary tree \(T\) when it is the key of some node of \(T\): no key belongs to the empty tree, and \(x\) belongs to a node with key \(k\), left subtree \(L\) and right subtree \(R\) exactly when \(x\) belongs to \(L\), \(x = k\), or \(x\) belongs to \(R\).
This membership does not use the search-tree ordering.
public def Mem {α : Type u} (x : α) : BinaryTree α → Prop
| .nil => False
| .node key left right => Mem x left ∨ x = key ∨ Mem x right
Used by 61
DefinitionInorder traversal of a binary tree with an accumulator#✓fully proved
For a binary tree \(t\) and a list \(s\), the accumulated traversal of the empty tree onto \(s\) is \(s\), and the accumulated traversal of a node with key \(k\), left subtree \(L\) and right subtree \(R\) onto \(s\) is the accumulated traversal of \(L\) onto the list consisting of \(k\) followed by the accumulated traversal of \(R\) onto \(s\).
Unfolding the recursion, the result is the inorder traversal of \(t\) followed by \(s\).
public def inorderAux {α : Type u} : BinaryTree α → List α → List α
| .nil, suffix => suffix
| .node key left right, suffix =>
inorderAux left (key :: inorderAux right suffix)
Used by 32
DefinitionInorder traversal of a binary tree#✓fully proved
The inorder traversal of a binary tree \(t\) is the accumulated inorder traversal of \(t\) onto the empty list. Equivalently, the empty tree gives the empty list, and a node with key \(k\), left subtree \(L\) and right subtree \(R\) gives the inorder traversal of \(L\), then \(k\), then the inorder traversal of \(R\).
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
public def inorderAux {α : Type u} : BinaryTree α → List α → List α
| .nil, suffix => suffix
| .node key left right, suffix =>
inorderAux left (key :: inorderAux right suffix)
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:50
Uses 1 · Used by 47
DefinitionA predicate holding at every key of a binary tree#✓fully proved
For a predicate \(p\) on a type \(\alpha\), the empty binary tree satisfies "every key satisfies \(p\)", and a node with key \(k\), left subtree \(L\) and right subtree \(R\) satisfies it exactly when \(L\) satisfies it, \(p(k)\) holds and \(R\) satisfies it.
public def All {α : Type u} (p : α → Prop) : BinaryTree α → Prop
| .nil => True
| .node key left right => All p left ∧ p key ∧ All p right
Used by 45
DefinitionOrdered binary trees#✓fully proved
Let \(\alpha\) be a type with a relation \(<\). The empty tree is ordered, and a binary tree with root key \(k\), left subtree \(L\) and right subtree \(R\) is ordered when \(L\) and \(R\) are ordered, every key of \(L\) is less than \(k\), and \(k\) is less than every key of \(R\).
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
public def All {α : Type u} (p : α → Prop) : BinaryTree α → Prop
| .nil => True
| .node key left right => All p left ∧ p key ∧ All p right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:83
Uses 1 · Used by 49
DefinitionSearch in a binary search tree#✓fully proved
For a linearly ordered type \(\alpha\), a key \(x \in \alpha\) and a binary tree \(t\) with keys in \(\alpha\), the search for \(x\) in \(t\) returns a Boolean: false if \(t\) is empty, and if \(t\) is a node with key \(k\), left subtree \(L\) and right subtree \(R\), the search for \(x\) in \(L\) when \(x < k\), the search for \(x\) in \(R\) when \(k < x\), and true otherwise.
Unlike the index of a key in a binary tree, which encodes the root-to-node path as a positive binary number, this search returns only whether the key was found.
public def contains {α : Type u} [LinearOrder α] (x : α) : BinaryTree α → Bool
| .nil => false
| .node key left right =>
if x < key then contains x left
else if key < x then contains x right
else true
DefinitionInsertion into a binary search tree#✓fully proved
For a linearly ordered type \(\alpha\), a key \(x \in \alpha\) and a binary tree \(t\) with keys in \(\alpha\), inserting \(x\) into the empty tree gives the single node with key \(x\) and empty subtrees; inserting \(x\) into a node with key \(k\), left subtree \(L\) and right subtree \(R\) inserts \(x\) into \(L\) when \(x < k\), inserts \(x\) into \(R\) when \(k < x\), and returns the node unchanged when \(x = k\).
public def insert {α : Type u} [LinearOrder α] (x : α) : BinaryTree α → BinaryTree α
| .nil => .node x .nil .nil
| tree@(.node key left right) =>
if x < key then .node key (insert x left) right
else if key < x then .node key left (insert x right)
else tree
Used by 9
DefinitionRank of a key in a binary tree#✓fully proved
For a linearly ordered type \(\alpha\), a key \(x \in \alpha\) and a binary tree \(t\) with keys in \(\alpha\), the rank of \(x\) in \(t\) is the number of positions of the inorder traversal of \(t\) whose entry is strictly less than \(x\).
public def rank {α : Type u} [LinearOrder α] (x : α) (tree : BinaryTree α) : Nat :=
((inorder tree).filter (· < x)).length
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
Uses 1 · Used by 7
DefinitionInorder selection in a binary tree#✓fully proved
For a binary tree \(t\) and a natural number \(i\), selecting index \(i\) in \(t\) returns the entry at zero-based position \(i\) of the inorder traversal of \(t\) when \(i\) is less than the length of that traversal, and returns nothing otherwise.
public def select {α : Type u} (tree : BinaryTree α) (index : Nat) : Option α :=
(inorder tree)[index]?
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
Uses 1 · Used by 7
DefinitionBinary search tree built from a list#✓fully proved
For a linearly ordered type \(\alpha\), the binary tree built from a list \([x_1, \ldots, x_n]\) of elements of \(\alpha\) is obtained from the empty tree by inserting \(x_n\), then \(x_{n-1}\), and so on down to \(x_1\), using binary search tree insertion; the empty list gives the empty tree.
public def fromList {α : Type u} [LinearOrder α] : List α → BinaryTree α
| [] => .nil
| x :: xs => insert x (fromList xs)
public def insert {α : Type u} [LinearOrder α] (x : α) : BinaryTree α → BinaryTree α
| .nil => .node x .nil .nil
| tree@(.node key left right) =>
if x < key then .node key (insert x left) right
else if key < x then .node key left (insert x right)
else tree
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:107
Uses 1 · Used by 3
TheoremA predicate holds at all keys of a binary tree iff it holds at all its members#✓fully proved
For a type \(\alpha\), a predicate \(p\) on \(\alpha\) and a binary tree \(t\) with keys in \(\alpha\), every key of \(t\) satisfies \(p\) in the recursive sense (the empty tree qualifies, and a node qualifies when its left subtree qualifies, its key satisfies \(p\) and its right subtree qualifies) if and only if \(p(x)\) holds for every \(x\) that is a member of \(t\).
public theorem all_iff_forall_mem {α : Type u} {p : α → Prop}
{tree : BinaryTree α} :
All p tree ↔ ∀ x, x ∈ tree → p x
public def All {α : Type u} (p : α → Prop) : BinaryTree α → Prop
| .nil => True
| .node key left right => All p left ∧ p key ∧ All p right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:83
Uses 2 · Used by 21
TheoremMembership in the inorder traversal of a binary tree#✓fully proved
For every binary tree \(T\) and every key \(x\), \(x\) occurs in the inorder traversal of \(T\) if and only if \(x\) lies in \(T\).
public theorem mem_inorder {α : Type u} {x : α} {tree : BinaryTree α} :
x ∈ inorder tree ↔ x ∈ tree
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
Uses 3 · Used by 15
TheoremMembership after insertion into a binary search tree#✓fully proved
Let the keys be linearly ordered. For all keys \(x, y\) and every binary tree \(T\), \(y\) lies in the tree obtained by inserting \(x\) into \(T\) if and only if \(y = x\) or \(y\) lies in \(T\).
public theorem mem_insert {α : Type u} [LinearOrder α] {x y : α}
{tree : BinaryTree α} :
y ∈ insert x tree ↔ y = x ∨ y ∈ tree
public def insert {α : Type u} [LinearOrder α] (x : α) : BinaryTree α → BinaryTree α
| .nil => .node x .nil .nil
| tree@(.node key left right) =>
if x < key then .node key (insert x left) right
else if key < x then .node key left (insert x right)
else tree
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:107
Uses 2 · Used by 1
TheoremBinary search tree insertion preserves order#✓fully proved
Let \(\alpha\) be a linearly ordered type. For every key \(x \in \alpha\) and every ordered binary tree \(T\) over \(\alpha\), the tree obtained by inserting \(x\) into \(T\) is ordered.
public theorem ordered_insert {α : Type u} [LinearOrder α] {x : α}
{tree : BinaryTree α} (hTree : Ordered tree) : Ordered (insert x tree)
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def insert {α : Type u} [LinearOrder α] (x : α) : BinaryTree α → BinaryTree α
| .nil => .node x .nil .nil
| tree@(.node key left right) =>
if x < key then .node key (insert x left) right
else if key < x then .node key left (insert x right)
else tree
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:107
Uses 3 · Used by 2
TheoremSearch in a binary search tree is correct#✓fully proved
For a linearly ordered type \(\alpha\), a key \(x \in \alpha\) and a binary tree \(t\) with keys in \(\alpha\) that is ordered (recursively, every key in the left subtree of a node is strictly smaller than the node's key and every key in its right subtree is strictly larger), the comparison search for \(x\) in \(t\) returns true if and only if \(x\) is a member of \(t\).
public theorem contains_eq_true_iff {α : Type u} [LinearOrder α] {x : α}
{tree : BinaryTree α} (hTree : Ordered tree) :
contains x tree = true ↔ x ∈ tree
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def contains {α : Type u} [LinearOrder α] (x : α) : BinaryTree α → Bool
| .nil => false
| .node key left right =>
if x < key then contains x left
else if key < x then contains x right
else true
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:99
Uses 5 · Used by 3
TheoremThe inorder traversal of an ordered binary tree is strictly increasing#✓fully proved
Let \(\alpha\) be a linearly ordered type. If a binary tree \(T\) over \(\alpha\) is ordered, then in the inorder traversal of \(T\) every key is less than every key that comes after it.
public theorem ordered_inorder {α : Type u} [LinearOrder α]
{tree : BinaryTree α} (hTree : Ordered tree) :
List.Pairwise (· < ·) (inorder tree)
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
Uses 7 · Used by 6
TheoremThe inorder traversal of a binary search tree has no duplicates#✓fully proved
For a linearly ordered type \(\alpha\) and a binary tree \(t\) with keys in \(\alpha\) that is ordered (recursively, every key in the left subtree of a node is strictly smaller than the node's key and every key in its right subtree is strictly larger), the inorder traversal of \(t\) is a list with no repeated entries.
public theorem inorder_nodup {α : Type u} [LinearOrder α]
{tree : BinaryTree α} (hTree : Ordered tree) : (inorder tree).Nodup
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
Uses 3 · Used by 1
TheoremBinary search trees built from a list are ordered#✓fully proved
Let \(\alpha\) be a linearly ordered type. For every list \(xs\) of elements of \(\alpha\), the tree obtained from the empty tree by inserting the elements of \(xs\) one at a time, from the last element to the first, is ordered.
public theorem ordered_fromList {α : Type u} [LinearOrder α] {xs : List α} :
Ordered (fromList xs)
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def fromList {α : Type u} [LinearOrder α] : List α → BinaryTree α
| [] => .nil
| x :: xs => insert x (fromList xs)
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:123
Uses 3
TheoremMembers of the binary search tree built from a list#✓fully proved
For a linearly ordered type \(\alpha\), a key \(x \in \alpha\) and a list \(xs\) of elements of \(\alpha\), \(x\) is a member of the binary tree built from \(xs\) by successive binary search tree insertion if and only if \(x\) occurs in \(xs\).
public theorem mem_fromList {α : Type u} [LinearOrder α] {x : α} {xs : List α} :
x ∈ fromList xs ↔ x ∈ xs
public def fromList {α : Type u} [LinearOrder α] : List α → BinaryTree α
| [] => .nil
| x :: xs => insert x (fromList xs)
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:123
Uses 4 · Used by 1
TheoremThe length of the inorder traversal of a binary tree is its number of nodes#✓fully proved
For every binary tree \(T\), the number of nodes of \(T\) (each node carries one key, and the empty tree has none) equals the length of the inorder traversal of \(T\).
public theorem numNodes_eq_length_inorder {α : Type u} {tree : BinaryTree α} :
tree.numNodes = (inorder tree).length
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
Uses 2 · Used by 5
TheoremInserting a present key into a binary search tree leaves its size unchanged#✓fully proved
Let the keys be linearly ordered, and let \(T\) be an ordered binary tree. If a key \(x\) lies in \(T\), then the tree obtained by inserting \(x\) into \(T\) has the same number of nodes as \(T\).
public theorem numNodes_insert_of_mem {α : Type u} [LinearOrder α] {x : α}
{tree : BinaryTree α} (hTree : Ordered tree) (hx : x ∈ tree) :
(insert x tree).numNodes = tree.numNodes
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def insert {α : Type u} [LinearOrder α] (x : α) : BinaryTree α → BinaryTree α
| .nil => .node x .nil .nil
| tree@(.node key left right) =>
if x < key then .node key (insert x left) right
else if key < x then .node key left (insert x right)
else tree
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:107
TheoremInserting an absent key into a binary search tree adds one node#✓fully proved
Let the keys be linearly ordered. For every binary tree \(T\) and every key \(x\) that does not lie in \(T\), the tree obtained by inserting \(x\) into \(T\) has one more node than \(T\).
public theorem numNodes_insert_of_not_mem {α : Type u} [LinearOrder α] {x : α}
{tree : BinaryTree α} (hx : ¬x ∈ tree) :
(insert x tree).numNodes = tree.numNodes + 1
public def insert {α : Type u} [LinearOrder α] (x : α) : BinaryTree α → BinaryTree α
| .nil => .node x .nil .nil
| tree@(.node key left right) =>
if x < key then .node key (insert x left) right
else if key < x then .node key left (insert x right)
else tree
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:107
Uses 2
TheoremRank counts the smaller inorder entries#✓fully proved
For a linearly ordered type \(\alpha\), a key \(x \in \alpha\) and a binary tree \(t\) with keys in \(\alpha\), the rank of \(x\) in \(t\) equals the length of the list obtained from the inorder traversal of \(t\) by keeping exactly the entries strictly less than \(x\).
public theorem rank_eq_count_inorder {α : Type u} [LinearOrder α] {x : α}
{tree : BinaryTree α} :
rank x tree = ((inorder tree).filter (· < x)).length
public def rank {α : Type u} [LinearOrder α] (x : α) (tree : BinaryTree α) : Nat :=
((inorder tree).filter (· < x)).length
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:115
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
Uses 2
TheoremSelection is indexing into the inorder traversal#✓fully proved
For a binary tree \(t\) and a natural number \(i\), selecting index \(i\) in \(t\) gives the same result as looking up zero-based position \(i\) of the inorder traversal of \(t\): the entry at that position when \(i\) is less than the length of the traversal, and nothing otherwise.
public theorem select_eq_getElem?_inorder {α : Type u} {tree : BinaryTree α}
{index : Nat} :
select tree index = (inorder tree)[index]?
public def select {α : Type u} (tree : BinaryTree α) (index : Nat) : Option α :=
(inorder tree)[index]?
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:119
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
Uses 2
TheoremSelecting the rank of a key returns the key#✓fully proved
Let \(\alpha\) be a linearly ordered type and let \(t\) be a binary search tree with keys in \(\alpha\), that is, at every node each key of the left subtree is strictly smaller and each key of the right subtree is strictly larger than the node's key. If \(x\) is a key of \(t\), then selecting, in \(t\), the index equal to the rank of \(x\) in \(t\) returns \(x\).
public theorem select_rank {α : Type u} [LinearOrder α] {x : α}
{tree : BinaryTree α} (hTree : Ordered tree) (hx : x ∈ tree) :
select tree (rank x tree) = some x
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def select {α : Type u} (tree : BinaryTree α) (index : Nat) : Option α :=
(inorder tree)[index]?
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:119
public def rank {α : Type u} [LinearOrder α] (x : α) (tree : BinaryTree α) : Nat :=
((inorder tree).filter (· < x)).length
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:115
Uses 7 · Used by 1
TheoremThe rank of a selected key is its index#✓fully proved
Let \(\alpha\) be a linearly ordered type and let \(t\) be a binary search tree with keys in \(\alpha\), that is, at every node each key of the left subtree is strictly smaller and each key of the right subtree is strictly larger than the node's key. If selecting index \(i\) in \(t\) returns a key \(x\), then the rank of \(x\) in \(t\) equals \(i\).
public theorem rank_select {α : Type u} [LinearOrder α] {tree : BinaryTree α}
{index : Nat} {x : α} (hTree : Ordered tree)
(hSelect : select tree index = some x) : rank x tree = index
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def select {α : Type u} (tree : BinaryTree α) (index : Nat) : Option α :=
(inorder tree)[index]?
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:119
public def rank {α : Type u} [LinearOrder α] (x : α) (tree : BinaryTree α) : Nat :=
((inorder tree).filter (· < x)).length
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:115
Uses 5 · Used by 1
DefinitionJoin of two binary trees#✓fully proved
The join of the empty tree and a tree \(R\) is \(R\). The join of a tree with root key \(k\), left subtree \(L_1\) and right subtree \(L_2\) and a tree \(R\) is the tree with root key \(k\), left subtree \(L_1\) and right subtree the join of \(L_2\) and \(R\). Thus, when the first tree is nonempty, \(R\) becomes the right subtree of its rightmost node.
In the inorder traversal of the join, every key of the first tree comes before every key of the second tree.
public def join {α : Type u} : BinaryTree α → BinaryTree α → BinaryTree α
| .nil, right => right
| .node key leftLeft leftRight, right =>
.node key leftLeft (join leftRight right)
Used by 9
DefinitionDeletion from a binary search tree#✓fully proved
Let \(x\) be a key of a linearly ordered type. Deleting \(x\) from the empty tree gives the empty tree. Deleting \(x\) from a tree with root key \(k\), left subtree \(L\) and right subtree \(R\) gives the tree with root key \(k\), left subtree \(L\) with \(x\) deleted and right subtree \(R\) if \(x < k\); the tree with root key \(k\), left subtree \(L\) and right subtree \(R\) with \(x\) deleted if \(k < x\); and the join of \(L\) and \(R\) otherwise, that is, if \(x = k\).
public def delete {α : Type u} [LinearOrder α] (x : α) :
BinaryTree α → BinaryTree α
| .nil => .nil
| .node key left right =>
if x < key then .node key (delete x left) right
else if key < x then .node key left (delete x right)
else join left right
public def join {α : Type u} : BinaryTree α → BinaryTree α → BinaryTree α
| .nil, right => right
| .node key leftLeft leftRight, right =>
.node key leftLeft (join leftRight right)
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree/Delete.lean:41
Uses 1 · Used by 5
TheoremMembership in a join#✓fully proved
For binary trees \(L\) and \(R\), a key \(x\) occurs in the join of \(L\) and \(R\) if and only if \(x\) occurs in \(L\) or \(x\) occurs in \(R\).
public theorem mem_join {α : Type u} {x : α} {left right : BinaryTree α} :
x ∈ join left right ↔ x ∈ left ∨ x ∈ right
public def join {α : Type u} : BinaryTree α → BinaryTree α → BinaryTree α
| .nil, right => right
| .node key leftLeft leftRight, right =>
.node key leftLeft (join leftRight right)
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree/Delete.lean:41
Uses 2 · Used by 5
TheoremInorder traversal of a join#✓fully proved
For binary trees \(L\) and \(R\), the inorder traversal of the join of \(L\) and \(R\) is the inorder traversal of \(L\) followed by the inorder traversal of \(R\).
public theorem inorder_join {α : Type u} {left right : BinaryTree α} :
inorder (join left right) = inorder left ++ inorder right
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
public def join {α : Type u} : BinaryTree α → BinaryTree α → BinaryTree α
| .nil, right => right
| .node key leftLeft leftRight, right =>
.node key leftLeft (join leftRight right)
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree/Delete.lean:41
Uses 3 · Used by 1
TheoremJoining separated ordered trees#✓fully proved
Let \(L\) and \(R\) be ordered binary search trees over a type with a relation \(<\). If \(a < b\) for every key \(a\) of \(L\) and every key \(b\) of \(R\), then the join of \(L\) and \(R\) is ordered.
public theorem ordered_join {α : Type u} [LT α] {left right : BinaryTree α}
(hLeft : Ordered left) (hRight : Ordered right)
(hSeparated : ∀ a ∈ left, ∀ b ∈ right, a < b) :
Ordered (join left right)
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def join {α : Type u} : BinaryTree α → BinaryTree α → BinaryTree α
| .nil, right => right
| .node key leftLeft leftRight, right =>
.node key leftLeft (join leftRight right)
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree/Delete.lean:41
Uses 6 · Used by 1
TheoremDeletion keeps the other keys#✓fully proved
Let \(t\) be a binary tree over a linearly ordered type, not necessarily ordered, and let \(x\) and \(y\) be keys with \(y \ne x\). If \(y\) occurs in \(t\), then \(y\) occurs in the tree obtained by deleting \(x\) from \(t\).
public theorem mem_delete_of_mem {α : Type u} [LinearOrder α] {x y : α}
{tree : BinaryTree α} (hy : y ∈ tree) (hxy : y ≠ x) :
y ∈ delete x tree
public def delete {α : Type u} [LinearOrder α] (x : α) :
BinaryTree α → BinaryTree α
| .nil => .nil
| .node key left right =>
if x < key then .node key (delete x left) right
else if key < x then .node key left (delete x right)
else join left right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree/Delete.lean:47
Uses 4 · Used by 1
TheoremThe deleted key is gone#✓fully proved
If \(t\) is an ordered binary search tree over a linearly ordered type and \(x\) is a key, then \(x\) does not occur in the tree obtained by deleting \(x\) from \(t\).
public theorem not_mem_delete_self {α : Type u} [LinearOrder α] {x : α}
{tree : BinaryTree α} (hTree : Ordered tree) : x ∉ delete x tree
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def delete {α : Type u} [LinearOrder α] (x : α) :
BinaryTree α → BinaryTree α
| .nil => .nil
| .node key left right =>
if x < key then .node key (delete x left) right
else if key < x then .node key left (delete x right)
else join left right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree/Delete.lean:47
Uses 7 · Used by 1
TheoremMembership after deletion#✓fully proved
If \(t\) is an ordered binary search tree over a linearly ordered type, then for all keys \(x\) and \(y\), the key \(y\) occurs in the tree obtained by deleting \(x\) from \(t\) if and only if \(y\) occurs in \(t\) and \(y \ne x\).
public theorem mem_delete {α : Type u} [LinearOrder α] {x y : α}
{tree : BinaryTree α} (hTree : Ordered tree) :
y ∈ delete x tree ↔ y ∈ tree ∧ y ≠ x
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def delete {α : Type u} [LinearOrder α] (x : α) :
BinaryTree α → BinaryTree α
| .nil => .nil
| .node key left right =>
if x < key then .node key (delete x left) right
else if key < x then .node key left (delete x right)
else join left right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree/Delete.lean:47
Uses 7
TheoremDeletion preserves the search-tree order#✓fully proved
If \(t\) is an ordered binary search tree over a linearly ordered type, then for every key \(x\) the tree obtained by deleting \(x\) from \(t\) is ordered.
public theorem ordered_delete {α : Type u} [LinearOrder α] {x : α}
{tree : BinaryTree α} (hTree : Ordered tree) :
Ordered (delete x tree)
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def delete {α : Type u} [LinearOrder α] (x : α) :
BinaryTree α → BinaryTree α
| .nil => .nil
| .node key left right =>
if x < key then .node key (delete x left) right
else if key < x then .node key left (delete x right)
else join left right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree/Delete.lean:47
Uses 8
TheoremInorder traversal after deletion#✓fully proved
If \(t\) is an ordered binary search tree over a linearly ordered type and \(x\) is a key, then the inorder traversal of the tree obtained by deleting \(x\) from \(t\) is the inorder traversal of \(t\) with the first occurrence of \(x\) removed (and is the inorder traversal of \(t\) itself if \(x\) does not occur in it).
public theorem inorder_delete {α : Type u} [LinearOrder α] {x : α}
{tree : BinaryTree α} (hTree : Ordered tree) :
inorder (delete x tree) = (inorder tree).erase x
public def Ordered {α : Type u} [LT α] : BinaryTree α → Prop
| .nil => True
| .node key left right =>
Ordered left ∧ Ordered right ∧ All (· < key) left ∧ All (key < ·) right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:88
public def inorder {α : Type u} (tree : BinaryTree α) : List α :=
inorderAux tree []
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree.lean:68
public def delete {α : Type u} [LinearOrder α] (x : α) :
BinaryTree α → BinaryTree α
| .nil => .nil
| .node key left right =>
if x < key then .node key (delete x left) right
else if key < x then .node key left (delete x right)
else join left right
CSLibExt/Algorithms/Lean/DataStructures/BinarySearchTree/Delete.lean:47
Uses 10
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.