May 2019
Intermediate to advanced
698 pages
17h 21m
English
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] ...Read now
Unlock full access