Class notes: binary tree algorithm design exercises

Traversing the binary tree is the basis of various operations of the binary tree. The access operation to each node in the traversal algorithm can be in various forms and multiple operations. According to the framework of the traversal algorithm, the content of the access operation can be appropriately modified to derive a lot of binary trees. Application algorithm.
Design algorithm to find the number of nodes of binary tree

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

The number of nodes in the tree = the number of nodes in the left subtree + the number of nodes in the right subtree + 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; 
}

Count the number of leaf nodes.
Add a data member, leafcount, and the initial value is 0. Traverse the tree, if a node is a leaf, leafcount + 1; can be calculated in the traversal process of the pre-order, middle-order or post-order.
The algorithm analysis
starts from the root node and determines whether the current node is a leaf node. If it is, the number of leaves is +1. Otherwise, it traverses in the left subtree and counts the number of leaf nodes. It traverses in the right subtree and counts The number of leaf nodes.

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; 
}

Number of leaf nodes in the tree = number of leaf nodes in the left subtree + number of leaf nodes in the right subtree

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; 
}

Calculate the height
of the tree Definition of the height: max ( the height of the left subtree and the height of the right subtree) +1
algorithm analysis
starts from the root node, if root == NULL, the height is 0; otherwise, the height of the left subtree is calculated separately ; The height of the right subtree; return max (the height of the left subtree, the height of the right subtree) +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; 
}

Output the infix expression and add the corresponding parentheses to traverse the
basic idea
in order.
Before traversing the left subtree in middle order, output the left parenthesis
After traversing the right subtree, output the right parenthesis.
If traversing the left and right subtrees of the leaf node, do not output parentheses.
If traversing the left and right subtrees of the root node, do not output parentheses (otherwise Will get an expression of the form (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<<")";  
	}   
}

The shape of the output binary tree rotated 90 degrees counterclockwise
is traversed in the order from right to left;
each line outputs a node;
indented according to the level of the node.

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);  
	} 
}

Calculate the width of the binary tree The
so-called width refers to the total number of nodes on the layer with the largest number of nodes on each layer of the binary tree.
Basic idea : Realize by using layer traversal.

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; 
}
Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/103112011