Binary tree traversal applicationOne

Binary tree traversal refers to accessing each node in the tree in a certain order. Due to the recursive nature of a binary tree, a non-empty binary tree can be regarded as consisting of three parts: the root node, the left subtree, and the right subtree. Traversing the information of these three parts in turn, the binary tree is traversed.

Full binary tree sequential storage to chain storage

A complete binary tree is stored in a one-dimensional array T [n], and the values ​​of each node are sequentially read from T [0] to establish a linked list representation of the binary tree

Since the one-dimensional array is stored starting from number 0, the left child of node i is 2i + 1, and the right child is 2i + 2.

void A_to_L(Type T[],int n,int i,BiTNode *&p)
{//利用引用型参数p将形参的值带回实参 
	if(i>=n)
	{
		p=NULL;
	} 
	else
	{
		p=(BiTNode*)malloc(sizeof(BiTNode));//建立根结点 
		p->data=T[i];
		A_to_L(T,n,2*i+1,p->lchild);        //递归建立左子女 
		A_to_L(T,n,2*i+2,p->rchild);        //递归建立右子女
	}
}

Complete binary tree chain storage to sequential storage

The recursive algorithm is also used, assuming that the root t should be stored in T [i], then its left child is stored in T [2i + 1], and its right child is stored in T [2i + 2]

void L_to_A(BiTNode *t,Type T[],int n,int i)
{
	if(t==NULL) return;
	if(i>=n)
	{
		cout<<"数组空间不足"<<endl; 
	}
	else
	{
		T[i]=t;
		L_to_A(t->lchild,T,n,2*i+1);
		L_to_A(t->rchild,T,n,2*i+2);
	}
}

Print the nodes of a binary tree

Binary tree with chain storage, output each node in the form of root (L, T)

void print_BiT(BiTNode *t)
{
	if(t!=NULL)
	{
		cout<<t->data;
		if(t->lchild!=NULL||t->rchild!=NULL)
		{
			cout<<"(";
			print_BiT(t->lchild);
			if(t->rchild!=NULL)
			{
				cout<<",";
				print_BiT(t->rchild);				
			}
			cout<<")";
		}
	}
}

Calculate the number of nodes with a degree of 1 in the binary tree

If the binary tree is empty, it directly returns 0; if it is not empty, the nodes with a degree of 1 in the left and right subtrees of the root are counted in turn, and then the root node is checked for a degree of 1;

int D_is_1(BiTNode *t)
{
	if(t==NULL) 
	{
	   return 0;
	}
	else
	{
		if((t->lchild!=NULL&&t->rchild==NULL)||(t->lchild==NULL&&t->rchild!=NULL))
		{
			return (1+D_is_1(t->lchild)+D_is_1(t->rchild));
		}
		else
		{
			return(D_is_1(t->lchild)+D_is_1(t->rchild));
		} 
	}
}

Calculate the number of nodes with a degree of 2 in the binary tree

int D_is_2(BiTNode *t)
{
	if(t==NULL) 
	{
	   return 0;
	}
	else
	{
		if(t->lchild!=NULL&&t->rchild!=NULL)
		{
			return (1+D_is_2(t->lchild)+D_is_2(t->rchild));
		}
		else
		{
			return(D_is_2(t->lchild)+D_is_2(t->rchild));
		} 
	}
}

Calculate the number of nodes with a degree of 0 in the binary tree

int D_is_0(BiTNode *t)
{
	if(t==NULL) 
	{
	   return 0;
	}
	else
	{
		if(t->lchild==NULL&&t->rchild==NULL)
		{
			return 1;
		}
		else
		{
			return(D_is_0(t->lchild)+D_is_0(t->rchild));
		} 
	}
}

Count the height of the binary tree

If the binary tree is empty, the height is 0; if the root node is a leaf node, the height is 1; otherwise, the height of the left and right subtrees is counted

int height(BiTNode *t)
{
	if(t==NULL) 
	{
	   return 0;
	}
	else
	{
		int lheight,rheight;
		lheight=height(t->lchild);
		rheight=height(t->rchild);
		return (1+(lheight>rheight)?lheight:rheight);		
	}
}

Count the depth of a node in a binary tree

If the node p is the root node, the level of t is returned (d, the initial value is 1); otherwise, the left subtree is searched recursively, and if it is not found, the right subtree is recursive, and none is found and returns 0;

int level(BiTNode *t,BiTNode *p,int d)
{
	if(t==NULL) 
	{
	   return 0;
	}
	else if(t==p)
	{
		return d;
	}
	else
	{
		int subTreelevel;
		if((subTreelevel=level(t->lchild,p,d+1))>0)
		{
			return subTreelevel;
		}
		else if((subTreelevel=level(t->rchild,p,d+1))>0)
		{
			return subTreelevel;
		}
		else
		{
			return 0;
		}	
	}
}
Published 28 original articles · won praise 2 · Views 3259

Guess you like

Origin blog.csdn.net/Maestro_T/article/details/83997366