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

通过树的递归实现前序遍历的例子,深入理解递归的执行原理

This chapter built on the basis of what chapters:

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

After understanding this chapter See also:

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

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

Recursive function performs a preorder traversal of Graphical

code show as below:

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

Recursively illustrating the principles of:

Original binary tree:

1. The first step calls PreorderTraversal, prints the node A, A node traversal completion.

 2. The first step calls the print node, then performs PreorderTraversal (BT-> Left), starts calling the second step, since not empty, the print node B, node B traversal completion.

 3. The second step calls the print node, then performs PreorderTraversal (BT-> Left), beginning with the third step, since not empty, the print node D, node D traversal completion.

 4. Third step After printing the node, then performs PreorderTraversal (BT-> Left), beginning with the fourth step, since not empty, the print node H, node H traversal completion.

 5. Fourth step After printing the node, then call PreorderTraversal (BT-> Left), beginning with the fifth step, as is empty, it returns to the calling PreorderTraversal fourth step (BT-> Left), the call start PreorderTraversal (BT-> Right), since BT-> Right is empty, with immediate return to the fourth step, the fourth step by the end of this time, returns to the third step. The third step started PreorderTraversal (BT-> Right), due to the BT-> Right is empty, returns immediately, also finished third step, the second step is returned to the calling, the second step started PreorderTraversal (BT-> Right) start the third step with, since not empty, the node E begin printing, E node traversal completion.

 6.新的第三步调用打印结点后,开始执行PreorderTraversal(BT->Left),由于BT->Left 为空,立刻返回至第三步,第三步开始执行PreorderTraversal(BT->Right),开始执行第四步调用,由于不为空,打印 I 结点,I 结点遍历完成。

 7.第四步打印结点后,开始执行PreorderTraversal(BT->Left),由于BT->Left为空,返回至第三步,这个第四步执行完毕,此时第三步执行到函数末尾,执第三步行完毕,返回至第二步,此时的第二步也执行到末尾,第二步执行完毕,返回至第一步,此时第一步已经调用了PreorderTraversal(BT->Left)打印了B结点,开始执行PreorderTraversal(BT->Right),新的第二步调用,由于BT->Right不为空,开始打印C结点,C结点遍历完成。

8.新的第二步调用打印结点后,开始执行PreorderTraversal(BT->Left),开始第三步调用,由于不为空,开始打印结点F,F结点遍历完成。

9.第三步调用打印完毕后,开始执行PreorderTraversal(BT->Left),由于为空,立刻返回,开始执行PreorderTraversal(BT->Right),由于为空。立刻返回,第三步执行到末尾,返回至第二步,第二步开始执行PreorderTraversal(BT->Right),开始新的第三步调用,由于BT->Right 不为空,开始打印G,结点G遍历完成。

10.新的第三步打印结点后,开始执行PreorderTraversal(BT->Left),由于BT->Left为空,开始执行PreorderTraversal(BT->Right),开始执行第四步调用,由于不为空,执行打印结点J,结点J遍历完成。

11.第四步执行完打印后,开始执行PreorderTraversal(BT->Left),由于BT->Left为空,立刻返回,开始执行PreorderTraversal(BT->Right),由于BT->Right为空,立刻返回,此时第四步执行完毕,返回至第三步,第三步已经调用了PreorderTraversal(BT->Right)打印了J,所以第三步执行完毕,返回至第二步,第二步已经调用了PreorderTraversal(BT->Right)打印了G,所以第二步执行完毕,返回至第一步,此时第一步已经调用了PreorderTraversal(BT->Right)打印了C,第一步调用结束,整个递归过程也随之结束,遍历完了所以结点。

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

感悟:

1.递归的实质就是不断调用自身,来实现某些功能,递归的结束标志就是第一次函数调用结束。

2.在明白原理后,思考理解就不要一直想象它的具体调用过程,只需要看在哪里执行遍历,在哪里调用自身,一直往下调用的结束条件是什么,如何才会完成第一步调用。

3.关于树的调用次数问题,可以直接看树的层数,可以这样理解,当前是第几层,就是第几次在调用,不过有着先左后右的顺序。

6.树的前序遍历的递归原理其实就是深度优先搜索的原理:一个结点有一条路就走一条路,等所有的路走完了,就返回至上一个结点。

 

 

 

此章结束。

 

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

Guess you like

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