主要仍是運用了遞歸的方法。
函數
# include <stdio.h> # include <malloc.h> //二叉樹結構體 struct BTNode { //存放數據 char data; //存放左孩子的指針 struct BTNode * Lchild; //存放右孩子的指針 struct BTNode * Rchild; }; //函數的前置聲明 struct BTNode * createtree(void); //先序遍歷函數 void pre_order(struct BTNode *); //中序遍歷函數 void middle_order(struct BTNode *); //後序遍歷函數 void last_order(struct BTNode *); void last_order(struct BTNode * pTree) { if( NULL != pTree -> Lchild ) { middle_order( pTree -> Lchild ); } if( NULL != pTree -> Rchild ) { middle_order( pTree -> Rchild ); } printf("%c\n",pTree -> data); } void middle_order(struct BTNode * pTree) { if( NULL != pTree -> Lchild ) { middle_order( pTree -> Lchild ); } printf("%c\n",pTree -> data); if( NULL != pTree -> Rchild ) { middle_order( pTree -> Rchild ); } } void pre_order(struct BTNode * pTree) { printf("%c\n",pTree -> data); if( NULL != pTree -> Lchild ) { pre_order( pTree -> Lchild ); } if( NULL != pTree -> Rchild ) { pre_order( pTree -> Rchild ); } }
學PHP的小螞蟻 博客 http://my.oschina.net/woshixiaomayi/blogspa