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   2016.05.08 19:14
qq423399099      2016.05.08 20:36