main shrub/kav / lib / tree.kav
  1use <prelude.kav>
  2use <list.kav> as list
  3use <bool.kav> as bool
  4use <int.kav> as int
  5
  6/%
  7A linear, size-indexed binary tree.
  8%/
  9pub linear datatype Tree[a: type, n: nat] {
 10    | Leaf: Tree[a, 0]
 11    | [n:nat, m:nat] Node(a, Tree[a, n], Tree[a, m]): Tree[a, m + n + 1]
 12}
 13
 14/%
 15Deallocates a tree.
 16%/
 17pub fn free[a: utype, n: nat](tree: Tree[a, n]) -> one {
 18    case tree of {
 19        | ~Leaf => one
 20        | ~Node(_, l, r) => {
 21            val _ = free(l)
 22            val _ = free(r)
 23            one
 24        }
 25    }
 26}
 27
 28/%
 29Returns the number of nodes in a tree.
 30%/
 31pub fn size[a: type, n: nat](tree: !Tree[a, n]) -> int {
 32    case tree of {
 33        | Leaf => 0
 34        | Node(_, l, r) => 1 + size(l) + size(r)
 35    }
 36}
 37
 38/%
 39Returns the height of a tree.
 40%/
 41pub fn height[a: type, n: nat](tree: !Tree[a, n]) -> int {
 42    case tree of {
 43        | Leaf => 0
 44        | Node(_, l, r) => 1 + int::max(height(l), height(r))
 45    }
 46}
 47
 48/%
 49Mirrors a tree by swapping the left and right subtree of every node.
 50%/
 51pub fn mirror[a: type, n: nat](tree: Tree[a, n]) -> Tree[a, n] {
 52    case tree of {
 53        | ~Leaf => Leaf
 54        | ~Node(x, l, r) => Node(x, mirror(r), mirror(l))
 55    }
 56}
 57
 58/%
 59Returns the elements of a tree in pre-order.
 60%/
 61pub fn preorder_traversal[a: utype, n: nat](tree: !Tree[a, n]) -> list::List[a, n] {
 62    case tree of {
 63        | Leaf => list::Nil
 64        | Node(x, l, r) =>
 65            list::Cons(x, list::append(preorder_traversal(l), preorder_traversal(r)))
 66    }
 67}
 68
 69/%
 70Returns the elements of a tree in post-order.
 71%/
 72pub fn postorder_traversal[a: utype, n: nat](tree: !Tree[a, n]) -> list::List[a, n] {
 73    case tree of {
 74        | Leaf => list::Nil
 75        | Node(x, l, r) =>
 76            list::append(
 77                list::append(postorder_traversal(l), postorder_traversal(r)),
 78                list::Cons(x, list::Nil))
 79    }
 80}
 81
 82/%
 83Fold over a tree, traversing in in-order.
 84
 85For a tree whose in-order traversal is `[x1, x2, x3]` this computes
 86`f(f(f(init, x1), x2), x3)`.
 87%/
 88pub fn fold[a: utype, b: type, n: nat](tree: !Tree[a, n], init: b, f: fn(b, a) -> b) -> b {
 89    case tree of {
 90        | Leaf => init
 91        | Node(x, l, r) => {
 92            val acc = fold(l, init, f)
 93            fold(r, f(acc, x), f)
 94        }
 95    }
 96}
 97
 98/%
 99Returns the elements of a tree in in-order.
100%/
101pub fn inorder_traversal[a: utype, n: nat](tree: !Tree[a, n]) -> list::List[a, n] {
102    case tree of {
103        | Leaf => list::Nil
104        | Node(x, l, r) => list::append(inorder_traversal(l), list::Cons(x, inorder_traversal(r)))
105    }
106}
107
108/%
109Returns a tree with the same shape, where each element is replaced by the result of applying `f`.
110%/
111pub fn map[a: type, b: type, n: nat](tree: Tree[a, n], f: fn(a) -> b) -> Tree[b, n] {
112    case tree of {
113        | ~Leaf => Leaf
114        | ~Node(x, l, r) => Node(f(x), map(l, f), map(r, f))
115    }
116}
117
118
119/%
120Returns the sum of the elements of an integer tree.
121
122The sum of the empty tree is `0`.
123%/
124pub fn sum[n: nat](tree: !Tree[int, n]) -> int {
125    case tree of {
126        | Leaf => 0
127        | Node(x, l, r) => x + sum(l) + sum(r)
128    }
129}
130
131/%
132Tests whether two integer trees are equal.
133
134Two trees are equal if they have the same shape and their corresponding elements are equal.
135%/
136pub fn equal[n: nat](tree1: !Tree[int, n], tree2: !Tree[int, n]) -> bool {
137    case (tree1, tree2) of {
138        | (Leaf, Leaf) => true
139        | (Node(x1, l1, r1), Node(x2, l2, r2)) => bool::and(x1 == x2, bool::and(equal(l1, l2), equal(r1, r2)))
140    }
141}