二叉树、二叉链表

</pre><pre name="code" class="cpp">#include <iostream>
using namespace std;
typedef char elemtype;
int n=0;
typedef struct BiNode
{
	elemtype data;
	BiNode *lchild, *rchild;
}BiNode;
class BiTree
{    
public:
	BiTree(){root=Creat();}
	~BiTree(){Release(root);}   
	BiNode * Getroot();
	void PreOrder(BiNode *root); //前序遍历递归算法
	void PreOrder2(BiNode *root);  //前序遍历非递归算法
	void InOrder(BiNode *root);   //中序遍历递归算法
	void InOrder2(BiNode *root); //中序遍历非递归算法
	void PostOrder(BiNode *root); //后序遍历递归算法
	void PostOrder2(BiNode *root); //后序遍历非递归算法
	void LeverOrder(BiNode *root); //层序遍历
	int  Depth(BiNode *root)  ; //求深度
	void Count(BiNode *root); //求二叉树的结点个数
private:
	BiNode *root; 
	BiNode* Creat();
	void Release(BiNode *root);
};

BiNode* BiTree::Getroot( )
{
	return root;
}
void   BiTree::PreOrder(BiNode *root) 
{
	if (root ==NULL)  return;    //递归调用的结束条件 
	else {
		cout<<root->data;         //访问根结点bt的数据域
		PreOrder(root->lchild);    //前序递归遍历bt的左子树
		PreOrder(root->rchild);    //前序递归遍历bt的右子树
	}
}
void BiTree::PreOrder2(BiNode *root) 
{
	int top= -1;      //采用顺序栈,并假定不会发生上溢
	BiNode * s[100];
	while (root!=NULL || top!= -1)
	{
		while (root!= NULL)
		{
			cout<<root->data;
			s[++top]=root;
			root=root->lchild;  
		}
		if (top!= -1) { 
			root=s[top--];
			root=root->rchild;  
		}
	}
}


void BiTree::InOrder (BiNode *root)//
{
	if (root==NULL) return;     
	else {
		InOrder(root->lchild); 
		cout<<root->data; 
		InOrder(root->rchild);
	}
}
void BiTree::InOrder2(BiNode *root)
{
	int top=-1;  //采用顺序栈,并假定不会发生上溢
	BiNode* s[100];
	while (root !=NULL||top!=-1)
	{
		while(root!=NULL)
		{
			s[++top]=root;  //将根指针root入栈
			root=root->lchild;
		}
		if (top!=-1)          //栈非空
		{
			root=s[top--];
			cout<<root->data;
			root=root->rchild;
		}
	}
}
void BiTree::PostOrder(BiNode *root)
{ 
	if (root==NULL) return; 
	else {
		PostOrder(root->lchild); 
		PostOrder(root->rchild); 
		cout<<root->data;          
	}
}

void BiTree::LeverOrder(BiNode *root)
{
	int front,rear;
	BiNode * Q[100];
	BiNode *q;
	front=rear=-1;    //采用顺序队列,并假定不会产生上溢
	if(root==NULL) return ;
	Q[++rear]=root;
	while (front!=rear)
	{
		q=Q[++front];
		cout<<q->data;
		if(q->lchild!=NULL)  Q[++rear]=q->lchild;
		if(q->rchild!=NULL)  Q[++rear]=q->rchild;
	}
}

/*
 *前置条件:空二叉树
 *输    入:数据ch;
 *功    能:初始化一棵二叉树,构造函数调用
 *输    出:无
 *后置条件:产生一棵二叉树
 */
BiNode* BiTree::Creat()
{
	BiNode* root;
	elemtype ch;
	cout<<"请输入创建一棵二叉树的结点数据"<<endl;
	cin>>ch;
	if (ch=='#') root = NULL;
	else{ 
		root = new BiNode;       //生成一个结点
		root->data=ch;
		root->lchild = Creat();    //递归建立左子树
		root->rchild = Creat();    //递归建立右子树
	} 
	return root;
}

void BiTree::Count(BiNode *root)  //n为全局量并已初始化为0
{
	if (root) {
		Count(root->lchild);
		n++;
		Count(root->rchild);
	}
}
int  BiTree::Depth(BiNode *root)  //求二叉树深度
{
	int hl ,hr;
	if (root==NULL) return 0;
	else {
		hl= Depth(root->lchild);
		hr= Depth(root ->rchild);
		return max(hl, hr)+1;
	}
}
void BiTree::Release(BiNode *root)
{
	if (root!=NULL)
	{
		Release(root->lchild); //释放左子树
		Release(root->rchild); //释放右子树
		delete root;    //释放根结点
	}
}

void main()
{
	BiTree bt;
//	cout<<"请输入二叉树序列,如:AB#D##C##"<<endl;
	BiNode *root=bt.Getroot();
	cout<<"树的前序遍历序列为:"<<endl;
	bt.PreOrder(root);
	cout<<endl;
	cout<<"------中序遍历------ "<<endl;
	bt.InOrder(root);
	cout<<endl;
	cout<<"------后序遍历------ "<<endl;
	bt.PostOrder(root);
	cout<<endl;
	cout<<"------层序遍历------ "<<endl;
	bt.LeverOrder(root);
	cout<<endl;
	cout<<bt.Depth(root)<<endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值