C data structure and algorithm - based finishing - Tree 04: Schematic - preorder 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

After understanding this chapter See also:

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

Inorder traversal of Graphical function recursively

code show as below:

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

Recursively process diagram:

Original binary tree:

1. The first step calls InorderTraversal, since the root node is not empty, started InorderTraversal (BT-> Left), since, BT-> Left is not empty, the second step starts call (in this case the node B location, but No printing), started InorderTraversal (BT-> Left), beginning with the third step (in this case the position of the node D, but not printed), started InorderTraversal (BT-> Left), the fourth step begins with (now in the position of the nodes H, but not printed), started InorderTraversal (BT-> Left), this time BT-> Left empty, return immediately, followed by a fourth step performs printing statements, printing of the nodes H, complete traversal of the nodes H. (Step number representative of the number of calls in the figure)

2. The fourth step print statement is finished, started InorderTraversal (BT-> Right), BT-> Right is empty, and immediately return to the fourth step is finished, return to the third step, the third step started printing knot points D, the node D traversal completion.

3. The third step is performed after the completion of a print statement, started InorderTraversal (BT-> Right), since BT-> Right is empty, returns immediately, the third step is finished, return to the second step, the node B begin printing , node B traversal completion.

 

 

 

 

 

 

4.第二步完成打印后开始执行 InorderTraversal(BT->Right),BT->Right 不为空,开始执行第三步调用(此时在结点E位置处,但并未打印),开始执行InorderTraversal(BT->Left),由于BT->Left 为空,立刻返回,开始执行打印结点E,结点E遍历完成。

5.第三步执行完打印后,开始执行 InorderTraversal(BT->Right),由于非空,开始第四步调用(此时在结点 I 位置处,但并未打印),开始执行InorderTraversal(BT->Left),为空,立刻返回,开始执行打印结点 I ,结点 I 遍历完成。

6.第四步执行完打印后,开始执行 InorderTraversal(BT->Right),非空,立刻返回,第四步执行完毕,返回至第三步,此前第三步已完成执行InorderTraversal(BT->Right),所以第三步执行完毕,返回至第二步,因为从右孩子返回,第二步也执行完毕,返回至第一步,从左孩子返回,开始打印结点A,结点A遍历完成。

7.第一步执行完打印后,开始执行InorderTraversal(BT->Right),不为空,开始执行第二步调用(此时在结点C位置,但并未打印),开始执行InorderTraversal(BT->Left),不为空,开始第三步调用(此时在F位置,但并未打印),开始执行InorderTraversal(BT->Left),为空,立刻返回,开始打印结点F,结点F遍历完成。

8.第三步打印完后开始执行InorderTraversal(BT->Right),为空,立刻返回,第三步执行完毕,从左孩子返回,开始打印结点C,结点C遍历完成。

9.第二步打印完后,开始执行InorderTraversal(BT->Right),不为空,开始第三步调用(此时在结点G位置,但并未打印),执行InorderTraversal(BT->Left),为空,立刻返回,从左孩子返回,执行打印结点G,结点G遍历完成。

10.第三步打印完后,开始执行InorderTraversal(BT->Right),非空,开始第四步调用(此时在结点J位置,但并未打印),执行InorderTraversal(BT->Left),为空,立刻返回,从左孩子返回,执行打印结点J,结点J遍历完成。

11.第四步打印完后,开始执行InorderTraversal(BT->Right),为空,立刻返回,第四步执行完毕,返回至第三步,从右孩子返回,第三步执行完毕,返回至第二步,从右孩子返回,第二步执行完毕,返回至第一步,从右孩子返回,第一步结束,整个递归过程完成。

 

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

 

感悟:

1.中序遍历不像前序遍历一样调用到哪里就打印哪里,它要一直往左调用,从左孩子返回就打印,从右孩子返回就结束该步,返回至上一步,一直到第一步执行完毕。

2.可以这样理解,从左子树为空返回的先打印,左子树执行完毕返回后打印,右子树为空就一直向上返回。

3.在理解了这种遍历顺序后,就很好理解如何用栈去实现了,栈中保留自己和自己的右孩子都未访问过的元素,正好与从左孩子返回就打印,右孩子返回就执行完毕的递归原理相对应。

 

 

此章结束。

 

 

 

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

Guess you like

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