Understanding the non-recursive implementation of binary trees from the program stack

The recursive implementation of binary tree traversal implements the pre-order, and the subsequent, middle-order traversal still looks very clear,
such as the recursive implementation of the pre-order traversal
void printdata(tree* t)
{ if t == 0 return; print(t->data); printdata(t->left); printdata(t->right); }




The implementation of non-recursive methods is generally carried out by using the stack. If you follow the calling method of the program stack, it can be implemented completely, but the space occupied by the stack frame is large. Generally, such operations are simulated Ways, such as pre-order traversal, push the initial node in, and then continue pop operation, when pop, access the node data, and then push the right and left nodes,
but for the post-order traversal, you need to use the marking method, the following The link is well written,
"Non-recursive traversal of binary tree (pre-order, middle-order, post-order non-recursive C language)", let’s watch https://blog.csdn.net/sinat_43009982/article/details/83343229

The main idea is to optimize the processing of the program stack through better technical methods, but some are not easy to understand. From the execution process of the program stack, we can understand these implementation skills well.

Guess you like

Origin blog.csdn.net/aaajj/article/details/107309941