diff --git a/sorted_set/set.mbt b/sorted_set/set.mbt index 6f8b41a83..578047273 100644 --- a/sorted_set/set.mbt +++ b/sorted_set/set.mbt @@ -348,29 +348,21 @@ fn each[V](self : Node[V], f : (V) -> Unit) -> Unit { ///| /// Iterates the set with index. pub fn eachi[V](self : T[V], f : (Int, V) -> Unit) -> Unit { - match self.root { - None => () - Some(root) => root.eachi(f) - } -} - -///| -fn eachi[V](self : Node[V], f : (Int, V) -> Unit) -> Unit { - let s = [] - let mut p = Some(self) let mut i = 0 - while not(p.is_empty()) || not(s.is_empty()) { - while not(p.is_empty()) { - s.push(p) - p = p.unwrap().left - } - if not(s.is_empty()) { - p = s.unsafe_pop() - f(i, p.unwrap().value) - p = p.unwrap().right - i += 1 + fn dfs(root : Node[V]?) -> Unit { + match root { + Some(root) => { + dfs(root.left) + f(i, root.value) + i = i + 1 + dfs(root.right) + } + None => () } + () } + + dfs(self.root) } ///|