程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 代碼-求c++大神幫忙改程序!!!

代碼-求c++大神幫忙改程序!!!

編輯:編程解疑
求c++大神幫忙改程序!!!

結果要求如下:例如輸入:先序ABCDE,中序BADCE
輸出:後序BDECA並打印二叉樹
A
|_B
|_C
|_D
|_E
代碼如下:
#include
#include
using namespace std;
struct MyTreeNode
{
int col;//打印結果中的列
int row;//打印結果中的行
//char val ;
char data;
MyTreeNode rightChild , *leftChild;
} ;
MyTreeNode
rebuild(string preorderTraverse,string inorderTraverse)
{
int i , len ;
MyTreeNode *root = new MyTreeNode() ;
root->data = preorderTraverse[0] ;
//cout << pre<< " " << mid << endl ;
len = inorderTraverse.length() ;
for(i=0;i {
if(preorderTraverse[0]==inorderTraverse[i])
{
if(i!=0)
{
root->leftChild = rebuild(preorderTraverse.substr(1,i),inorderTraverse.substr(0,i)); //左子樹

}
else{
root->leftChild = NULL ;
}
if(i!=len-1)
{
root->rightChild = rebuild(preorderTraverse.substr(i+1,len-1-i),inorderTraverse.substr(i+1,len-1-i));//右子樹

}
else{
root->rightChild = NULL ;
}
}
}
return root ;
}
void after(MyTreeNode *root)
{
if(root==NULL)return ;
{
if(root->leftChild!=NULL)
after(root->leftChild) ;
if(root->rightChild!=NULL)
after(root->rightChild) ;

    cout << root->data;
}

}
const int SPAN=4;//每一層的橫向縮進值為4
unsigned char printResult[9][SPAN*4];//打印結果數組
int currRow=0;//第一輪行計數器
void printArray(unsigned char array[][SPAN*4],int length0,int length1)
{//打印結果數組
for(int i=0;i<length0;i++)
{//結果數組的行
for(int j=0;j<length1;j++)
{//結果數組的列
unsigned char p=array[i][j];
if(p==0)
{
//若字符值為0則換為空格
p=' ';

}
cout<<(unsigned char)p;

}
cout<<endl;
}
}

//確定每個節點在結果中的行列
void preorderTraverse(MyTreeNode*root,int level)
{
if(root==NULL)return;
root->row=currRow;
currRow++;
root->col=level*SPAN;
printResult[root->row][root->col]=root->data;

preorderTraverse(root->leftChild,level+1);
preorderTraverse(root->rightChild,level+1); 

}

//填充父節點與子節點的連線
void preorderForLine(MyTreeNode*root)
{
if(root==NULL)return;

int sCol=root->col;
int sRow=root->row;

if(root->leftChild!=NULL)
{
 //當前子樹根節點到左子節點的連線 

    int eCol=root->leftChild->col;
    int eRow=root->leftChild->row;

    for(int i=sRow+1;i<=eRow;i++) 
    {
        //豎線
        printResult[i][sCol]=179; 
    }
    for(int i=sCol+1;i<eCol;i++)
    {
        //橫線 
        printResult[eRow][i]=196; 
    }       

}

if(root->rightChild!=NULL)
{
//當前子樹根節點到右子節點的連線
int eCol=root->rightChild->col;
int eRow=root->rightChild->row;

for(int i=sRow+1;i<eRow;i++)
{
//豎線
    printResult[i][sCol]=179;

} 
for(int i=sCol+1;i<eCol;i++)
{
    //橫線 
    printResult[eRow][i]=196; 
} 

}

preorderForLine(root->leftChild);
preorderForLine(root->rightChild);
}

//掃描結果數組將其右側有橫線的豎線替換為豎橫線
void processAfterOne(unsigned char array[][SPAN*4],int length0,int length1)
{
for(int i=0;i<length0;i++)
{
//結果數組的行
for(int j=0;j<length1;j++)
{
//結果數組的列
unsigned char p=array[i][j];
//若字符值為179豎線
if (p==179)
{
//若字符值為179豎線則查看其右側字符是否為橫線
unsigned char pr=array[i][j+1];
if(pr==196)
{
//若其右側為橫線則將其替換為豎橫線
array[i][j]=195;

            } 
        }
    }
}

}

//掃描結果數組將其中下面沒有豎橫線替換為右拐豎線
void processAfterTwo(unsigned char array[][SPAN*4],int length0,int length1)
{
for(int i=0;i<length0;i++)
{// 結果數組的行
for(int j=0;j<length1;j++)
{
//結果數組的列
unsigned char p=array[i][j];
//若字符值為195豎線
if(p==195)
{
//若字符值為195豎橫線則查看其下側字符是否為空白
if(i+1<length0)
{
unsigned char pr=array[i+1][j];
if(pr=0)
{
//若其右側為橫線則將其替換為豎橫線
array[i][j]=192;

}
}
else
{
//若其位於最後一行則將其替代為右拐豎線
array[i][j]=192;
}
}

 } 

}
}

int main(int argc, char *argv[])
{
system("chcp 437>nul.");

string preorderTraverse , inorderTraverse ;
MyTreeNode *root = NULL ;
while(cin>>preorderTraverse>>inorderTraverse)
{
cout<<"後序:"<<endl;
MyTreeNode * root ;
root = rebuild(preorderTraverse,inorderTraverse) ;
after(root) ;
}

cout<<"=====preorder traverse print binary tree====="<<endl;
cout<<"================"<<endl;
//先序遍歷(計算每個節點打印位置)
preorderTraverse(root,0);
printArray(printResult,9,SPAN*4);

cout<<"================="<<endl;
//先序遍歷(填充父子節點到子節點的連線)
preorderForLine(root);
printArray(printResult,9,SPAN*4);

cout<<"===================="<<endl;
//掃描結果數組將其中右側有橫線的豎線替換為豎橫線 
processAfterOne(printResult,9,SPAN*4);
printArray(printResult,9,SPAN*4); 

cout<<"===================="<<endl;
//掃描結果數組將其中的下面沒有豎線的豎橫線替換為右拐豎線 
processAfterTwo(printResult,9,SPAN*4);
printArray(printResult,9,SPAN*4);
cout<<"===================="<<endl;

return 0;

}

最佳回答:


http://www.cnblogs.com/ITEagle/archive/2011/09/19/2180839.html
http://blog.csdn.net/nyist327/article/details/27736239

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved