public static void nonRecursiveInorder(Node node)
{
Stack<Node> st=new Stack();
st.push(node);
Node temp;
while(!st.isEmpty())
{
temp=st.peek();
if(temp.left!=null && temp.left.visited==false)
st.push(temp.left);
else
{
if(temp.left==null && temp.right!=null && temp.right.visited==false)
{
System.out.print(" "+temp.value);
temp.visited=true;
st.pop();
st.push(temp.right);
}
else
{
if(temp.left!=null && temp.left.visited==true && temp.right!=null && temp.right.visited==false)
{
System.out.print(" "+temp.value);
temp.visited=true;
st.pop();
st.push(temp.right);
}
else
{
System.out.print(" "+temp.value);
temp.visited=true;
st.pop();
}
}
}
}
}
sbharathind
0
Newbie Poster
Recommended Answers
Jump to PostYour algorithm is more complex than it should be, compare with the following:
iterativeInorder(node) parentStack = empty stack while not parentStack.isEmpty() or node != null if node != null then parentStack.push(node) node = node.left else node = parentStack.pop() visit(node) node = node.right
Jump to Post``You use class Stack, this is a legacy datastructure in Java, and it extends from Vector …
All 5 Replies
sbharathind
0
Newbie Poster
mvmalderen
2,072
Postaholic
devinnrock
-3
Newbie Poster
mvmalderen
commented:
Signature spammer.
-3
mvmalderen
2,072
Postaholic
sbharathind
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.