// Binary Search Tree example by @SleepyRoy struct Empty {} struct Node { value T left Tree right Tree } type Tree = Empty | Node // return size(number of nodes) of BST fn (tree Tree) size() int { return match tree { Empty { 0 } Node { 1 + tree.left.size() + tree.right.size() } } } // insert a value to BST fn (tree Tree) insert(x T) Tree { return match tree { Empty { Node{x, tree, tree} } Node { if x == tree.value { tree } else if x < tree.value { Node{ ...tree left: tree.left.insert(x) } } else { Node{ ...tree right: tree.right.insert(x) } } } } } // whether able to find a value in BST fn (tree Tree) search(x T) bool { return match tree { Empty { false } Node { if x == tree.value { true } else if x < tree.value { tree.left.search(x) } else { tree.right.search(x) } } } } // find the minimal value of a BST fn (tree Tree) min() T { return match tree { Empty { T(1e9) } Node { if tree.value < tree.left.min() { tree.value } else { tree.left.min() } } } } // delete a value in BST (if nonexistant do nothing) fn (tree Tree) delete(x T) Tree { return match tree { Empty { tree } Node { if tree.left !is Empty && tree.right !is Empty { if x < tree.value { Node{ ...tree left: tree.left.delete(x) } } else if x > tree.value { Node{ ...tree right: tree.right.delete(x) } } else { Node{ ...tree value: tree.right.min() right: tree.right.delete(tree.right.min()) } } } else if tree.left !is Empty { if x == tree.value { tree.left } else { Node{ ...tree left: tree.left.delete(x) } } } else { if x == tree.value { tree.right } else { Node{ ...tree right: tree.right.delete(x) } } } } } } fn main() { mut tree := Tree(Empty{}) vals := [0.2, 0.0, 0.5, 0.3, 0.6, 0.8, 0.9, 1.0, 0.1, 0.4, 0.7] for i in vals { tree = tree.insert(i) } println('[1] after insertion tree size is ${tree.size()}') // 11 del_vals := [-0.3, 0.0, 0.3, 0.6, 1.0, 1.5] for i in del_vals { tree = tree.delete(i) } print('[2] after deletion tree size is ${tree.size()}, ') // 7 print('and these elements were deleted: ') // 0.0 0.3 0.6 1.0 for i in vals { if !tree.search(i) { print('${i} ') } } println('') }