Forests are binary trees #
A forest, a list of rose trees, is either empty or a first tree followed by a forest, and the
first tree is a value over the forest of its children. A binary tree is either nil or a value
with a left and a right subtree. Reading the left subtree as the children of the first tree and
the right subtree as the rest of the forest identifies the two: this is the natural
correspondence between forests and binary trees of [Knu97], the left-child right-sibling
representation. A rose tree is a value together with the forest of its children, hence a value
together with a binary tree, and rose trees over an encodable type are encodable.
Main declarations #
BinaryTree.ofForest,BinaryTree.toForest: the natural correspondence and its inverse, bundled asBinaryTree.equivForest;BinaryTree.consis the list cons transported along it.RoseTree.equivProdBinaryTree: a rose tree is a value together with a binary tree.RoseTree.encodable,RoseTree.countable: the instances transported along it.
Main results #
BinaryTree.numNodes_ofForest,BinaryTree.numNodes_cons: the correspondence preserves the number of nodes.
The natural correspondence sends a forest to the binary tree obtained by consing its
trees, in order, onto nil.
Equations
- BinaryTree.ofForest [] = BinaryTree.nil
- BinaryTree.ofForest (t :: rest) = BinaryTree.cons t (BinaryTree.ofForest rest)
Instances For
The list cons transported along the natural correspondence makes a tree a node whose left
subtree is the forest of its children and whose right subtree is the rest of the forest.
Equations
- BinaryTree.cons (RoseTree.node a cs) x✝ = BinaryTree.node a (BinaryTree.ofForest cs) x✝
Instances For
The inverse of the natural correspondence BinaryTree.ofForest.
Equations
- BinaryTree.nil.toForest = []
- (BinaryTree.node a l r).toForest = RoseTree.node a l.toForest :: r.toForest
Instances For
The natural correspondence between binary trees and forests.
Equations
- BinaryTree.equivForest = { toFun := BinaryTree.toForest, invFun := BinaryTree.ofForest, left_inv := ⋯, right_inv := ⋯ }
Instances For
A rose tree is its root value together with the binary tree of the forest of its children.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- RoseTree.encodable = Encodable.ofEquiv (α × BinaryTree α) RoseTree.equivProdBinaryTree