From 4fbf3fdf0310f04ea2e206f2e1257f3466cd1ab8 Mon Sep 17 00:00:00 2001 From: Ruofan XU <47302112+SleepyRoy@users.noreply.github.com> Date: Sat, 25 Sep 2021 02:17:26 +0800 Subject: [PATCH] examples: make the binary search tree example generic (#11968) --- examples/binary_search_tree.v | 135 ++++++++++++++-------------------- 1 file changed, 55 insertions(+), 80 deletions(-) diff --git a/examples/binary_search_tree.v b/examples/binary_search_tree.v index 514426ac6a..88a2d31746 100644 --- a/examples/binary_search_tree.v +++ b/examples/binary_search_tree.v @@ -1,42 +1,41 @@ // Binary Search Tree example by @SleepyRoy -// TODO: make Node.value generic once it's robust enough struct Empty {} -struct Node { - value f64 - left Tree - right Tree +struct Node { + value T + left Tree + right Tree } -type Tree = Empty | Node +type Tree = Empty | Node // return size(number of nodes) of BST -fn size(tree Tree) int { +fn (tree Tree) size() int { return match tree { Empty { 0 } - Node { 1 + size(tree.left) + size(tree.right) } + Node { 1 + tree.left.size() + tree.right.size() } } } // insert a value to BST -fn insert(tree Tree, x f64) Tree { +fn (tree Tree) insert(x T) Tree { return match tree { Empty { - Node{x, tree, tree} + Node{x, tree, tree} } - Node { + Node { if x == tree.value { tree } else if x < tree.value { - Node{ + Node{ ...tree - left: insert(tree.left, x) + left: tree.left.insert(x) } } else { - Node{ + Node{ ...tree - right: insert(tree.right, x) + right: tree.right.insert(x) } } } @@ -44,80 +43,80 @@ fn insert(tree Tree, x f64) Tree { } // whether able to find a value in BST -fn search(tree Tree, x f64) bool { +fn (tree Tree) search(x T) bool { return match tree { Empty { false } - Node { + Node { if x == tree.value { true } else if x < tree.value { - search(tree.left, x) + tree.left.search(x) } else { - search(tree.right, x) + tree.right.search(x) } } } } // find the minimal value of a BST -fn min(tree Tree) f64 { +fn (tree Tree) min() T { return match tree { Empty { - 1e100 + T(1e9) } - Node { - if tree.value < min(tree.left) { + Node { + if tree.value < tree.left.min() { tree.value } else { - min(tree.left) + tree.left.min() } } } } // delete a value in BST (if nonexistant do nothing) -fn delete(tree Tree, x f64) Tree { +fn (tree Tree) delete(x T) Tree { return match tree { Empty { tree } - Node { - if tree.left is Node && tree.right is Node { + Node { + if tree.left !is Empty && tree.right !is Empty { if x < tree.value { - Node{ + Node{ ...tree - left: delete(tree.left, x) + left: tree.left.delete(x) } } else if x > tree.value { - Node{ + Node{ ...tree - right: delete(tree.right, x) + right: tree.right.delete(x) } } else { - Node{ + Node{ ...tree - value: min(tree.right) - right: delete(tree.right, min(tree.right)) + value: tree.right.min() + right: tree.right.delete(tree.right.min()) } } - } else if tree.left is Node { + } else if tree.left !is Empty { if x == tree.value { tree.left } else { - Node{ + Node{ ...tree - left: delete(tree.left, x) + left: tree.left.delete(x) } } } else { if x == tree.value { tree.right } else { - Node{ + Node{ ...tree - right: delete(tree.right, x) + right: tree.right.delete(x) } } } @@ -126,46 +125,22 @@ fn delete(tree Tree, x f64) Tree { } fn main() { - $if !freestanding { - mut tree := Tree(Empty{}) - input := [0.3, 0.2, 0.5, 0.0, 0.6, 0.8, 0.9, 1.0, 0.1, 0.4, 0.7] - for i in input { - tree = insert(tree, i) - } - println('[1] after insertion tree size is ${size(tree)}') // 11 - del := [-0.3, 0.0, 0.3, 0.6, 1.0, 1.5] - for i in del { - tree = delete(tree, i) - } - print('[2] after deletion tree size is ${size(tree)}, ') // 7 - print('and these elements were deleted: ') // 0.0 0.3 0.6 1.0 - for i in input { - if !search(tree, i) { - print('$i ') - } - } - println('') - } $else { - mut tree := Tree(Empty{}) - input := [0.3, 0.2, 0.5, 0.0, 0.6, 0.8, 0.9, 1.0, 0.1, 0.4, 0.7] - for i in input { - tree = insert(tree, i) - } - print('[1] after insertion tree size is ') // 11 - println(size(tree)) - del := [-0.3, 0.0, 0.3, 0.6, 1.0, 1.5] - for i in del { - tree = delete(tree, i) - } - print('[2] after deletion tree size is ') // 7 - print(size(tree)) - print(', and these elements were deleted: ') // 0.0 0.3 0.6 1.0 - for i in input { - if !search(tree, i) { - print(i) - print(' ') - } - } - println('') + 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('') }