Data update

This commit is contained in:
Ingy döt Net 2024-07-13 15:19:22 -07:00
parent 29a5eea0d4
commit 5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions

View file

@ -64,38 +64,23 @@ impl<T> TreeNode<T> {
res
}
// Leftmost to rightmost
// From left to right
fn iterative_inorder(&self) -> Vec<&TreeNode<T>> {
let mut stack: Vec<&TreeNode<T>> = Vec::new();
let mut res: Vec<&TreeNode<T>> = Vec::new();
let mut p = self;
let mut opt = Some(self);
loop {
// Stack parents and right children while left-descending
loop {
match p.right {
None => {}
Some(box ref n) => stack.push(n),
}
stack.push(p);
match p.left {
None => break,
Some(box ref n) => p = n,
}
}
// Visit the nodes with no right child
p = stack.pop().unwrap();
while !stack.is_empty() && p.right.is_none() {
res.push(p);
p = stack.pop().unwrap();
}
// First node that can potentially have a right child:
res.push(p);
if stack.is_empty() {
break;
} else {
p = stack.pop().unwrap();
while {
// Descend leftwards
while let Some(tree) = opt {
stack.push(tree);
opt = tree.left.as_deref();
}
!stack.is_empty()
} {
let tree = stack.pop().unwrap();
res.push(tree);
opt = tree.right.as_deref();
}
res
}