C data structure and algorithm - based finishing - Tree 05: Schematic - order traversal of the tree

图解树的后序遍历递归执行过程

This chapter is based on the following sections:

C data structures and algorithms - the basis of consolidation - Tree-02: the establishment of binary tree traversal and Different

C data structures and algorithms - the basis of consolidation - Tree-03: Schematic - order traversal through in-depth understanding of the principles of recursively

C data structure and algorithm - based finishing - Tree 04: Schematic - preorder tree

Postorder recursive function execution process illustrated

code show as below:

//后序遍历,递归实现
void PostorderTraversal(BinTree BT)
{
    if (BT == NULL) return;
    PostorderTraversal(BT->Left);
    PostorderTraversal(BT->Right);
    printf(" %c", BT->data);
    return;
}

Recursively process diagram:

Original binary tree:

1. The first step calls PostorderTraversal, execution PostorderTraversal (BT-> Left), is not empty, began to call the second step (B position at this time, but did not print), started PostorderTraversal (BT-> Left), not is empty, beginning with the third step (D position at this time, but not printed), started PostorderTraversal (BT-> Left), is not empty, the fourth step begins with (in this case the H position, but not Print), started PostorderTraversal (BT-> Left), is empty, return immediately started PostorderTraversal (BT-> Right), is empty, return immediately begin printing H nodes, the nodes H traverse completed.

2. The fourth step print node, finished, return to the third step, the third step performed at this time PostorderTraversal (BT-> Right), since empty, return immediately, returns right child, printing operation is performed junction points D, traversing the node D is completed.

3.第三步打印完成后,执行完毕,返回至第二步,开始执行PostorderTraversal(BT->Right),非空,开始第三步调用(此时在E位置,但并未打印),执行PostorderTraversal(BT->Left),非空,立即返回,开始执行PostorderTraversal(BT->Right),非空,执行第四步调用(此时在 I 位置,但并未打印),执行ostorderTraversal(BT->Left),为空,立即返回,开始执行PostorderTraversal(BT->Right),为空,立即返回,开始执行打印节点 I  ,结点 I 遍历完成。

4.第四步打印完成后,返回第三步,右孩子返回,开始执行打印结点E,结点E遍历完成。

5.第三步打印完成后,返回至第二步,右孩子返回,打印结点B,结点B遍历完成。

6.第二步打印完成后,返回至第一步,开始执行PostorderTraversal(BT->Right),非空,开始第二步调用(此时在C位置,但并未打印),执行PostorderTraversal(BT->Left),非空,开始第三步调用(此时在F位置,但并未打印),开始执行PostorderTraversal(BT->Left),为空,立即返回,开始执行PostorderTraversal(BT->Right),为空,立即返回右孩子返回,开始执行打印结点F,结点F遍历完成。

7.第三步打印结点后,返回至第二步,左孩子返回,开始访问右孩子,开始执行PostorderTraversal(BT->Right),非空,开始第三步调用(此时在G位置,但并未打印),开始执行PostorderTraversal(BT->Left),为空,返回,开始执行PostorderTraversal(BT->Right),非空,开始第四步调用(此时在J位置,但并未打印),执行PostorderTraversal(BT->Left),为空,返回,开始执行PostorderTraversal(BT->Right),为空,返回,右孩子返回,开始打印结点J,结点J遍历完成。

8.第四步打印完成后,返回至第三步,右孩子返回,执行打印结点G,结点G遍历完成。

9.第三步打印完后,返回至第二步,右孩子返回,开始打印结点C,结点C遍历完成。

10.第二步打印完后,返回至第一步,右孩子返回,打印结点A,第一步执行完毕,递归过程结束。

 

遍历顺序:H->D->I->E->B->F->J->G->C->A

 

 

 

感悟:

1.第一次调用访问左孩子,从左孩子返回接着访问右孩子,从右孩子返回就打印自身,打印完后就返回。

2.与栈来实现后序遍历密不可分,与该节点第几次访问离不开关系,第一次访问就继续访问左孩子,第二次访问说明是从左孩子返回,那就接着访问右孩子,第三次访问说明是从右孩子返回,访问自身。

3.后序遍历实际上就是把孩子全部遍历完了再去遍历父结点。

 

 

 

此章结束。

 

 

 

发布了19 篇原创文章 · 获赞 7 · 访问量 420

Guess you like

Origin blog.csdn.net/ATFWUS/article/details/104277718