课堂笔记:二叉树算法设计练习

遍历二叉树是二叉树各种操作的基础, 遍历算法中对每个结点的访问操作可以是多种形式及多个操作, 根据遍历算法的框架,适当修改访问操作的内容,可以派生出很多关于二叉树的应用算法。
设计算法求二叉树的结点个数

void Count(BiNode *root){     
	if (root){          
		Count(root->lchild);          
		number++;//number为数据成员
		Count(root->rchild);    
	} 
}

树中节点的数目=左子树中结点的数目+右子树中结点的数目+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; 
}

统计叶子节点的数目
增加一个数据成员,leafcount, 初值为0。对树进行遍历,如果一个节点是叶子,则将leafcount+1; 可以在前序、中序或后序的遍历过程中进行计算。
算法分析
从根节点出发,判断当前节点是否是叶子节点,如果是,则叶子数+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; 
}

输出中缀表达式,并加上相应的括号
基本思想
中序遍历。
中序遍历左子树前,输出左括号
中序遍历右子树后,输出右括号
如果遍历叶子结点的左右子树,不输出括号
如果遍历根节点的左右子树,不输出括号(否则,会得到形如(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; 
}
发布了48 篇原创文章 · 获赞 25 · 访问量 2453

猜你喜欢

转载自blog.csdn.net/qq_43628959/article/details/103112011