问题描述
- 关于二叉树中根和后根构造二叉树求大神帮忙看看有没有什么问题?
-
#include
using namespace std;template
class BinaryNode
{
public:
T data;
BinaryNode *left, *right;
BinaryNode(T data, BinaryNode*left = NULL, BinaryNode*right = NULL)
{
this->data = data;
this->left = left;
this->right = right;
}
};template
class BinaryTree
{
public:
BinaryNoderoot;
BinaryTree();
BinaryTree(T prelist[], int n);
BinaryTree(T inlist[], T postlist[], int n);
BinaryNode create(T prelist[], int n, int &i);
~BinaryTree();
};
template
BinaryTree::BinaryTree()
{
this->root = NULL;
}template
BinaryTree::BinaryTree(T prelist[], int n)
{
int i = 0;
this->root = this->create(T prelist[], int n, int &i);
}
template
BinaryNode* BianryTree::create(T prelist[], int n, int &i)
{
BinaryNode*p = NULL;
if (i < n)
{
T elem = prelist[i];
i++;
if (elem != NULL)
{
p = new BianryNode(elem);
p->left = create(prelist, n, i);
p->right = creat(prelist, n, i);
}
}
return p;
}
template
BinaryTree::BinaryTree(T inlist[], T postlist[], int n) //中根和后跟次序构造二叉树
{
BinaryNode*p = NULL;
if (n >0)
{
p = new BinaryNode(postlist[n-1]);
for (int i = 0; i < n;i++)
if (inlist[i] == postlist[n - 1])
break;
p - >left = new BinaryNode(T inlist[], T postlist[], i);
p -> right = new BinaryNode(T inlist[], T postlist[], n-1);
}
return p;
}
解决方案
http://bbs.csdn.net/topics/391853173
解决方案二:
不妨自己先单步看看和自己想的是否一样
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。