|
| 1 | +//! Traverse a binary tree in in-order / pre-order / post-order. |
| 2 | +//! |
| 3 | +//! Type choices: |
| 4 | +//! - `val: i64` — Rust's 64-bit signed integer (the draft's `int64` isn't a |
| 5 | +//! real type name). |
| 6 | +//! - Children are `Option<Box<Node>>`, not `Option<&Node>`. A `&Node` only |
| 7 | +//! *borrows* a node owned elsewhere; a tree needs to *own* its children, and |
| 8 | +//! `Box` is an owning heap pointer. `Box` is also required for the type to |
| 9 | +//! exist at all: a `Node` holding a bare `Node` would be infinitely sized, |
| 10 | +//! and the fixed-size `Box` pointer breaks that recursion. |
| 11 | +//! - `Option` distinguishes an empty subtree (`None`) from a present child. |
| 12 | +
|
| 13 | +#[derive(Debug)] |
| 14 | +struct Node { |
| 15 | + val: i64, |
| 16 | + // `Box` owns the child on the heap; `Option` allows an absent subtree. |
| 17 | + left: Option<Box<Node>>, |
| 18 | + right: Option<Box<Node>>, |
| 19 | +} |
| 20 | + |
| 21 | +impl Node { |
| 22 | + fn leaf(val: i64) -> Box<Node> { |
| 23 | + Box::new(Node { |
| 24 | + val, |
| 25 | + left: None, |
| 26 | + right: None, |
| 27 | + }) |
| 28 | + } |
| 29 | + |
| 30 | + fn new(val: i64, left: Option<Box<Node>>, right: Option<Box<Node>>) -> Box<Node> { |
| 31 | + Box::new(Node { val, left, right }) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Root, then left subtree, then right subtree. |
| 36 | +fn pre_order(root: &Option<Box<Node>>, out: &mut Vec<i64>) { |
| 37 | + if let Some(node) = root { |
| 38 | + out.push(node.val); |
| 39 | + pre_order(&node.left, out); |
| 40 | + pre_order(&node.right, out); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Left subtree, then root, then right subtree. |
| 45 | +fn in_order(root: &Option<Box<Node>>, out: &mut Vec<i64>) { |
| 46 | + if let Some(node) = root { |
| 47 | + in_order(&node.left, out); |
| 48 | + out.push(node.val); |
| 49 | + in_order(&node.right, out); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// Left subtree, then right subtree, then root. |
| 54 | +fn post_order(root: &Option<Box<Node>>, out: &mut Vec<i64>) { |
| 55 | + if let Some(node) = root { |
| 56 | + post_order(&node.left, out); |
| 57 | + post_order(&node.right, out); |
| 58 | + out.push(node.val); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Small helper so callers don't have to allocate the output vector themselves. |
| 63 | +fn collect(root: &Option<Box<Node>>, order: fn(&Option<Box<Node>>, &mut Vec<i64>)) -> Vec<i64> { |
| 64 | + let mut out = Vec::new(); |
| 65 | + order(root, &mut out); |
| 66 | + out |
| 67 | +} |
| 68 | + |
| 69 | +fn main() { |
| 70 | + // 1 |
| 71 | + // / \ |
| 72 | + // 2 3 |
| 73 | + // / \ |
| 74 | + // 4 5 |
| 75 | + let tree = Some(Node::new( |
| 76 | + 1, |
| 77 | + Some(Node::new(2, Some(Node::leaf(4)), Some(Node::leaf(5)))), |
| 78 | + Some(Node::leaf(3)), |
| 79 | + )); |
| 80 | + |
| 81 | + println!("pre-order: {:?}", collect(&tree, pre_order)); |
| 82 | + println!("in-order: {:?}", collect(&tree, in_order)); |
| 83 | + println!("post-order: {:?}", collect(&tree, post_order)); |
| 84 | + |
| 85 | + let empty: Option<Box<Node>> = None; |
| 86 | + println!("empty: {:?}", collect(&empty, in_order)); |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + use super::*; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn empty_tree_yields_nothing() { |
| 95 | + let empty: Option<Box<Node>> = None; |
| 96 | + assert!(collect(&empty, pre_order).is_empty()); |
| 97 | + assert!(collect(&empty, in_order).is_empty()); |
| 98 | + assert!(collect(&empty, post_order).is_empty()); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn single_root() { |
| 103 | + let tree = Some(Node::leaf(1)); |
| 104 | + assert_eq!(collect(&tree, pre_order), [1]); |
| 105 | + assert_eq!(collect(&tree, in_order), [1]); |
| 106 | + assert_eq!(collect(&tree, post_order), [1]); |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn root_with_left_child() { |
| 111 | + let tree = Some(Node::new(1, Some(Node::leaf(2)), None)); |
| 112 | + assert_eq!(collect(&tree, pre_order), [1, 2]); |
| 113 | + assert_eq!(collect(&tree, in_order), [2, 1]); |
| 114 | + assert_eq!(collect(&tree, post_order), [2, 1]); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn root_with_right_child() { |
| 119 | + let tree = Some(Node::new(1, None, Some(Node::leaf(2)))); |
| 120 | + assert_eq!(collect(&tree, pre_order), [1, 2]); |
| 121 | + assert_eq!(collect(&tree, in_order), [1, 2]); |
| 122 | + assert_eq!(collect(&tree, post_order), [2, 1]); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn root_left_then_right() { |
| 127 | + // root -> left -> right |
| 128 | + let tree = Some(Node::new(1, Some(Node::new(2, None, Some(Node::leaf(3)))), None)); |
| 129 | + assert_eq!(collect(&tree, pre_order), [1, 2, 3]); |
| 130 | + assert_eq!(collect(&tree, in_order), [2, 3, 1]); |
| 131 | + assert_eq!(collect(&tree, post_order), [3, 2, 1]); |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn full_tree() { |
| 136 | + let tree = Some(Node::new( |
| 137 | + 1, |
| 138 | + Some(Node::new(2, Some(Node::leaf(4)), Some(Node::leaf(5)))), |
| 139 | + Some(Node::leaf(3)), |
| 140 | + )); |
| 141 | + assert_eq!(collect(&tree, pre_order), [1, 2, 4, 5, 3]); |
| 142 | + assert_eq!(collect(&tree, in_order), [4, 2, 5, 1, 3]); |
| 143 | + assert_eq!(collect(&tree, post_order), [4, 5, 2, 3, 1]); |
| 144 | + } |
| 145 | +} |
0 commit comments