diff --git a/lib/src/red_black_tree.rs b/lib/src/red_black_tree.rs index 6d2eca879..278c52252 100644 --- a/lib/src/red_black_tree.rs +++ b/lib/src/red_black_tree.rs @@ -54,15 +54,11 @@ impl<'a, V: TreeValue> RedBlackTreeReadOnly<'a, V> { } } - /// Sorted iterator starting from the max. + /// Sorted iterator starting from the min. pub fn iter(&self) -> RedBlackTreeReadOnlyIterator { RedBlackTreeReadOnlyIterator { tree: self, - index: if self.max_index == NIL { - self.get_max_index() - } else { - self.max_index - }, + index: self.get_min_index::(), } } } @@ -968,15 +964,12 @@ impl<'a, V: TreeValue> RedBlackTree<'a, V> { } } - /// Sorted iterator starting from the max. + // TODO: Go from max to min because that would be cheaper as we already store max + /// Sorted iterator starting from the min. pub fn iter(&self) -> RedBlackTreeIterator { RedBlackTreeIterator { tree: self, - index: if self.max_index == NIL { - self.get_max_index() - } else { - self.max_index - }, + index: self.get_min_index::(), } } @@ -1105,12 +1098,12 @@ impl<'a, V: TreeValue> Iterator for RedBlackTreeIterator<'a, V> { fn next(&mut self) -> Option { let index: DataIndex = self.index; - let predecessor_index: DataIndex = self.tree.get_predecessor_index::(self.index); + let successor_index: DataIndex = self.tree.get_successor_index::(self.index); if index == NIL { None } else { let result: &RBNode = get_helper::>(self.tree.data, index); - self.index = predecessor_index; + self.index = successor_index; Some((index, result)) } } @@ -1126,12 +1119,12 @@ impl<'a, V: TreeValue> Iterator for RedBlackTreeReadOnlyIterator<'a, V> { fn next(&mut self) -> Option { let index: DataIndex = self.index; - let predecessor_index: DataIndex = self.tree.get_predecessor_index::(self.index); + let successor_index: DataIndex = self.tree.get_successor_index::(self.index); if index == NIL { None } else { let result: &RBNode = get_helper::>(self.tree.data, index); - self.index = predecessor_index; + self.index = successor_index; Some((index, result)) } }