头文件及功能函数见【二叉树算法库】
代码:
- #include "btree.h"
-
- void PreOrder(BTNode *b)
- {
- if (b!=NULL)
- {
- printf("%c ",b->data);
- PreOrder(b->lchild);
- PreOrder(b->rchild);
- }
- }
-
- void InOrder(BTNode *b)
- {
- if (b!=NULL)
- {
- InOrder(b->lchild);
- printf("%c ",b->data);
- InOrder(b->rchild);
- }
- }
-
- void PostOrder(BTNode *b)
- {
- if (b!=NULL)
- {
- PostOrder(b->lchild);
- PostOrder(b->rchild);
- printf("%c ",b->data);
- }
- }
-
- int main()
- {
- BTNode *b;
- CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
- printf("二叉树b:");
- DispBTNode(b);
- printf("\n");
- printf("先序遍历序列:\n");
- PreOrder(b);
- printf("\n");
- printf("中序遍历序列:\n");
- InOrder(b);
- printf("\n");
- printf("后序遍历序列:\n");
- PostOrder(b);
- printf("\n");
- DestroyBTNode(b);
- return 0;
- }
运行结果:
知识点总结:
二叉树的先序,中序,后序遍历的递归算法的实现。
学习心得:
要理解二叉树的先序,中序,后序遍历,可以自己在纸上写一下,更好理解。