2021-01-19 16:18:38 +03:00
|
|
|
// Binary Search Tree example by @SleepyRoy
|
|
|
|
|
|
|
|
struct Empty {}
|
|
|
|
|
2021-09-24 21:17:26 +03:00
|
|
|
struct Node<T> {
|
|
|
|
value T
|
|
|
|
left Tree<T>
|
|
|
|
right Tree<T>
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:17:26 +03:00
|
|
|
type Tree<T> = Empty | Node<T>
|
2021-01-19 16:18:38 +03:00
|
|
|
|
|
|
|
// return size(number of nodes) of BST
|
2021-09-24 21:17:26 +03:00
|
|
|
fn (tree Tree<T>) size<T>() int {
|
2021-01-19 16:18:38 +03:00
|
|
|
return match tree {
|
2021-05-27 20:13:02 +03:00
|
|
|
Empty { 0 }
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T> { 1 + tree.left.size() + tree.right.size() }
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert a value to BST
|
2021-09-24 21:17:26 +03:00
|
|
|
fn (tree Tree<T>) insert<T>(x T) Tree<T> {
|
2021-05-27 20:13:02 +03:00
|
|
|
return match tree {
|
2021-02-23 20:43:44 +03:00
|
|
|
Empty {
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T>{x, tree, tree}
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T> {
|
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-09-24 21:17:26 +03:00
|
|
|
Node<T>{
|
2021-02-23 20:43:44 +03:00
|
|
|
...tree
|
2021-09-24 21:17:26 +03:00
|
|
|
left: tree.left.insert(x)
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
} else {
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T>{
|
2021-02-23 20:43:44 +03:00
|
|
|
...tree
|
2021-09-24 21:17:26 +03:00
|
|
|
right: tree.right.insert(x)
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// whether able to find a value in BST
|
2021-09-24 21:17:26 +03:00
|
|
|
fn (tree Tree<T>) search<T>(x T) 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
|
|
|
}
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T> {
|
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 {
|
2021-09-24 21:17:26 +03:00
|
|
|
tree.left.search(x)
|
2021-01-19 16:18:38 +03:00
|
|
|
} else {
|
2021-09-24 21:17:26 +03:00
|
|
|
tree.right.search(x)
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the minimal value of a BST
|
2021-09-24 21:17:26 +03:00
|
|
|
fn (tree Tree<T>) min<T>() T {
|
2021-05-27 20:13:02 +03:00
|
|
|
return match tree {
|
|
|
|
Empty {
|
2021-09-24 21:17:26 +03:00
|
|
|
T(1e9)
|
2021-05-27 20:13:02 +03:00
|
|
|
}
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T> {
|
|
|
|
if tree.value < tree.left.min() {
|
2021-05-27 20:13:02 +03:00
|
|
|
tree.value
|
|
|
|
} else {
|
2021-09-24 21:17:26 +03:00
|
|
|
tree.left.min()
|
2021-05-27 20:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
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-09-24 21:17:26 +03:00
|
|
|
fn (tree Tree<T>) delete<T>(x T) Tree<T> {
|
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-09-24 21:17:26 +03:00
|
|
|
Node<T> {
|
|
|
|
if tree.left !is Empty && tree.right !is Empty {
|
2021-05-27 20:13:02 +03:00
|
|
|
if x < tree.value {
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T>{
|
2021-02-23 20:43:44 +03:00
|
|
|
...tree
|
2021-09-24 21:17:26 +03:00
|
|
|
left: tree.left.delete(x)
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
} else if x > tree.value {
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T>{
|
2021-02-23 20:43:44 +03:00
|
|
|
...tree
|
2021-09-24 21:17:26 +03:00
|
|
|
right: tree.right.delete(x)
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
} else {
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T>{
|
2021-02-23 20:43:44 +03:00
|
|
|
...tree
|
2021-09-24 21:17:26 +03:00
|
|
|
value: tree.right.min()
|
|
|
|
right: tree.right.delete(tree.right.min())
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 21:17:26 +03:00
|
|
|
} else if tree.left !is Empty {
|
2021-05-27 20:13:02 +03:00
|
|
|
if x == tree.value {
|
2021-02-23 20:43:44 +03:00
|
|
|
tree.left
|
|
|
|
} else {
|
2021-09-24 21:17:26 +03:00
|
|
|
Node<T>{
|
2021-02-23 20:43:44 +03:00
|
|
|
...tree
|
2021-09-24 21:17:26 +03:00
|
|
|
left: tree.left.delete(x)
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
|
|
|
}
|
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-09-24 21:17:26 +03:00
|
|
|
Node<T>{
|
2021-02-23 20:43:44 +03:00
|
|
|
...tree
|
2021-09-24 21:17:26 +03:00
|
|
|
right: tree.right.delete(x)
|
2021-02-23 20:43:44 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-09-24 21:17:26 +03: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)
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
println('[1] after insertion tree size is ${tree.size()}') // 11
|
2021-09-24 21:17:26 +03:00
|
|
|
del_vals := [-0.3, 0.0, 0.3, 0.6, 1.0, 1.5]
|
|
|
|
for i in del_vals {
|
|
|
|
tree = tree.delete(i)
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
print('[2] after deletion tree size is ${tree.size()}, ') // 7
|
2021-09-24 21:17:26 +03:00
|
|
|
print('and these elements were deleted: ') // 0.0 0.3 0.6 1.0
|
|
|
|
for i in vals {
|
|
|
|
if !tree.search(i) {
|
2022-11-15 16:53:13 +03:00
|
|
|
print('${i} ')
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 21:17:26 +03:00
|
|
|
println('')
|
2021-01-19 16:18:38 +03:00
|
|
|
}
|