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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use super::node::Node;

/// Level order iterator struct.
/// A stack is used to hold all nodes from the current level.
/// When the stack is empty, try pushing the node of the next level.
pub struct LevelOrderIterator<'a, T: Clone> {
    root: Option<&'a Node<T>>,
    next_level: usize,
    stack: Vec<&'a Node<T>>,
}

impl<'a, T: Clone> LevelOrderIterator<'a, T> {
    pub fn new(root: Option<&'a Node<T>>) -> Self {
        let mut iter = Self {
          root,
          next_level: 0,
          stack: Vec::new(),
        };
        // push first level
        iter.push_next_level();
        iter
    }

    /// Recurse down the stack until we reach the current level.
    /// Push the nodes at that level
    fn push_level(&mut self, root: Option<&'a Node<T>>, level: usize, curr: usize) {
        if let Some(node) = root {
            if level == curr {
                self.stack.push(node);
            } else {
                self.push_level(node.right_child_opt(), level+1, curr);
                self.push_level(node.left_child_opt(), level+1, curr);
            }
        }
    }

    /// Push all of the node at the current level.
    fn push_next_level(&mut self) {
        self.push_level(self.root, 0, self.next_level);
        self.next_level += 1;
    }
}

/// Implement the level order iterator.
impl<'a, T: Clone> Iterator for LevelOrderIterator<'a, T> {
    type Item = &'a Node<T>;

    fn next(&mut self) -> Option<Self::Item> {
        // pop next node for current level.
        self.stack.pop().or_else(|| {
            // current level is finished.  Push next level.
            self.push_next_level();
            // If the stack is still empty, then we are finished.
            self.stack.pop()
        })
    }
}

/// In order iterator for the tree.
/// Since the nodes in the tree have parent references we can
/// walk up and down the tree and don't need a stack.
///
/// See details of the algorithm here: https://stackoverflow.com/questions/12850889/in-order-iterator-for-binary-tree
pub struct InOrderIterator<'a, T: Clone> {
    next: Option<&'a Node<T>>,
}

/// Find the left most node in the tree.
fn left_most<'a, T: Clone>(node: Option<&'a Node<T>>) -> Option<&'a Node<T>> {
    // The first node is the left most node in the tree.
    match node {
        Some(mut next) => {
            // find left most node from the root.
            while let Some(left) = next.left_child_opt() {
                next = left;
            }
            Some(next)
        },
        None => None,
    }
}

impl<'a, T: Clone> InOrderIterator<'a, T> {
    pub fn new(root: Option<&'a Node<T>>) -> Self {
        // The first node is the left most node in the tree.
        Self {
            next: left_most(root),
        }
    }
}

/// Implement the in order iterator.
impl<'a, T: Clone> Iterator for InOrderIterator<'a, T> {
    type Item = &'a Node<T>;

    fn next(&mut self) -> Option<Self::Item> {
        // Save the current next node to be returned.
        let curr_node = self.next;

        // Find the new next node.
        if let Some(mut node) = curr_node {
            // Check if we can walk right.
            if let Some(right) = node.right_child_opt() {
                // walk fully left.
                self.next = left_most(Some(right));
                return curr_node;
            }

            // walk up the tree.
            loop {
                // get parent.
                match node.parent_opt() {
                    None => {
                        // No parent.  We are back at root node, finished.
                        self.next = None;
                        return curr_node;
                    },
                    Some(parent) => {
                        // check if we are walking up from left-side
                        if parent.check_left_child(node) {
                            // The next node is the parent.
                            self.next = Some(parent);
                            return curr_node;
                        }
                        // when walking up from the right-side, keep going up.
                        node = parent;
                    },
                }
            }
        }
        curr_node
    }
}

/// Implement an in order iterator which allows for mutability of the
/// nodes inside the iterator
pub struct IterMut<'a, T: Clone> {
    stack: Vec<Option<*mut Node<T>>>,
    phantom: std::marker::PhantomData<&'a Node<T>>,
}

impl<'a, T: Clone> IterMut<'a, T> {
    pub fn new(root: Option<&'a mut Node<T>>) -> Self {
        let mut stack = Vec::new();
        // map mutable reference to raw pointer.
        stack.push(root.map(|n| n as *mut Node<T>));
        Self {
            stack,
            phantom: std::marker::PhantomData,
        }
    }
}

