2021-01-19 14:18:38 +01:00
|
|
|
// Binary Search Tree example by @SleepyRoy
|
|
|
|
|
|
|
|
struct Empty {}
|
|
|
|
|
2021-09-24 20:17:26 +02:00
|
|
|
struct Node<T> {
|
|
|
|
value T
|
|
|
|
left Tree<T>
|
|
|
|
right Tree<T>
|
2021-01-19 14:18:38 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 20:17:26 +02:00
|
|
|
type Tree<T> = Empty | Node<T>
|
2021-01-19 14:18:38 +01:00
|
|
|
|
|
|
|
// return size(number of nodes) of BST
|
2021-09-24 20:17:26 +02:00
|
|
|
fn (tree Tree<T>) size<T>() int {
|
2021-01-19 14:18:38 +01:00
|
|
|
return match tree {
|
2021-05-27 19:13:02 +02:00
|
|
|
Empty { 0 }
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T> { 1 + tree.left.size() + tree.right.size() }
|
2021-01-19 14:18:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert a value to BST
|
2021-09-24 20:17:26 +02:00
|
|
|
fn (tree Tree<T>) insert<T>(x T) Tree<T> {
|
2021-05-27 19:13:02 +02:00
|
|
|
return match tree {
|
2021-02-23 18:43:44 +01:00
|
|
|
Empty {
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T>{x, tree, tree}
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T> {
|
2021-05-27 19:13:02 +02:00
|
|
|
if x == tree.value {
|
2021-01-19 14:18:38 +01:00
|
|
|
tree
|
|
|
|
} else if x < tree.value {
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T>{
|
2021-02-23 18:43:44 +01:00
|
|
|
...tree
|
2021-09-24 20:17:26 +02:00
|
|
|
left: tree.left.insert(x)
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
2021-01-19 14:18:38 +01:00
|
|
|
} else {
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T>{
|
2021-02-23 18:43:44 +01:00
|
|
|
...tree
|
2021-09-24 20:17:26 +02:00
|
|
|
right: tree.right.insert(x)
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 14:18:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// whether able to find a value in BST
|
2021-09-24 20:17:26 +02:00
|
|
|
fn (tree Tree<T>) search<T>(x T) bool {
|
2021-05-27 19:13:02 +02:00
|
|
|
return match tree {
|
2021-02-23 18:43:44 +01:00
|
|
|
Empty {
|
2021-05-27 19:13:02 +02:00
|
|
|
false
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T> {
|
2021-05-27 19:13:02 +02:00
|
|
|
if x == tree.value {
|
2021-01-19 14:18:38 +01:00
|
|
|
true
|
|
|
|
} else if x < tree.value {
|
2021-09-24 20:17:26 +02:00
|
|
|
tree.left.search(x)
|
2021-01-19 14:18:38 +01:00
|
|
|
} else {
|
2021-09-24 20:17:26 +02:00
|
|
|
tree.right.search(x)
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
2021-01-19 14:18:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the minimal value of a BST
|
2021-09-24 20:17:26 +02:00
|
|
|
fn (tree Tree<T>) min<T>() T {
|
2021-05-27 19:13:02 +02:00
|
|
|
return match tree {
|
|
|
|
Empty {
|
2021-09-24 20:17:26 +02:00
|
|
|
T(1e9)
|
2021-05-27 19:13:02 +02:00
|
|
|
}
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T> {
|
|
|
|
if tree.value < tree.left.min() {
|
2021-05-27 19:13:02 +02:00
|
|
|
tree.value
|
|
|
|
} else {
|
2021-09-24 20:17:26 +02:00
|
|
|
tree.left.min()
|
2021-05-27 19:13:02 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 14:18:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 10:15:11 +01:00
|
|
|
// delete a value in BST (if nonexistant do nothing)
|
2021-09-24 20:17:26 +02:00
|
|
|
fn (tree Tree<T>) delete<T>(x T) Tree<T> {
|
2021-05-27 19:13:02 +02:00
|
|
|
return match tree {
|
2021-02-23 18:43:44 +01:00
|
|
|
Empty {
|
2021-05-27 19:13:02 +02:00
|
|
|
tree
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T> {
|
|
|
|
if tree.left !is Empty && tree.right !is Empty {
|
2021-05-27 19:13:02 +02:00
|
|
|
if x < tree.value {
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T>{
|
2021-02-23 18:43:44 +01:00
|
|
|
...tree
|
2021-09-24 20:17:26 +02:00
|
|
|
left: tree.left.delete(x)
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
2021-01-19 14:18:38 +01:00
|
|
|
} else if x > tree.value {
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T>{
|
2021-02-23 18:43:44 +01:00
|
|
|
...tree
|
2021-09-24 20:17:26 +02:00
|
|
|
right: tree.right.delete(x)
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
2021-01-19 14:18:38 +01:00
|
|
|
} else {
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T>{
|
2021-02-23 18:43:44 +01:00
|
|
|
...tree
|
2021-09-24 20:17:26 +02:00
|
|
|
value: tree.right.min()
|
|
|
|
right: tree.right.delete(tree.right.min())
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 20:17:26 +02:00
|
|
|
} else if tree.left !is Empty {
|
2021-05-27 19:13:02 +02:00
|
|
|
if x == tree.value {
|
2021-02-23 18:43:44 +01:00
|
|
|
tree.left
|
|
|
|
} else {
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T>{
|
2021-02-23 18:43:44 +01:00
|
|
|
...tree
|
2021-09-24 20:17:26 +02:00
|
|
|
left: tree.left.delete(x)
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 14:18:38 +01:00
|
|
|
} else {
|
2021-02-23 18:43:44 +01:00
|
|
|
if x == tree.value {
|
2021-05-27 19:13:02 +02:00
|
|
|
tree.right
|
2021-02-23 18:43:44 +01:00
|
|
|
} else {
|
2021-09-24 20:17:26 +02:00
|
|
|
Node<T>{
|
2021-02-23 18:43:44 +01:00
|
|
|
...tree
|
2021-09-24 20:17:26 +02:00
|
|
|
right: tree.right.delete(x)
|
2021-02-23 18:43:44 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 14:18:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-09-24 20:17:26 +02:00
|
|
|
mut tree := Tree<f64>(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 ')
|
2021-01-19 14:18:38 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 20:17:26 +02:00
|
|
|
println('')
|
2021-01-19 14:18:38 +01:00
|
|
|
}
|