求解二叉树的镜像用递归方式十分简单,详细见代码:
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