二叉树的简单操作(前中后序遍历,层次遍历,树高,叶子数,交换二叉树等)

//程序名:tree_define.h
//      程序功能:实现二叉树基本操作(包括建立,前中后序遍历,树高等操作的实现)
//          作者:吴雨羲
//          日期:2013.9.30
//          版本:1.0
//      修改内容:无
//      修改日期:
//      修改作者:
//
#include<iostream>
using namespace std;
//定义二叉树结点结构
struct BTNode
{
	char data;
	BTNode *lc;
	BTNode *rc;//左右孩子指针
	BTNode():lc(NULL),rc(NULL){}
	BTNode(char x):data(x),lc(NULL),rc(NULL){}
	
};
//定义非递归后序遍历结点结构
struct node
{
	BTNode *t;
	bool flag;//判断数据进栈次数 第二次为真输出数据
};
class BT
{
public:
	BT(){root=NULL;}//定义构造函数 
	~BT(){}//析构函数
	void CreateBT(char chr[]);// 建立二叉树函数
    void N_preOrder();// 前序非递归遍历函数
    void N_inOrder();// 中序非递归遍历函数
	void N_postOrder();// 后序非递归遍历函数
    void levelOrder();//层次遍历函数
	void CheckRoot()//检验根结点
	{
		if (root==NULL)
			cout<<"   请先建立二叉树!!!!!!!!"<<endl;
	}

	void preOrder()// 前序递归遍历函数 (以下函数利用公有函数重载私有函数消除参数)
	{ 
		preOrder(root);
	}
	void inOrder()// 中序递归遍历函数
	{ 
		inOrder(root);
	}	
	void postOrder()// 后序递归遍历函数
	{
		postOrder(root);
	}

	//
	// 二叉树叶子总数函数
	// 函数功能:求二叉树叶子总数
	//函数参数:无
	//
	//参数返回值:c 叶子总数
	int LeafCount()
	{
		int c=0;
		LeafCount(root,c);
		return c;
	}
	int Height()// 二叉树深度函数 
	{
		return Height(root);
	}
	void ChangeChild()// 交换左右子树函数
	{
		ChangeChild(root);
	}
	void PrintGenlist()// 输出二叉树函数
	{
		PrintGenlist(root);
	}
	
private:
	BTNode*root;//	根结点
	void LeafCount(BTNode*r,int&c);// 二叉树叶子总数函数
	void preOrder(BTNode*r);// 前序递归遍历函数
	void inOrder(BTNode*r);// 中序递归遍历函数
	void postOrder(BTNode*r);// 后序递归遍历函数
	void PrintGenlist(BTNode*r);// 输出二叉树函数
	void ChangeChild(BTNode*r);// 交换左右子树函数
	int Height(BTNode*r);// 二叉树深度函数 
};

//程序名:tree_function.cpp
//      程序功能:实现二叉树基本操作(包括建立,前中后序遍历,树高等操作的实现)
//          作者:吴雨羲
//          日期:2013.9.30
//          版本:1.0
//      修改内容:无
//      修改日期:
//      修改作者:
//
#include<iostream>
#include<stack>
#include<queue>
#include"tree_define.h"
using namespace std;
//
// 建立二叉树函数
// 函数功能:广义表方式输入
//函数参数:chr[] 存储广义表结构
//参数返回值:无
void BT::CreateBT(char chr[])
{
	stack< BTNode* > s;
	BTNode *p,*t;
	
	root=NULL;
	int k;//判断左右孩子 1左2右
	int i=0;
	while(chr[i]!='\0')
	{
		switch(chr[i])
		{
		case'(':
			s.push(p);
			k=1;
			i++;
			break;
		case',':
			k=2;
			i++;
			break;
		case')':
			t=s.top();s.pop();
			i++;
			break;
		default:
			p=new BTNode;
			p->data=chr[i];
			i++;
			if(root==NULL)
				root=p;
			else if(k==1)
			{
				t=s.top();t->lc=p;
			}
			else
			{
				t=s.top();t->rc=p;
			}
		}
		
	}
}
//
// 前序递归遍历函数
// 函数功能:前序递归遍历
//函数参数:
//       r   根或结点
//参数返回值:无
void BT::preOrder(BTNode*r)
{
	if(r==NULL)
		return ;
	else
	{
		cout<<r->data<<" ";
		preOrder(r->lc);
		preOrder(r->rc);
	}
}
//
// 中序递归遍历函数
// 函数功能:中序递归遍历
//函数参数:
//       r   根或结点
//参数返回值:无
void BT::inOrder(BTNode*r)
{
	if(r==NULL)
		return ;
	else
	{
		
		inOrder(r->lc);
		cout<<r->data<<" ";
		inOrder(r->rc);
	}
}
//
// 后序递归遍历函数
// 函数功能:后序递归遍历
//函数参数:
//       r   根或结点
//参数返回值:无
void BT::postOrder(BTNode*r)
{
	if(r==NULL)
		return ;
	else
	{
		
		postOrder(r->lc);
		postOrder(r->rc);
		cout<<r->data<<" ";
	}                                                                                                             
}
//
//层次遍历函数
// 函数功能:层次遍历函数
//函数参数:
//       r   根或结点
//参数返回值:无
void BT::levelOrder()
{   BTNode* t;
    queue<BTNode*>q;
    q.push(root);
    while(!q.empty())
   {
	t=q.front();
	q.pop();
	cout<<t->data<<" ";
	if(t->lc!=NULL)q.push(t->lc);
	if(t->rc!=NULL)q.push(t->rc);
	}
}

