1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
// 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 {
Empty { 0 }
Node { 1 + size(tree.left) + size(tree.right) }
}
}
// insert a value to BST
fn insert(tree Tree, x f64) Tree {
return match tree {
Empty {
Node{x, tree, tree}
}
Node {
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 {
return match tree {
Empty {
false
}
Node {
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 {
return match tree {
Empty {
1e100
}
Node {
if tree.value < min(tree.left) {
tree.value
} else {
min(tree.left)
}
}
}
}
// delete a value in BST (if nonexistant do nothing)
fn delete(tree Tree, x f64) Tree {
return match tree {
Empty {
tree
}
Node {
if tree.left is Node && tree.right is Node {
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 {
if x == tree.value {
tree.left
} else {
Node{
...tree
left: delete(tree.left, x)
}
}
} else {
if x == tree.value {
tree.right
} else {
Node{
...tree
right: delete(tree.right, x)
}
}
}
}
}
}
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('')
}
}
|