voidTree_printTree_postorder_way2(pTree tree){if(tree ==NULL){return;}// stack stucture implement
pTree stack[10];int stackTop =-1;
pTree pMove = tree;
pTree pNodeVisited =NULL;while(pMove){// Locating the bottom of the left
stack[++stackTop]= pMove;// Saving the path
pMove = pMove->L;}while(stackTop !=-1){
pMove = stack[stackTop];// backing one
stackTop--;// popif(pMove->R ==NULL|| pMove->R == pNodeVisited){// Right node of curent nodeprintf("%c\t", pMove->dat);
pNodeVisited = pMove;}else{
stack[++stackTop]= pMove;
pMove = pMove->R;while(pMove){
stack[++stackTop]= pMove;
pMove = pMove->L;}}}}
4 测试用例
intmain(int argc,char*argv[]){
pTree A =Tree_createNode('A');
pTree B =Tree_createNode('B');
pTree C =Tree_createNode('C');
pTree D =Tree_createNode('D');
pTree E =Tree_createNode('E');
pTree F =Tree_createNode('F');
pTree G =Tree_createNode('G');
pTree H =Tree_createNode('H');Tree_insertNode(A, B, C);Tree_insertNode(B, D,NULL);Tree_insertNode(D, G,NULL);Tree_insertNode(C, E, F);Tree_insertNode(E,NULL, H);printf("root is in the front(recursive): \r\n");Tree_printTree_preorder(A);printf("\r\n");printf("root is in the front(non-recursive): \r\n");Tree_printTree_preorder_way2(A);printf("\r\n");printf("root is in the middle(recursive): \r\n");Tree_printTree_inorder(A);printf("\r\n");printf("root is in the middle(non-recursive): \r\n");Tree_printTree_inorder_way2(A);printf("\r\n");printf("root is in the back(recursive): \r\n");Tree_printTree_postorder(A);printf("\r\n");printf("root is in the back(non-recursive): \r\n");Tree_printTree_postorder_way2(A);printf("\r\n");return0;}