1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/// Binary Tree
///
/// # Examples
///
/// ```
/// use structures::tree::{TraverseOrder, Tree};
///
/// let tree = Tree::branch(1, Tree::leaf(2), Tree::leaf(3));
///
/// println!("{:#?}", tree);
///
/// println!("{:?}", tree.iter(TraverseOrder::InOrder).collect::<Vec<_>>());
/// ```
#[derive(Debug, PartialEq)]
pub enum Tree<T> {
  Empty,
  Branch(T, Box<Self>, Box<Self>),
}

impl<T> Tree<T> {
  pub fn empty() -> Self {
    Tree::Empty
  }

  pub fn leaf(v: T) -> Self {
    Tree::branch(v, Tree::Empty, Tree::Empty)
  }

  pub fn branch(v: T, l: Self, r: Self) -> Self {
    Tree::Branch(v, Box::new(l), Box::new(r))
  }

  pub fn left(&self) -> Option<&Self> {
    match self {
      Tree::Empty => None,
      Tree::Branch(_, l, _) => Some(&l),
    }
  }

  pub fn right(&self) -> Option<&Self> {
    match self {
      Tree::Empty => None,
      Tree::Branch(_, _, r) => Some(&r),
    }
  }

  pub fn value(&self) -> Option<&T> {
    match self {
      Tree::Empty => None,
      Tree::Branch(v, _, _) => Some(&v),
    }
  }

  pub fn iter(&self, order: TraverseOrder) -> impl Iterator<Item = &T> {
    use {IterState::*, TraverseOrder::*, Tree::*};
    enum IterState<'a, T> {
      T(&'a Tree<T>),
      V(&'a T),
    }
    let mut stack = vec![IterState::T(self)];
    std::iter::from_fn(move || {
      while let Some(state) = stack.pop() {
        match state {
          T(Empty) => {}
          T(Branch(v, l, r)) => {
            if let PostOrder = order {
              stack.push(V(v))
            }
            stack.push(T(r));
            if let InOrder = order {
              stack.push(V(v))
            }
            stack.push(T(l));
            if let PreOrder = order {
              stack.push(V(v))
            }
          }
          V(v) => {
            return Some(v);
          }
        }
      }
      None
    })
  }

  pub fn traverse(&self, order: TraverseOrder, f: &mut dyn FnMut(&T)) {
    use {TraverseOrder::*, Tree::*};
    match self {
      Empty => {}
      Branch(v, l, r) => {
        if let PreOrder = order {
          f(v)
        }
        l.traverse(order, f);
        if let InOrder = order {
          f(v)
        }
        r.traverse(order, f);
        if let PostOrder = order {
          f(v)
        }
      }
    }
  }
}

#[derive(Clone, Copy)]
pub enum TraverseOrder {
  InOrder,
  PreOrder,
  PostOrder,
}

#[cfg(test)]
mod tests {
  use super::Tree;

  #[test]
  fn left() {
    assert_eq!(Tree::<()>::empty().left(), None);
    assert_eq!(Tree::branch(1, new_tree(), Tree::empty()).left(), Some(&new_tree()));
  }

  #[test]
  fn right() {
    assert_eq!(Tree::<()>::empty().right(), None);
    assert_eq!(Tree::branch(1, Tree::empty(), new_tree()).right(), Some(&new_tree()));
  }

  #[test]
  fn value() {
    assert_eq!(Tree::<()>::empty().value(), None);
    assert_eq!(Tree::leaf(1).value(), Some(&1));
  }

  #[test]
  fn iter() {
    use super::TraverseOrder::*;
    let h = |o| new_tree().iter(o).cloned().collect::<Vec<_>>();
    assert_eq!(h(InOrder), [4, 2, 5, 1, 6, 3, 7]);
    assert_eq!(h(PreOrder), [1, 2, 4, 5, 3, 6, 7]);
    assert_eq!(h(PostOrder), [4, 5, 2, 6, 7, 3, 1]);
  }

  #[test]
  fn traverse() {
    use super::TraverseOrder::*;
    let h = |o| {
      let mut rs = vec![];
      new_tree().traverse(o, &mut |&x| rs.push(x));
      rs
    };
    assert_eq!(h(InOrder), [4, 2, 5, 1, 6, 3, 7]);
    assert_eq!(h(PreOrder), [1, 2, 4, 5, 3, 6, 7]);
    assert_eq!(h(PostOrder), [4, 5, 2, 6, 7, 3, 1]);
  }

  fn new_tree() -> Tree<i32> {
    Tree::branch(1, Tree::branch(2, Tree::leaf(4), Tree::leaf(5)), Tree::branch(3, Tree::leaf(6), Tree::leaf(7)))
  }
}