Skip to content

Commit 10fc416

Browse files
author
user
committed
Make some minor corrections and turn some methods into recursive ones.
1 parent 9deb1b5 commit 10fc416

File tree

2 files changed

+65
-82
lines changed

2 files changed

+65
-82
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ When implementing an AVL tree, there are some choices:
99
This is a recursive implementation of an AVL tree that stores the height in a node
1010
and does not store the parent pointer in it. Key points:
1111
* Written in pure Rust.
12-
* No `unsafe` code (other than the [mutable iterator](src/avl.rs#L544)).
12+
* No `unsafe` code (other than the [mutable iterator](src/avl.rs#L527)).
1313
* The key type can be any type that has `Ord`.
1414
* Each node is identified by a key, and so there are no nodes with the same key.
1515

src/avl.rs

Lines changed: 64 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
use std::mem as memory;
2+
use std::mem;
33

44

55
#[derive(Clone, Default)]
@@ -48,17 +48,17 @@ impl<K: Ord, V> AvlTree<K, V> {
4848

4949

5050
pub fn get(&self, key: &K) -> Option<&V> {
51-
Some(&self.root.as_ref()?.get(&key)?.value)
51+
Some(&self.root.as_deref()?.get(&key)?.value)
5252
}
5353

5454

5555
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
56-
Some(&mut self.root.as_mut()?.get_mut(&key)?.value)
56+
Some(&mut self.root.as_deref_mut()?.get_mut(&key)?.value)
5757
}
5858

5959

6060
pub fn first_key_value(&self) -> Option<(&K, &V)> {
61-
Some(self.root.as_ref()?.minimum())
61+
Some(self.root.as_deref()?.minimum())
6262
}
6363

6464

@@ -121,7 +121,7 @@ impl<K: Ord, V> AvlTree<K, V> {
121121

122122

123123
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
124-
match self.root.insert_(Box::new(Node::new(key, value))) {
124+
match self.root.append(Box::new(Node::new(key, value))) {
125125
None => {
126126
self.node_count += 1;
127127
None
@@ -157,106 +157,89 @@ impl<K: Ord, V> Node<K, V> {
157157
}
158158

159159

160-
fn get(&self, with: &K) -> Option<&Self> {
161-
let mut current: Option<&Node<K, V>> = Some(self);
162-
while let Some(node) = current {
163-
if *with < node.key {
164-
current = node.left.as_deref();
165-
} else if *with > node.key {
166-
current = node.right.as_deref();
167-
} else {
168-
break;
169-
}
160+
fn get(&self, key: &K) -> Option<&Self> {
161+
if *key < self.key {
162+
self.left.as_deref()?.get(key)
163+
} else if *key > self.key {
164+
self.right.as_deref()?.get(key)
165+
} else {
166+
Some(self)
170167
}
171-
current
172168
}
173169

174170

175-
fn get_mut(&mut self, with: &K) -> Option<&mut Self> {
176-
let mut current: Option<&mut Node<K, V>> = Some(self);
177-
while let Some(node) = current {
178-
if *with < node.key {
179-
current = node.left.as_deref_mut();
180-
} else if *with > node.key {
181-
current = node.right.as_deref_mut();
182-
} else {
183-
current = Some(node);
184-
break;
185-
}
171+
fn get_mut(&mut self, key: &K) -> Option<&mut Self> {
172+
if *key < self.key {
173+
self.left.as_deref_mut()?.get_mut(key)
174+
} else if *key > self.key {
175+
self.right.as_deref_mut()?.get_mut(key)
176+
} else {
177+
Some(self)
186178
}
187-
current
188179
}
189180

190181

191-
fn minimum(mut self: &Self) -> (&K, &V) {
192-
while let Some(left) = self.left.as_deref() {
193-
self = left
182+
fn minimum(&self) -> (&K, &V) {
183+
match self.left.as_deref() {
184+
Some(left) => left.minimum(),
185+
None => (&self.key, &self.value),
194186
}
195-
(&self.key, &self.value)
196187
}
197188

198189

199-
fn minimum_mut(mut self: &mut Self) -> (&K, &mut V) {
200-
while let Some(left) = self.left.as_deref_mut() {
201-
self = left
190+
fn minimum_mut(&mut self) -> (&K, &mut V) {
191+
match self.left.as_deref_mut() {
192+
Some(left) => left.minimum_mut(),
193+
None => (&self.key, &mut self.value),
202194
}
203-
(&self.key, &mut self.value)
204195
}
205196

206197

207-
fn maximum(mut self: &Self) -> (&K, &V) {
208-
while let Some(right) = self.right.as_deref() {
209-
self = right
198+
fn maximum(&self) -> (&K, &V) {
199+
match self.right.as_deref() {
200+
Some(right) => right.maximum(),
201+
None => (&self.key, &self.value),
210202
}
211-
(&self.key, &self.value)
212203
}
213204

214205

215-
fn maximum_mut(mut self: &mut Self) -> (&K, &mut V) {
216-
while let Some(right) = self.right.as_deref_mut() {
217-
self = right
206+
fn maximum_mut(&mut self) -> (&K, &mut V) {
207+
match self.right.as_deref_mut() {
208+
Some(right) => right.maximum_mut(),
209+
None => (&self.key, &mut self.value),
218210
}
219-
(&self.key, &mut self.value)
220211
}
221212

222213

223214
fn predecessor(&self, of: &K) -> Option<(&K, &V)> {
224-
let mut predecessor: Option<(&K, &V)> = None;
225-
let mut current: Option<&Node<K, V>> = Some(self);
226-
while let Some(node) = current {
227-
if *of < node.key {
228-
current = node.left.as_deref();
229-
} else if *of > node.key {
230-
predecessor = Some((&node.key, &node.value));
231-
current = node.right.as_deref();
232-
} else if let Some(left) = node.left.as_deref() {
233-
predecessor = Some(left.maximum());
234-
break;
235-
} else {
236-
break;
237-
}
215+
if *of < self.key {
216+
self.left.as_deref()?.predecessor(of)
217+
} else if *of > self.key {
218+
self.right
219+
.as_deref()
220+
.and_then(|right: &Node<K, V>| right.predecessor(of))
221+
.or(Some((&self.key, &self.value)))
222+
} else if let Some(left) = self.left.as_deref() {
223+
Some(left.maximum())
224+
} else {
225+
None
238226
}
239-
predecessor
240227
}
241228

242229

243230
fn successor(&self, of: &K) -> Option<(&K, &V)> {
244-
let mut successor: Option<(&K, &V)> = None;
245-
let mut current: Option<&Node<K, V>> = Some(self);
246-
while let Some(node) = current {
247-
if *of < node.key {
248-
successor = Some((&node.key, &node.value));
249-
current = node.left.as_deref();
250-
} else if *of > node.key {
251-
current = node.right.as_deref();
252-
} else if let Some(right) = node.right.as_deref() {
253-
successor = Some(right.minimum());
254-
break;
255-
} else {
256-
break;
257-
}
231+
if *of < self.key {
232+
self.left
233+
.as_deref()
234+
.and_then(|left: &Node<K, V>| left.successor(of))
235+
.or(Some((&self.key, &self.value)))
236+
} else if *of > self.key {
237+
self.right.as_deref()?.successor(of)
238+
} else if let Some(right) = self.right.as_deref() {
239+
Some(right.minimum())
240+
} else {
241+
None
258242
}
259-
successor
260243
}
261244

262245

@@ -280,7 +263,7 @@ impl<K: Ord, V> Node<K, V> {
280263
fn rotate_left(&mut self) {
281264
let mut t: Box<Node<K, V>> = self.right.take().unwrap();
282265
self.right = t.left.take();
283-
memory::swap(self, &mut t); // `self` is `t` now.
266+
mem::swap(self, &mut t); // `self` is `t` now.
284267
t.update_height();
285268
self.left = Some(t);
286269
self.update_height();
@@ -297,7 +280,7 @@ impl<K: Ord, V> Node<K, V> {
297280
fn rotate_right(&mut self) {
298281
let mut r: Box<Node<K, V>> = self.left.take().unwrap();
299282
self.left = r.right.take();
300-
memory::swap(self, &mut r); // `self` is `r` now.
283+
mem::swap(self, &mut r); // `self` is `r` now.
301284
r.update_height();
302285
self.right = Some(r);
303286
self.update_height();
@@ -327,7 +310,7 @@ impl<K: Ord, V> Node<K, V> {
327310

328311
trait OptionNodeExt<K: Ord, V> {
329312
fn height(&self) -> u8;
330-
fn insert_(&mut self, new: Box<Node<K, V>>) -> Option<V>;
313+
fn append(&mut self, new: Box<Node<K, V>>) -> Option<V>;
331314
fn remove(&mut self, key: &K) -> Option<V>;
332315
fn remove_minimum(&mut self) -> Option<(K, V)>;
333316
fn remove_maximum(&mut self) -> Option<(K, V)>;
@@ -341,18 +324,18 @@ impl<K: Ord, V> OptionNodeExt<K, V> for Option<Box<Node<K, V>>> {
341324
}
342325

343326

344-
fn insert_(&mut self, new: Box<Node<K, V>>) -> Option<V> {
327+
fn append(&mut self, new: Box<Node<K, V>>) -> Option<V> {
345328
let Some(mut node) = self.take() else {
346329
*self = Some(new);
347330
return None;
348331
};
349332
let old: Option<V>;
350333
if new.key < node.key {
351-
old = node.left.insert_(new);
334+
old = node.left.append(new);
352335
} else if new.key > node.key {
353-
old = node.right.insert_(new);
336+
old = node.right.append(new);
354337
} else {
355-
old = Some(memory::replace(&mut node.value, new.value));
338+
old = Some(mem::replace(&mut node.value, new.value));
356339
}
357340
node.rebalance();
358341
*self = Some(node);

0 commit comments

Comments
 (0)