private void preOrder(BSTNode<T> tree)
// Initializes preOrderQueue with tree elements in preOrder order.
{
LinkedStack<BSTNode<T>> stack = new LinkedStack<BSTNode<T>>();
stack.push(root);
while(!stack.isEmpty())
{
BSTNode<T> parent = stack.top();
preOrderQueue.enqueue(parent.getInfo());
if(parent.getLeft() != null && parent.LeftVisited == false)
{
stack.push(parent.getLeft());
parent.LeftVisited = true;
}
else if(parent.getRight() != null && parent.RightVisited == false)
{
stack.push(parent.getRight());
parent.RightVisited = true;
}
else
{
stack.pop();
parent.resetVisitor();
}
}
}
Why is the parent of the node printed twice? For example, I put F,G,A,D,I,C,E,H and the output is F,B,A,B,D,C,D,E,D.

New Topic/Question
Reply



MultiQuote



|