Skip to content

Commit

Permalink
Improve iterator performance 🏁 (#39)
Browse files Browse the repository at this point in the history
* fix iterator

* fix typing

* undo package lock changes
  • Loading branch information
danmunson authored Apr 21, 2023
1 parent f9a8205 commit d016cc7
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,22 @@ export default class Tree<Key=number, Value=any> {
}

*[Symbol.iterator]() {
let n = this.minNode();
while (n) {
yield n;
n = this.next(n);
let current = this._root;
const Q: Node<Key, Value>[] = []; /* Initialize stack s */
let done = false;

while (!done) {
if (current !== null) {
Q.push(current);
current = current.left;
} else {
if (Q.length !== 0) {
current = Q.pop()!;
yield current;

current = current.right;
} else done = true;
}
}
}
}
Expand Down

0 comments on commit d016cc7

Please sign in to comment.