Browse Source

Looked up how to do iterative traversal with a stack

master
Raphael Roberts 6 years ago
parent
commit
cfb4a58202
  1. 15
      BST.java

15
BST.java

@ -65,17 +65,18 @@ public class BST {
public String inOrder() {
// TODO: implement this method using a non-recursive solution
String return_string = "";
BSTNode[] stack = new BSTNode[size()];
int stack_pointer = 0;
Stack<BSTNode> stack = new Stack<BSTNode>();
BSTNode current = m_root;
while (true) {
while (current.getLeft() != null) {
stack[stack_pointer] = current;
stack_pointer++;
while ((current != null) || (stack.size() > 0)) {
while (current != null) {
stack.push(current);
current = current.getLeft();
}
current = stack.pop();
return_string += current.getInfo() + " ";
current = current.getRight();
}
return return_string; // replace this statement with your own return
return return_string;
}
// This method returns the smallest element in the binary search tree. You

Loading…
Cancel
Save