Similarly to the binary trees earlier in this chapter, walking can be done with different strategies, even if there are many more branches to walk. The following code shows an in-order tree walking algorithm, where the callback is executed between the left child and before descending into the child that is currently looked at:
pub fn walk(&self, callback: impl Fn(&IoTDevice) -> ()) {
if let Some(ref root) = self.root {
self.walk_in_order(root, &callback);
}
}
fn walk_in_order(&self, node: &Tree, callback: &impl Fn(&IoTDevice) -> ()) {
if let Some(ref left) = node.left_child {
self.walk_in_order(left, callback);
}
for i in 0..node.devices.len() {
if let Some(ref k) = node.devices[i] {
callback(k);
}
if let Some(ref c) = node.children[i] {
self.walk_in_order(&c, callback);
}
}
}
Thanks to the internal sorting, this walk retrieves the keys in an ascending...