2021-01-19 16:18:38 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tree = Empty | Node
|
|
|
|
|
|
|
|
// return size(number of nodes) of BST
|
|
|
|
fn size(tree Tree) int {
|
|
|
|
return match tree {
|
2021-05-27 20:13:02 +03:00
|
|
|
Empty { 0 }
|
2021-01-19 16:18:38 +03:00
|
|
|
Node { 1 + size(tree.left) + size(tree.right) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert a value to BST
|
|
|
|
fn insert(tree Tree, x f64) Tree {
|
2021-05-27 20:13:02 +03:00
|
|
|
return match tree {
|
2021-02-23 20:43:44 +03:00
|
|
|
Empty {
|
2021-05-27 20:13:02 +03:00
|
|
|
Node{x, tree, tree}
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
|
|
|
Node {
|
2021-05-27 20:13:02 +03:00
|
|
|
if x == tree.value {
|
2021-01-19 16:18:38 +03:00
|
|
|
tree
|
|
|
|
} else if x < tree.value {
|
2021-02-23 20:43:44 +03:00
|
|
|
Node{
|
|
|
|
...tree
|
|
|
|
left: insert(tree.left, x)
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
} else {
|
2021-02-23 20:43:44 +03:00
|
|
|
Node{
|
|
|
|
...tree
|
|
|
|
right: insert(tree.right, x)
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// whether able to find a value in BST
|
|
|
|
fn search(tree Tree, x f64) bool {
|
2021-05-27 20:13:02 +03:00
|
|
|
return match tree {
|
2021-02-23 20:43:44 +03:00
|
|
|
Empty {
|
2021-05-27 20:13:02 +03:00
|
|
|
false
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
|
|
|
Node {
|
2021-05-27 20:13:02 +03:00
|
|
|
if x == tree.value {
|
2021-01-19 16:18:38 +03:00
|
|
|
true
|
|
|
|
} else if x < tree.value {
|
|
|
|
search(tree.left, x)
|
|
|
|
} else {
|
|
|
|
search(tree.right, x)
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the minimal value of a BST
|
|
|
|
fn min(tree Tree) f64 {
|
2021-05-27 20:13:02 +03:00
|
|
|
return match tree {
|
|
|
|
Empty {
|
|
|
|
1e100
|
|
|
|
}
|
|
|
|
Node {
|
|
|
|
if tree.value < min(tree.left) {
|
|
|
|
tree.value
|
|
|
|
} else {
|
|
|
|
min(tree.left)
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 12:15:11 +03:00
|
|
|
// delete a value in BST (if nonexistant do nothing)
|
2021-01-19 16:18:38 +03:00
|
|
|
fn delete(tree Tree, x f64) Tree {
|
2021-05-27 20:13:02 +03:00
|
|
|
return match tree {
|
2021-02-23 20:43:44 +03:00
|
|
|
Empty {
|
2021-05-27 20:13:02 +03:00
|
|
|
tree
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
Node {
|
|
|
|
if tree.left is Node && tree.right is Node {
|
2021-05-27 20:13:02 +03:00
|
|
|
if x < tree.value {
|
2021-02-23 20:43:44 +03:00
|
|
|
Node{
|
|
|
|
...tree
|
|
|
|
left: delete(tree.left, x)
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
} else if x > tree.value {
|
2021-02-23 20:43:44 +03:00
|
|
|
Node{
|
|
|
|
...tree
|
|
|
|
right: delete(tree.right, x)
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
} else {
|
2021-02-23 20:43:44 +03:00
|
|
|
Node{
|
|
|
|
...tree
|
|
|
|
value: min(tree.right)
|
|
|
|
right: delete(tree.right, min(tree.right))
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
} else if tree.left is Node {
|
2021-05-27 20:13:02 +03:00
|
|
|
if x == tree.value {
|
2021-02-23 20:43:44 +03:00
|
|
|
tree.left
|
|
|
|
} else {
|
|
|
|
Node{
|
|
|
|
...tree
|
|
|
|
left: delete(tree.left, x)
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
} else {
|
2021-02-23 20:43:44 +03:00
|
|
|
if x == tree.value {
|
2021-05-27 20:13:02 +03:00
|
|
|
tree.right
|
2021-02-23 20:43:44 +03:00
|
|
|
} else {
|
2021-05-27 20:13:02 +03:00
|
|
|
Node{
|
2021-02-23 20:43:44 +03:00
|
|
|
...tree
|
|
|
|
right: delete(tree.right, x)
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-04-14 08:50:50 +03:00
|
|
|
$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(' ')
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
2021-04-14 08:50:50 +03:00
|
|
|
println('')
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|