I have a B-Tree that I can currently iterate over using recursive inorder traversal. However, I need to iterate inorder NOT recursively. I have tried all kinds of things and the closest I got was using depth-first-search. I am successfully printing all the elements in the tree however not in the correct order. I do understand why its not in sorted order but I don't understand how I can fix it.. This is what I got:
public void iterative() {
Stack<BTreeNode<T>> stack = new Stack<>();
stack.push(this);
while(!stack.empty()) {
BTreeNode<T> curr = stack.pop();
int i;
for (i = 0; i < curr.numNodes; i++) {
if (!curr.isLeaf) {
stack.push(curr.children[i]);
}
System.out.println(curr.keys[i].toString());
}
if (!curr.isLeaf) {
stack.push(curr.children[i]);
}
}
}
```
What am I missing here?