//
// 前序非递归遍历函数
// 函数功能:前序非递归遍历
//函数参数:无
//参数返回值:无
void BT::N_preOrder()
{
	BTNode *p=root;
	stack<BTNode*>s;
	while(!s.empty()||p!=NULL)
	{
		while(p!=NULL)
		{
			cout<<p->data<<" ";
			s.push(p);
			p=p->lc;
		}
		if(!s.empty())
		{
			p=s.top();
			s.pop();
			p=p->rc;
		}
	}
}
//
// 中序非递归遍历函数
// 函数功能:中序非递归遍历
//函数参数:
//       r   根或结点
//参数返回值:无
void BT::N_inOrder()
{
	BTNode *p=root;
	stack<BTNode*>s;
	while(!s.empty()||p!=NULL)
	{
		while(p!=NULL)
		{
			s.push(p);
			p=p->lc;
		}
		if(!s.empty())
		{
			p=s.top();s.pop();
			cout<<p->data<<" ";
			p=p->rc;
		}
	}
}
//
// 后序非递归遍历函数
// 函数功能:后序非递归遍历
//函数参数:r   根或结点
//参数返回值:无
void BT::N_postOrder()
{	
	node n;
	BTNode *p=root;
	stack< node > s;
	while(!s.empty()||p!=NULL)
	{
		while(p!=NULL)
		{
			n.t=p;
			n.flag=0;
			s.push(n);
			p=p->lc;
		}
		if(!s.empty())
		{
			n=s.top();s.pop();
			p=n.t;
			if(n.flag==1)
			{
				cout<<p->data<<" ";
				p=NULL;
			}
			else
			{
				n.flag=1;s.push(n);
				p=p->rc;
			}
		}
	}
}
//
// 二叉树深度函数
// 函数功能:求二叉树深度
//函数参数:
//       r   根或结点
//参数返回值:无

int BT::Height(BTNode*r)
{
	int lh,rh;
	if(r==0)
		return 0;
	else
	{
		lh=Height(r->lc);
		rh=Height(r->rc);
		if(lh>rh)
			return lh+1;
		else
			return rh+1;
	}
}
//
// 二叉树叶子总数函数
// 函数功能:求二叉树叶子总数
//函数参数:
//       r   根或结点
//       c   叶子个数
//参数返回值:无
void BT::LeafCount(BTNode*r,int&c)
{
	if(r!=NULL)
	{
		if(r->lc==NULL&&r->rc==NULL)
			c++;
		LeafCount(r->lc,c);
		LeafCount(r->rc,c);
	}
	
}
//
// 交换左右子树函数
// 函数功能:交换左右子树
//函数参数:
//       r   根或结点
//参数返回值:无
void BT::ChangeChild(BTNode*r)
{
	BTNode*temp;
	
	if(r==0)
		return ;
	else
	{
		temp=r->lc;
		r->lc=r->rc;
		r->rc=temp;
		ChangeChild(r->lc);
		ChangeChild(r->rc);
	}
}
//
// 输出二叉树函数
// 函数功能:输出二叉树
//函数参数:
//       r   根或结点
//参数返回值:无
void BT::PrintGenlist(BTNode*r)
{
	if(r!=0)
	{
		cout<<r->data;
		if(r->lc!=NULL||r->rc!=NULL)
		{
			cout<<'(';
			PrintGenlist(r->lc);
			if(r->rc!=NULL)
			{
				cout<<',';
				PrintGenlist(r->rc);
			}
			cout<<')';
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值