The iterative (non-recursive) function preorderTreeTraverse() can perform preorder traversing of a binary tree with the help of a stack that can hold pointers to nodes of the tree.
Also, the iterative (non-recursive) function layerTreeTraverse() can perform level-order traversing of a binary tree with the help of a queue that can hold pointers to nodes of the tree.
void
preorderTreeTraverse (link h, void visit (link)) {
STACK<link> s (max);
s.push (h);
while (!s.empty ()) {
visit (h = s.pop ());
if (h->r != 0) s.push (h->r);
if (h->l != 0) s.push (h->l);
}
}
void
layerTreeTraverse (link h, void visit (link)) {
QUEUE<link> q (max);
q.put (h);
while (!q.empty ()) {
visit (h = q.get ());
if (h->l != 0) q.put (h->l);
if (h->r != 0) q.put (h->r);
}
}


