"#" symbol as empty child
(vec: &[&'a str])
| 63 | |
| 64 | // "#" symbol as empty child |
| 65 | fn expand_sharp<'a>(vec: &[&'a str]) -> Vec<&'a str> { |
| 66 | let mut results = Vec::new(); |
| 67 | |
| 68 | for &v in vec.iter() { |
| 69 | // root |
| 70 | if results.is_empty() { |
| 71 | results.push(v); |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | // child |
| 76 | if v == "#" { |
| 77 | results.push(v); |
| 78 | } else { |
| 79 | // child |
| 80 | loop { |
| 81 | // new child idx |
| 82 | let idx = results.len(); |
| 83 | let parent = results[binary_tree::parent(idx)]; |
| 84 | if parent == "#" { |
| 85 | // if parent is "#", children are "#", |
| 86 | // so just push "#" |
| 87 | results.push(parent); |
| 88 | } else { |
| 89 | results.push(v); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | results |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn t_expand_sharp() { |