小蚂蚁学习数据结构(12)——二叉树的遍历代码实现

    主要仍是运用了递归的方法。
函数

# 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

相关文章
相关标签/搜索