From 103901a5cb4a23eece8cb728be2c3cab25bfcade Mon Sep 17 00:00:00 2001 From: Ruofan XU <47302112+SleepyRoy@users.noreply.github.com> Date: Tue, 19 Jan 2021 21:18:38 +0800 Subject: [PATCH] example/test: add a binary search tree example and test (#8145) --- examples/binary_search_tree.v | 106 +++++++++++++++++++++++++++++++++ vlib/v/tests/sum_type_test.v | 107 ++++++++++++++++++++++++++++------ 2 files changed, 195 insertions(+), 18 deletions(-) create mode 100644 examples/binary_search_tree.v diff --git a/examples/binary_search_tree.v b/examples/binary_search_tree.v new file mode 100644 index 0000000000..64015f59c8 --- /dev/null +++ b/examples/binary_search_tree.v @@ -0,0 +1,106 @@ +// Binary Search Tree example by @SleepyRoy + +// TODO: make Node.value generic once it's robust enough +// TODO: `return match` instead of returns everywhere inside match + +struct Empty {} + +struct Node { + value f64 + left Tree + right Tree +} + +type Tree = Empty | Node + +// return size(number of nodes) of BST +fn size(tree Tree) int { + return match tree { + // TODO: remove int() once match gets smarter + Empty { int(0) } + Node { 1 + size(tree.left) + size(tree.right) } + } +} + +// insert a value to BST +fn insert(tree Tree, x f64) Tree { + match tree { + Empty { return Node{x, tree, tree} } + Node { + return if x == tree.value { + tree + } else if x < tree.value { + Node{...tree, left: insert(tree.left, x)} + } else { + Node{...tree, right: insert(tree.right, x)} + } + } + } +} + +// whether able to find a value in BST +fn search(tree Tree, x f64) bool { + match tree { + Empty { return false } + Node { + return if x == tree.value { + true + } else if x < tree.value { + search(tree.left, x) + } else { + search(tree.right, x) + } + } + } +} + +// find the minimal value of a BST +fn min(tree Tree) f64 { + match tree { + Empty { return 1e100 } + Node { return if tree.value < min(tree.left) { tree.value } else { min(tree.left) } } + } +} + +// delete a value in BST (if nonexist do nothing) +fn delete(tree Tree, x f64) Tree { + match tree { + Empty { return tree } + Node { + if tree.left is Node && tree.right is Node { + return if x < tree.value { + Node{...tree, left: delete(tree.left, x)} + } else if x > tree.value { + Node{...tree, right: delete(tree.right, x)} + } else { + Node{...tree, value: min(tree.right), right: delete(tree.right, min(tree.right))} + } + } else if tree.left is Node { + return if x == tree.value { tree.left } else { Node{...tree, left: delete(tree.left, x)} } + } else { + if x == tree.value { return tree.right } else { return Node{...tree, right: delete(tree.right, x)} } + } + } + } +} + +fn main() { + 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('') +} diff --git a/vlib/v/tests/sum_type_test.v b/vlib/v/tests/sum_type_test.v index 2778cc4496..7e95595ed5 100644 --- a/vlib/v/tests/sum_type_test.v +++ b/vlib/v/tests/sum_type_test.v @@ -1,6 +1,6 @@ type Expr = IfExpr | IntegerLiteral type Stmt = FnDecl | StructDecl -type Node = Expr | Stmt +type ExprStmt = Expr | Stmt struct FnDecl { pos int @@ -540,38 +540,109 @@ fn handle(e Expr) string { return '' } -// for a binary tree +// Binary Search Tree test by @SleepyRoy +// TODO: make Node.value generic once it's robust enough +// TODO: `return match` instead of returns everywhere inside match + struct Empty {} -struct Node_ { - // TODO: make value generic once it's more robust +struct Node { value f64 left Tree right Tree } -type Tree = Empty | Node_ +type Tree = Empty | Node +// return size(number of nodes) of BST fn size(tree Tree) int { return match tree { - // TODO: remove int() here once match gets smarter + // TODO: remove int() once match gets smarter Empty { int(0) } - Node_ { 1 + size(tree.left) + size(tree.right) } + Node { 1 + size(tree.left) + size(tree.right) } } } -fn sum(tree Tree) f64 { - return match tree { - // TODO: remove f64() here once match gets smarter - Empty { f64(0) } - Node_ { tree.value + sum(tree.left) + sum(tree.right) } +// insert a value to BST +fn insert(tree Tree, x f64) Tree { + match tree { + Empty { return Node{x, tree, tree} } + Node { + return if x == tree.value { + tree + } else if x < tree.value { + Node{...tree, left: insert(tree.left, x)} + } else { + Node{...tree, right: insert(tree.right, x)} + } + } } } -fn test_binary_tree_operation() { - left := Node_{0.2, Empty{}, Empty{}} - right := Node_{0.3, Empty{}, Node_{0.4, Empty{}, Empty{}}} - tree := Node_{0.5, left, right} - assert size(tree) == 4 - assert sum(tree) == 1.4 +// whether able to find a value in BST +fn search(tree Tree, x f64) bool { + match tree { + Empty { return false } + Node { + return if x == tree.value { + true + } else if x < tree.value { + search(tree.left, x) + } else { + search(tree.right, x) + } + } + } +} + +// find the minimal value of a BST +fn min(tree Tree) f64 { + match tree { + Empty { return 1e100 } + Node { return if tree.value < min(tree.left) { tree.value } else { min(tree.left) } } + } +} + +// delete a value in BST (if nonexist do nothing) +fn delete(tree Tree, x f64) Tree { + match tree { + Empty { return tree } + Node { + if tree.left is Node && tree.right is Node { + return if x < tree.value { + Node{...tree, left: delete(tree.left, x)} + } else if x > tree.value { + Node{...tree, right: delete(tree.right, x)} + } else { + Node{...tree, value: min(tree.right), right: delete(tree.right, min(tree.right))} + } + } else if tree.left is Node { + return if x == tree.value { tree.left } else { Node{...tree, left: delete(tree.left, x)} } + } else { + if x == tree.value { return tree.right } else { return Node{...tree, right: delete(tree.right, x)} } + } + } + } +} + +fn test_binary_search_tree() { + 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) + } + assert size(tree) == 11 + del := [-0.3, 0.0, 0.3, 0.6, 1.0, 1.5] + for i in del { + tree = delete(tree, i) + } + assert size(tree) == 7 + mut deleted := []f64{ } + for i in input { + if !search(tree, i) { + deleted << i + } + } + deleted.sort() + assert deleted == [0.0, 0.3, 0.6, 1.0] }