数据结构笔记(关于二叉树的一些操作)

**

一、树中节点的数目

**
左子树中节点的数目+右子树中节点的数目+1

template<class T>
int BiTree<T>::count(BiNode<T>* root){
	int number=0;

	if (root==NULL)
		number=0;
	else
		number=count(root->lchild)+count(root->rchild)+1;
	return number;
}

二、统计叶子节点的数目

(一)基本思想:
1、增加一个数据成员,leafcount, 初值为0
2、对树进行遍历。 如果一个节点是叶子,则将leafcount+1;
3、可以在前序、中序或后序的遍历过程中进行计算。
(二)算法分析
从根节点出发,判断当前节点是否是叶子节点,如果是,则叶子数+1,否则,在左子树中遍历,并统计叶子节点的数目,在右子树中进行遍历,并统计叶子节点的数目。

template<typename T> 
void BiTree<T>:: countleaf(BiTreeNode<T> * root){
	if (root) {
		if (root->lchild==NULL && root->rchild==NULL)
			leafcount=leafcount+1;
		else
		{
			countleaf(root->lchild);
			countleaf(root->rchild);
		}
	}
	return;
}

三、树中叶子节点的数目

左子树中叶子节点的数目+右子树中叶子节点的数目

template<class T>
int BiTree<T>::leafcount(BiNode<T>* root){
	int number=0;

	if (root==NULL)
		number=0;
	else if(root->lchild==NULL && root->rchild==NULL)
		number=1;
	else
	    number=leafcount(root->lchild)+leafcount(root->rchild);
	return number;
}

四、计算树的高度

(一)高度的定义
max(左子树高度,右子树高度)+1
(二)算法分析
从根节点出发开始计算,如果root==NULL, 高度为0;否则,分别计算左子树的高度;右子树的高度;返回max(左子树高度,右子树高度)+1
(三)递归的定义

template<typename T> 
 int BiTree<T>::cal_height(BiTreeNode<T> * root){
	int lheight=0,rheight=0;
	if (root==0)  	 return 0;	
	lheight=cal_height(root->lchild);
	rheight=cal_height(root->rchild);
	if (lheight>rheight)	return lheight+1;
	else 		return rheight+1;
}

五、输出中缀表达式(并加上相应的括号)

Eg:(a+(b*(c-d)))-(e/f)
(一)基本思想
中序遍历。
中序遍历左子树前,输出左括号
中序遍历右子树后,输出右括号
如果遍历叶子结点的左右子树,不输出括号
如果遍历根节点的左右子树,不输出括号(否则,会得到形如(a+b)的表达式)

void BiTree<T>::In_Expression(BiNode<T>* root){
	if(root)
	{
  	   if(root!=this->root&&root->lchild!=0&&root->rchild!=0)
   		         cout<<"(";
	   In_Expression(root->lchild);
	   cout<<root->data;
   	   In_Expression(root->rchild);
 	   if(root!=this->root&&root->lchild!=0&&root->rchild!=0)
			cout<<")";
	}
	
}

六、输出二叉树逆时针旋转90后的形状

template <class T>
void BiTree<T>::Left_Rotate(BiNode<T>* root,int level){
	if(root){
		Left_Rotate(root->rchild, level+1);
		for(int i=0;i<level;i++)
			cout<<"\t";
		cout<<root->data<<endl;
		Left_Rotate(root->lchild, level+1);
	}
}

七、计算二叉树的宽度

(一)宽度定义
所谓宽度是指在二叉树的各层上,具有结点数最多的那一层上的结点总数
(二)基本思想
利用层次遍历实现

struct q_element{	BiNode * root;		int level;};
int BiTree::Width(){
	queue< struct q_element > q;
	int num[100]={0,1};
	q_element s,child;
	BiNode *root;
	root=this->root;
	if(root==NULL)
		return 0;
	s.root=root;	s.level=1;	q.push(s);	
	while(!q.empty())	{
		s=q.front();
		if(s.root->lchild){
			num[s.level+1]++;
			child.root=s.root->lchild;
			child.level=s.level+1;
			q.push(child);
		}
		if(s.root->rchild)	{
			num[s.level+1]++;
			child.root=s.root->rchild;
			child.level=s.level+1;
			q.push(child);
		}
		q.pop();
	}
	int max=0,i=1;
	while(num[i]>0){
		if(max<num[i])
			max=num[i];
		i++;
	}
	
	return max;
}

八、判断一棵树是否是完全二叉树

(一)基本思想:
基于层次遍历。
定义bool变量is_leaf,初值为false
如果is_leaf的值为true, 表示遍历的过程中遇到了叶子结点。
一旦在叶子结点之后再出现度为1、2的结点,则该树不是完全二叉树。
(二)算法分析
在层次遍历中,如果遇到一个节点
只有右儿子,没有左儿子,
false;
只有左,没有右
If (is_leaftrue) return false;
Else is_leaf=true;
两个儿子均为空
is_leaf=true
两个儿子均不空
If (is_leaf
true) return false;
将存在的儿子入队
能遍历完所有节点,即为完全二叉树

template<class T>
bool BiTree<T>::Is_Wanquan(BiNode<T> *root){
	queue<BiNode<T>*> q;
	BiNode <T>* pointer;
	bool is_leaf=false;
	if(!root)
		return false;
	q.push(root);
	while(!q.empty())	{
		pointer=q.front();	q.pop();
		if(pointer->rchild!=NULL && pointer->lchild==NULL)
			return false;
		else if(pointer->rchild==NULL && pointer->lchild!=NULL )
			if(is_leaf)	
				return false;
			else  //如果是完全二叉树,则,该结点之后的结点应为叶子节点
				is_leaf=true;
		else if(pointer->rchild==NULL && pointer->lchild==NULL )
			is_leaf=true;
		if(pointer->lchild!=NULL)
			q.push(pointer->lchild);
		if(pointer->rchild!=NULL)
			q.push(pointer->rchild);
	}
	return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值