From cfb4a58202842b208bb9f24dfbd6cd71ad350a13 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Mon, 11 Nov 2019 11:33:40 -0600 Subject: [PATCH] Looked up how to do iterative traversal with a stack --- BST.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/BST.java b/BST.java index 75be4c6..f6ed692 100644 --- a/BST.java +++ b/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 stack = new Stack(); 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