跳到內容

tree

本頁內容尚未翻譯。

data Tree (A : Type)

A binary tree with values of A at its nodes.

def size {A : Type} (t : Tree A) : Nat

The number of nodes.

def leafCount {A : Type} (t : Tree A) : Nat

The number of leaves (one more than the nodes).

def depth {A : Type} (t : Tree A) : Nat

The number of nodes on the longest path from the root to a leaf.

def map {A : Type} {B : Type} (f : A -> B) (t : Tree A) : Tree B

f applied to every value, the shape kept.

def mirror {A : Type} (t : Tree A) : Tree A

The subtrees swapped at every node.

def zip {A : Type} {B : Type} (a : Tree A) (b : Tree B) : Tree (A * B)

The pairs of values where both trees have a node.

def fold {A : Type} {B : Type} (atLeaf : B) (f : A -> B -> B -> B) (t : Tree A) : B

The values from the leaves up: atLeaf at each leaf, f x left right at each node.

def inorder {A : Type} (t : Tree A) : List A

The values in order: the left subtree, the node, the right subtree.

def preorder {A : Type} (t : Tree A) : List A

The values with each node before its subtrees.

def postorder {A : Type} (t : Tree A) : List A

The values with each node after its subtrees.

def all {A : Type} (test : A -> Bool) (t : Tree A) : Bool

Whether every value passes the test.

def any {A : Type} (test : A -> Bool) (t : Tree A) : Bool

Whether some value passes the test.

def root {A : Type} (t : Tree A) : Option A

The value at the root, if the tree is a node.

def leftSubtree {A : Type} (t : Tree A) : Tree A

The left subtree of a node; a leaf’s is a leaf.

def rightSubtree {A : Type} (t : Tree A) : Tree A

The right subtree of a node; a leaf’s is a leaf.

def insertLeft {A : Type} (x : A) (t : Tree A) : Tree A

A new node with x at the leftmost leaf.

def insertRight {A : Type} (x : A) (t : Tree A) : Tree A

A new node with x at the rightmost leaf.

def mapIdentity {A : Type} (t : Tree A) : map (fun (x : A) => x) t = t

Mapping the identity changes nothing.

def mapCompose {A : Type} {B : Type} {C : Type} (f : A -> B) (g : B -> C) (t : Tree A) : map g (map f t) = map (fun (x : A) => g (f x)) t

Mapping twice is mapping the composition.

def sizeMap {A : Type} {B : Type} (f : A -> B) (t : Tree A) : size (map f t) = size t

Mapping keeps the size.

def mirrorMirror {A : Type} (t : Tree A) : mirror (mirror t) = t

Mirroring twice gives the tree back.

def sizeMirror {A : Type} (t : Tree A) : size (mirror t) = size t

Mirroring keeps the size.

def depthMirror {A : Type} (t : Tree A) : depth (mirror t) = depth t

Mirroring keeps the depth.

def mirrorMap {A : Type} {B : Type} (f : A -> B) (t : Tree A) : mirror (map f t) = map f (mirror t)

Mirroring and mapping commute.