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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use Direction::{Left, Middle, Right, Leaf};
use InsertResult::{Fit, Split};
enum SplitNode<V: Ord> {
Four(V, V, V, Box<Node<V>>, Box<Node<V>>, Box<Node<V>>, Box<Node<V>>),
LeafFour(V, V, V),
}
impl <V: Ord> SplitNode<V> {
fn to_two(self) -> Two<V> {
match self {
SplitNode::LeafFour(v1, v2, v3) =>
Two(v2,
Box::new(Node::LeafTwo(LeafTwo(v1))),
Box::new(Node::LeafTwo(LeafTwo(v3)))),
SplitNode::Four(v1, v2, v3, n1, n2, n3, n4) =>
Two(v2,
Box::new(Node::Two(Two(v1, n1, n2))),
Box::new(Node::Two(Two(v3, n3, n4)))),
}
}
}
enum InsertResult<V: Ord> {
Fit(Node<V>),
Split(SplitNode<V>),
}
enum Direction {
Left,
Middle,
Right,
Leaf,
}
pub struct Two<V: Ord>(pub V, pub Box<Node<V>>, pub Box<Node<V>>);
impl <V: Ord> Two<V> {
fn as_node(self) -> Node<V> { Node::Two(self) }
fn to_three(self, other_value: V, other_node: Box<Node<V>>) -> Three<V> {
let Two(self_value, self_left, self_middle) = self;
if self_value > other_value {
Three(other_value, self_value, other_node, self_left, self_middle)
} else {
Three(self_value, other_value, self_left, self_middle, other_node)
}
}
fn has_value(&self, value: &V) -> bool {
let &Two(ref self_value, _, _) = self;
*self_value == *value
}
}
pub struct Three<V: Ord>(pub V, pub V, pub Box<Node<V>>, pub Box<Node<V>>, pub Box<Node<V>>);
impl <V: Ord> Three<V> {
fn as_node(self) -> Node<V> { Node::Three(self) }
fn to_split_node(self, other_value: V, other_node: Box<Node<V>>) -> SplitNode<V> {
let Three(self_value1, self_value2, self_left, self_middle, self_right) = self;
if other_value < self_value1 {
SplitNode::Four(other_value, self_value1, self_value2, other_node, self_left, self_middle, self_right)
} else {
SplitNode::Four(self_value1, self_value2, other_value, self_left, self_middle, self_right, other_node)
}
}
fn has_value(&self, value: &V) -> bool {
let Three(ref self_value1, ref self_value2, _, _, _) = *self;
*self_value1 == *value || *self_value2 == *value
}
}
pub struct LeafTwo<V: Ord>(pub V);
impl <V: Ord> LeafTwo<V> {
fn to_three(self, other_value: V) -> LeafThree<V> {
let LeafTwo(self_value) = self;
if self_value > other_value {
LeafThree(other_value, self_value)
} else {
LeafThree(self_value, other_value)
}
}
fn has_value(&self, value: &V) -> bool {
let &LeafTwo(ref self_value) = self;
*self_value == *value
}
}
pub struct LeafThree<V: Ord>(pub V, pub V);
impl <V: Ord> LeafThree<V> {
fn as_node(self) -> Node<V> { Node::LeafThree(self) }
fn to_split_node(self, value: V) -> SplitNode<V> {
let LeafThree(value1, value2) = self;
if value > value2 { SplitNode::LeafFour(value1, value2, value) }
else if value < value1 { SplitNode::LeafFour(value, value1, value2) }
else { SplitNode::LeafFour(value1, value, value2) }
}
fn has_value(&self, value: &V) -> bool {
let &LeafThree(ref self_value1, ref self_value2) = self;
*self_value1 == *value || *self_value2 == *value
}
}
pub enum Node<V: Ord> {
LeafTwo(LeafTwo<V>),
LeafThree(LeafThree<V>),
Two(Two<V>),
Three(Three<V>),
}
impl <V: Ord> Node<V> {
fn next_direction(&self, to_insert: &V) -> Direction {
match *self {
Node::Two(Two(ref value, _, _)) =>
if to_insert < value { Left }
else { Middle },
Node::Three(Three(ref value1, ref value2, _, _, _)) =>
if to_insert < value1 { Left }
else if to_insert > value2 { Right }
else { Middle },
_ => Leaf,
}
}
fn has_value(&self, value: &V) -> bool {
match *self {
Node::LeafTwo(ref n) => n.has_value(value),
Node::LeafThree(ref n) => n.has_value(value),
Node::Two(ref n) => n.has_value(value),
Node::Three(ref n) => n.has_value(value),
}
}
fn insert(self, to_insert: V) -> InsertResult<V> {
if self.has_value(&to_insert) { return Fit(self); }
let next_direction = self.next_direction(&to_insert);
match self {
Node::LeafTwo(leaf_two) => {
let three_node = leaf_two.to_three(to_insert);
Fit(three_node.as_node())
},
Node::LeafThree(leaf_three) => {
let split_node = leaf_three.to_split_node(to_insert);
Split(split_node)
},
Node::Two(Two(value, left, middle)) => {
let (next_node, other_node) = match next_direction {
Left => (left, middle),
Middle => (middle, left),
_ => unreachable!(),
};
let insert_result = next_node.insert(to_insert);
let new_node = match insert_result {
Fit(returned_node) =>
match next_direction {
Left => Two(value, Box::new(returned_node), other_node),
Middle => Two(value, other_node, Box::new(returned_node)),
_ => unreachable!(),
}.as_node(),
Split(split_node) => split_node.to_two().to_three(value, other_node).as_node(),
};
Fit(new_node)
},
Node::Three(Three(value1, value2, left, middle, right)) => {
let (next_node, other_node1, other_node2) = match next_direction {
Left => (left, middle, right),
Middle => (middle, left, right),
Right => (right, left, middle),
_ => unreachable!(),
};
let insert_result = next_node.insert(to_insert);
match insert_result {
Fit(returned_node) => {
let three = match next_direction {
Left => Three(value1, value2, Box::new(returned_node), other_node1, other_node2),
Middle => Three(value1, value2, other_node1, Box::new(returned_node), other_node2),
Right => Three(value1, value2, other_node1, other_node2, Box::new(returned_node)),
_ => unreachable!(),
};
Fit(three.as_node())
},
Split(split_node) => {
let two = split_node.to_two();
let new_node = match next_direction {
Left => two.to_three(value1, other_node1).to_split_node(value2, other_node2),
Middle => two.to_three(value1, other_node1).to_split_node(value2, other_node2),
Right => two.to_three(value2, other_node2).to_split_node(value1, other_node1),
_ => unreachable!(),
};
Split(new_node)
}
}
}
}
}
}
pub struct TTTree<V: Ord> {
pub root: Option<Node<V>>,
}
impl <V: Ord> TTTree<V> {
pub fn new() -> TTTree<V> {
TTTree { root: None }
}
pub fn insert(&mut self, value: V) -> () {
let root: Option<Node<V>> = self.root.take();
let new_root: Node<V> = match root {
None => Node::LeafTwo(LeafTwo(value)),
Some(root) => {
let result = root.insert(value);
match result {
Fit(node) => node,
Split(split_node) => split_node.to_two().as_node(),
}
}
};
self.root = Some(new_root);
}
}