二叉树的层次遍历,叶子节点的个数,是否为完全二叉树等等

#include "stdafx.h"
#include <string>
#include <vector>
#include <queue>

#include <iostream>
using namespace std;


//定义树的节点
typedef struct BiTNode{
	char data;
	BiTNode *lchild,*rchild;  //左右孩子的指针
}*BiTree;

//先序创建一个二叉树  
int  CreateBiTree(BiTree &T,string &str,int *index)  
{  
	//index为字符串的序数的指针  
	//#代表结束  
	//string str="AB#D##C##";    //测试树  
	if ((*index)<str.size())  
	{         
		char t_nodeData=str[(*index)++];  
		if(t_nodeData=='#') T=NULL;  
		else  
		{  
			if(!(T=new (BiTNode))) 
				throw runtime_error("OVERFLOW");  

			T->data = t_nodeData;     // 生成根节点  
			CreateBiTree(T->lchild,str,index);  // 创建左子树   
			CreateBiTree(T->rchild,str,index);  // 创建右子树  
		}  
	}  

	return 0;  
}  

//后序递归遍历,交换左右节点
void PostOrderTraverse(const BiTree T){
	if (T == NULL) return;
	PostOrderTraverse(T->lchild);
	PostOrderTraverse(T->rchild);
	if (T->lchild!=NULL || T->rchild!=NULL)   //交换左右子树
	{
		BiTree p=T->lchild;
		T->lchild=T->rchild;
		T->rchild = p;
	}
}

//先序遍历输出这个树
void PreOrderTraverse1(BiTree T)  
{     
	if (!T) return;  
	cout<<T->data<<endl;  
	PreOrderTraverse1(T->lchild);  
	PreOrderTraverse1(T->rchild);  
}  

//计算树的最大深度
int TreeDepth(BiTree BT)
{
	int leftDep,rightDep;
	if(BT==NULL) return 0;
	else
	{
		leftDep=TreeDepth(BT->lchild);
		rightDep=TreeDepth(BT->rchild);

		if(leftDep>rightDep)
			return(leftDep+1);
		else
			return (rightDep+1);
	}
}
int NodeCount(BiTree BT)//统计所有节点的个数
{
	if(BT==NULL) 
		return 0;
	else 
		return (NodeCount(BT->lchild)+NodeCount(BT->rchild)+1);
}

int LeafCount(BiTree BT)//统计叶子节点的个数
{
	if(BT==NULL) 
		return 0;
	else if(BT->lchild==NULL&&BT->rchild==NULL) 
		return 1;
	else 
		return (LeafCount(BT->lchild)+LeafCount(BT->rchild));
}

void LevelTraverse(BiTree T)//层次遍历
{
	queue<BiTree> t_vBiTree;   //保存每个节点的指针的队列

	if (T)  t_vBiTree.push(T);
	while (!t_vBiTree.empty()) {
		//如果左右子不为空,则压栈
		if (t_vBiTree.front()->lchild != NULL)  
			t_vBiTree.push(t_vBiTree.front()->lchild);

		if (t_vBiTree.front()->rchild != NULL)  
			t_vBiTree.push(t_vBiTree.front()->rchild);

		cout<<t_vBiTree.front()->data<<endl;
		t_vBiTree.pop();
	}  //while
}

//求最宽层次的点的个数
int  WideLevelNum(BiTree T)
{
	queue<BiTree> t_vBiTree;         //保存每个节点的指针的队列

	//保存每层的最后一个节点
	BiTree t_pflag=T;   

	int maxnum=0;              //记录最大层次的节点的个数

	if (T){
		t_vBiTree.push(T);
		maxnum++;
	}
	while (!t_vBiTree.empty()) {
		//如果左右子不为空,则压栈
		if (t_vBiTree.front()->lchild != NULL) 
			t_vBiTree.push(t_vBiTree.front()->lchild);

		if (t_vBiTree.front()->rchild != NULL)   
			t_vBiTree.push(t_vBiTree.front()->rchild);

		//如果出栈的是标记的点,也就是上层的最后一个点,则将现在队列的
		//最后一个点赋值为当前层的最后一个点
		if (t_vBiTree.front()==t_pflag) 
			t_pflag=t_vBiTree.back();  

		cout<<t_vBiTree.front()->data<<endl;
		t_vBiTree.pop();
		maxnum=maxnum<t_vBiTree.size()?t_vBiTree.size():maxnum;
	}  //while
	return maxnum;
}



//判断是否为完全二叉树,返回1代表否,返回0代表是二叉树
int  JudgeTree(BiTree T)
{
	if( T == NULL )
		return 1;

	queue<BiTree> t_vBiTree;   //保存每个节点的指针的队列

	if (T)  
	   t_vBiTree.push(T);

	while (!t_vBiTree.empty()) {
		//如果左右子不为空,则压队列,循环结束时,
		//队列中存的是完全二叉树的最下面一层
		if (t_vBiTree.front()->lchild == NULL) 
			break;
		else 
			t_vBiTree.push(t_vBiTree.front()->lchild);

		if (t_vBiTree.front()->rchild == NULL)  
			break;
		else 
			t_vBiTree.push(t_vBiTree.front()->rchild);

		t_vBiTree.pop();
	}  //while


	//没有左子树,只有右子树
	if (t_vBiTree.front()->rchild != NULL && 
		    t_vBiTree.front()->lchild==NULL)  
		return 1;

	//左子树还有左子树
	if ( t_vBiTree.front()->lchild != NULL && 
		   t_vBiTree.front()->lchild->lchild != NULL )
		return 1;

	//如果后续的节点仍然有子树,那么也是非完全二叉树
	while (!t_vBiTree.empty()) {
		if  (t_vBiTree.front()->lchild != NULL ||
			      t_vBiTree.front()->rchild != NULL ) 
			return 1;

		t_vBiTree.pop();
	}
	if (t_vBiTree.empty())  return 0;  
}


int main()
{
	string str="AB#D##C##";    //测试树
	int index=0;
	BiTNode *T;
	CreateBiTree(T,str,&index);

	cout<< JudgeTree(T);
	return 0;

}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值