求改代码及注释,谢谢!

双木夕七七 2016-11-29 08:54:43
#include <iostream>
#include <stack>
using namespace std;

template<typename T>
struct TreeNode
{
T val;
TreeNode* left;
TreeNode* right;
TreeNode(T value):val(value),left(NULL),right(NULL){}
};

template<typename T>
class BinTree
{
public:
TreeNode<T>* _root;
BinTree<T>(TreeNode<T>* root):_root(root){}
void preOrder(TreeNode<T>* root);
void inOrder(TreeNode<T>* root);
void postOrder(TreeNode<T>* root);

};
template<typename T>
void postGoAlongLeft(TreeNode<T>* root,stack<pair<TreeNode<T>*,bool> >& pairStack)//pair是一种模板类型,可存储两个值
{
TreeNode<T>* t = root;
while(t)
{
if(t)
pairStack.push(make_pair(t,false));
t = t->left;
}
}
template<typename T>
void BinTree<T>::postOrder(TreeNode<T>* root)
{
stack<pair<TreeNode<T>*,bool> > pairStack;
postGoAlongLeft(root,pairStack);
while(pairStack.size())
{
pair<TreeNode<T>*,bool>&t = pairStack.top();
if(!t.first->right||t.second)
{
visit(t.first);
pairStack.pop();
}else
{
t.second = true;
postGoAlongLeft(t.first->right,pairStack);
}
}
}
template<typename T>
void visit(TreeNode<T>* node)
{
cout<<node->val<<" ";
}
template<typename T>
void BinTree<T>::inOrder(TreeNode<T>* root)
{
if(!root)
return;
TreeNode<T>* t;
stack<TreeNode<T>*> nodeStack;
goAlongLeft(root,nodeStack);
while(nodeStack.size())
{
TreeNode<T>* t = nodeStack.top();
nodeStack.pop();
visit(t);
if(t->right)
goAlongLeft(t->right,nodeStack);
}
}
template<typename T>
void goAlongLeft(TreeNode<T>* root,stack<TreeNode<T>*>& nodeStack)
{
TreeNode<T>* t = root;
while(t)
{
nodeStack.push(t);
t=t->left;
}
}
template<typename T>
void goAlongWithLeft(TreeNode<T>* root,stack<TreeNode<T>*>& nodeStack)
{
TreeNode<T>* t = root;
while(t)
{
nodeStack.push(t);
visit(t);
t=t->left;
}
}
template<typename T>
void BinTree<T>::preOrder(TreeNode<T>* root)
{
if(!root)
return;
TreeNode<T>* t;
stack<TreeNode<T>*> nodeStack;
goAlongWithLeft(root,nodeStack);
while(nodeStack.size())
{
TreeNode<T>* t = nodeStack.top();
nodeStack.pop();
if(t->right)
goAlongWithLeft(t->right,nodeStack);
}
}
int main()
{ TreeNode<int>* root = new TreeNode<int>(1);
root->left = new TreeNode<int>(2);
root->right = new TreeNode<int>(3);
root->right->left = new TreeNode<int>(5);
root->left->left = new TreeNode<int>(4);
BinTree<int> binTree(root);
cout << "Preorder sequence is ";
binTree.preOrder(root);
cout<<endl;
cout << "Inorder sequence is ";
binTree.inOrder(root);
cout<<endl;
cout << "Postorder sequence is ";
binTree.postOrder(root);
cout<<endl;
}
//每一层的横向缩进值
const int SPAN=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(BiTNode*root,int level)
{
if(root==NULL)return;
root->row=currRow;
currRow++;
root->col=level*SPAN;
printResult[root->row][root->col]=root->data;

preorderTraverse(root->lchild,level+1);
preorderTraverse(root->rchild,level+1);

}

//第二轮遍历填充父节点到子节点的连线
void preorderForLine(BiTNode*root)
{
if(root==NULL)return;

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

if(root->lchild!=NULL)
{
//当前子树根节点到左子节点的连线

int eCol=root->lchild->col;
int eRow=root->lchild->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->rchild!=NULL)
{
//当前子树根节点到右子节点的连线
int eCol=root->rchild->col;
int eRow=root->rchild->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->lchild);
preorderForLine(root->rchild);
}

//扫描结果数组将其中右侧有横线的竖线替换为竖横线
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 PreArray,InArray;

cout << "Please input preorder ";
getline(cin,PreArray);

cout << "Please input inorder ";
getline(cin,InArray);

BiTNode *T;

PreInCreateTree(T,0,PreArray.size()-1,0,InArray.size()-1,PreArray,InArray);

cout << "The postorder is ";

PostOrder(T);

cout << endl;

cout<<"=====preorder traverse print binary tree====="<<endl;
cout<<"================"<<endl;
preorderTraverse(T,0);
printArray(printResult,9,SPAN*4);

cout<<"================="<<endl;
preorderForLine(T);
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;
}
...全文
468 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-11-30
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