求二叉树的镜像

        求解二叉树的镜像用递归方式十分简单,详细见代码:

void swap(BinTree *x,BinTree *y)
{
	BinTree t = *x;
	*x = *y;
	*y = t;
}
void Mirror(BinTree T)
{
	if(T)
	{
		swap(&(T->lChild),&(T->rChild));
		Mirror(T->lChild);
		Mirror(T->rChild);
	}
}

同样可以得到代码2:

void Mirror2(BinTree T)
{ 
	if (T == NULL) { 
    	return; 
  	} 
  	else { 
    	BinTree temp;

    	// do the subtrees 
    	Mirror2(T->lChild); 
    	Mirror2(T->rChild);

    	// swap the pointers in this node 
    	temp = T->lChild; 
    	T->lChild = T->rChild; 
    	T->rChild = temp; 
  	} 
}

完整测试程序(附带完整的树形显示函数):

#include <iostream>
#include <iomanip>
#include <stack>
#include <queue>
#include <assert.h>

using namespace std;
typedef struct BinTreeNode * BinTree;
typedef struct BinTreeNode
{
	char data;
	BinTree lChild,rChild;
} BinTreeNode;

void BuildBinTree(BinTree *T)
{
	char item;
	cin>>item;
	if(item=='#')
	{
		*T = NULL;
	}
	else {
		*T = new BinTreeNode;
		(*T)->data = item; 
		BuildBinTree(&((*T)->lChild));
		BuildBinTree(&((*T)->rChild));
	}
}
int Height(BinTree T)
{
	if (!T)
		return 0;
	return 1 + max(Height(T->lChild),Height(T->rChild));
	
}

void ClearBinTree(BinTree *T)
{
	if(*T)
	{
		ClearBinTree(&((*T)->lChild));
		ClearBinTree(&((*T)->rChild));
		delete (*T);
		*T = NULL;
	}	
}  

void MakeMat(BinTree T,int root_x,int root_y,int step,int **m)
{
	int lChildPos,rChildPos;
	lChildPos = root_x - step;
	rChildPos = root_x + step;
	if (!T) 
		return;
	else
	{
		m[root_y][root_x] = T->data;
		if (T->lChild) 
			m[root_y+1][root_x-1] = '/';
		if (T->rChild)
			m[root_y+1][root_x+1] = '\\';
		MakeMat(T->lChild,lChildPos,root_y+2,step>>1,m);
		MakeMat(T->rChild,rChildPos,root_y+2,step>>1,m);
	}
}

void MakeMat2(BinTree T,int root_x,int root_y,int step,int **m)
{
	int lChildPos,rChildPos;
	lChildPos = root_x - step;
	rChildPos = root_x + step;
	if (!T) 
		return;
	else
	{
		m[root_y][root_x] = 1;
		MakeMat2(T->lChild,lChildPos,root_y+1,step>>1,m);
		MakeMat2(T->rChild,rChildPos,root_y+1,step>>1,m);
	}
}

void BinTreeDisplay(BinTree T)
{
	if(!T)
		return;
	/* init placehold flags m[h][len] */
	int h = Height(T);
	int len = (1<<h) - 1;
	int row = 2 * h - 1; 
	int **m = new int*[row];
	for(int i= 0;i<row;i++){
		m[i] = new int[len];
		memset(m[i],0,len*sizeof(int));	
	}
	/* get level order traversal sequence */
	vector<char> v;
	queue<BinTree> q;
	queue<BinTree> qt;
	q.push(T);
	BinTree pt;
	while(!q.empty())
	{
		pt = q.front();
		if (pt->lChild)
			q.push(pt->lChild);
		if(pt->rChild)
			q.push(pt->rChild);
		v.push_back(pt->data);		
		q.pop();
	}
	/* generate output matrix  plus '/' and '\\' m[2*h-1][len] */
	MakeMat(T,len>>1,0,len+1>>2,m);
	/* generate output */
	int cnt = 0;
	int width = 2;
	for(int i = 0; i < row; i++)
	{
		for(int j = 0; j < len; j++)
		{
			if(m[i][j])
			{
				if (i & 1) 
					cout<<setw(width)<<char(m[i][j]);
				else
					cout<<setw(width)<<char(m[i][j]);
					//cout<<setw(width)<<m[i][j];
			}
			else
				cout<<setw(width)<<' ';		
		}
		cout<<endl;
	}
	
} 

void BinTreeDisplay2(BinTree T)
{
	if(!T)
		return;
	/* init placehold flags m[h][len] */
	int h = Height(T);
	int len = (1<<h) - 1;
	int row = h; 
	int **m = new int*[row];
	for(int i= 0;i<row;i++){
		m[i] = new int[len];
		memset(m[i],0,len*sizeof(int));	
	}
	/* get level order traversal sequence */
	vector<char> v;
	queue<BinTree> q;
	queue<BinTree> qt;
	q.push(T);
	BinTree pt;
	while(!q.empty())
	{
		pt = q.front();
		if (pt->lChild)
			q.push(pt->lChild);
		if(pt->rChild)
			q.push(pt->rChild);
		v.push_back(pt->data);		
		q.pop();
	}
	/* generate output matrix  plus '/' and '\\' m[row][len] */
	MakeMat2(T,len>>1,0,len+1>>2,m);
	/* generate output */
	int cnt = 0;
	int width = 2;
	for(int i = 0; i < row; i++)
	{
		for(int j = 0; j < len; j++)
		{
			if(m[i][j])
			{
					cout<<setw(width)<<v[cnt++];
			}
			else
				cout<<setw(width)<<' ';		
		}
		cout<<endl;
	}
	
} 
void swap(BinTree *x,BinTree *y)
{
	BinTree t = *x;
	*x = *y;
	*y = t;
}
void Mirror(BinTree T)
{
	if(T)
	{
		swap(&(T->lChild),&(T->rChild));
		Mirror(T->lChild);
		Mirror(T->rChild);
	}
}

int main()
{
	BinTree T = NULL;
	BuildBinTree(&T);
	BinTreeDisplay(T);
	cout<<"After Mirror:"<<endl;
	Mirror(T);
	BinTreeDisplay(T);
	ClearBinTree(&T); 
}
运行结果示例:

 

       A      
     /   \    
   B       C  
 /   \   /    
 D   E   F    
After Mirror:
       A      
     /   \    
   C       B  
     \   /   \
     F   E   D 
       A      
   B       C  
 D   E       F
After Mirror:
       A      
   C       B  
 F       E   D



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值