/// implement an in order iterator.
/// We have to use raw pointers and unsafe because the borrow checker
/// will not allow use to have more then one mutable reference to the same node.
impl<'a, T: Clone> Iterator for IterMut<'a, T> {
    type Item = &'a mut Node<T>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(mut curr_node) = self.stack.pop() {
            while let Some(curr) = curr_node {
                curr_node = unsafe { &mut *curr }.left_child_mut_ptr_opt();
                self.stack.push(Some(curr));
            }
        }
        self.stack.pop()?.map(|res_node| {
            let res_node = unsafe { &mut *res_node};
            self.stack.push(res_node.right_child_mut_ptr_opt());
            res_node
        })
    }
}

#[cfg(test)]
mod test {
    use crate::tree::*;

    #[test]
    fn level_order_iter() {
        //                     0
        //              ______/ \______
        //             /               \
        //            1                 2
        //        __/   \__          __/ \__
        //       /         \        /       \
        //      3           4      5         6
        //     / \         / \    / \       / \
        //    7   8       9  10  11  12   13  14
        //
        let mut nums = [7,3,8,1,9,4,10,0,11,5,12,2,13,6,14].iter().map(|n| Some(*n)).collect::<Vec<_>>();
        let tree = Tree::from_slice(&mut nums[..]);

        let root = tree.root_opt().expect("no root node");
        assert_eq!(root.get(), &0);
        let left = root.left_child_opt().expect("no left node");
        assert_eq!(left.get(), &1);
        let right = root.right_child_opt().expect("no right node");
        assert_eq!(right.get(), &2);

        for (i, n) in tree.level_order_iter().enumerate() {
            println!(" - level order[{}] = {}", i, n.get());
            assert_eq!(i, *n.get());
        }

        let mut iter = tree.level_order_iter().map(|n| n.get());
        assert_eq!(iter.next(), Some(&0));
        assert_eq!(iter.next(), Some(&1));
        assert_eq!(iter.next(), Some(&2));
        assert_eq!(iter.next(), Some(&3));
        assert_eq!(iter.next(), Some(&4));
        assert_eq!(iter.next(), Some(&5));
        assert_eq!(iter.next(), Some(&6));
    }

    #[test]
    fn in_order_iter() {
        let mut nums = (0..10).map(|n| Some(n)).collect::<Vec<_>>();
        let tree = Tree::from_slice(&mut nums[..]);
        println!("tree = {:?}", tree);
        for (i, n) in tree.in_order_iter().enumerate() {
            println!(" - tree[{}] = {}", i, n.get());
        }

        assert_eq!(tree.len(), 10);

        let mut iter = tree.in_order_iter().map(|n| &**n);
        assert_eq!(iter.next(), Some(&0));
        assert_eq!(iter.next(), Some(&1));
        assert_eq!(iter.next(), Some(&2));
        assert_eq!(iter.next(), Some(&3));
    }

    #[test]
    fn iter_mut() {
        let mut nums = (0..10).map(|n| Some(n)).collect::<Vec<_>>();
        let mut tree = Tree::from_slice(&mut nums[..]);
        println!("tree = {:?}", tree);
        for (i, n) in tree.in_order_iter().enumerate() {
            println!(" - tree[{}] = {}", i, n.get());
        }

        assert_eq!(tree.len(), 10);

        let mut iter = tree.iter_mut().map(|n| n.get_mut());
        assert_eq!(iter.next(), Some(&mut 0));
        assert_eq!(iter.next(), Some(&mut 1));
        assert_eq!(iter.next(), Some(&mut 2));
        assert_eq!(iter.next(), Some(&mut 3));

        for n in tree.iter_mut() {
            *n.get_mut() *= 2;
        }

        println!("tree = {:?}", tree);
        for (i, n) in tree.in_order_iter().enumerate() {
            println!(" - tree[{}] = {}", i, n.get());
        }

        let mut iter = tree.iter_mut().map(|n| n.get_mut());
        assert_eq!(iter.next(), Some(&mut 0));
        assert_eq!(iter.next(), Some(&mut 2));
        assert_eq!(iter.next(), Some(&mut 4));
        assert_eq!(iter.next(), Some(&mut 6));
    }
}