2 apprentices apprentices 于 2016.05.08 13:12 提问

关于二叉树中根和后根构造二叉树求大神帮忙看看有没有什么问题?

#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;
}

2个回答

havedream_one
havedream_one   2016.05.08 19:14
qq423399099
qq423399099   Ds   Rxr 2016.05.08 20:36

不妨自己先单步看看和自己想的是否一样
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!