【发布时间】:2013-11-18 06:47:25
【问题描述】:
我正在编写一个二叉搜索树来保存多项式的值。我唯一的问题是制作一个带有 2 个参数(系数,x 的幂)的叶子。
tree newTree(5,2) ;5(x^2)
tree anothertree() ;it works then I can insert leaf into the root
构造函数生成根,但是当我想打印树时它是空的。
这是我的构造函数,它接受 2 个值来为树创建一个节点。
tree::tree(const int& c,const int& x)
{
treeNode* root = new treeNode(c,x);
}
这是我用来为树创建节点的 Node 结构。
struct treeNode
{
int x;
int coef;
treeNode* left;
treeNode* right;
treeNode()
{
this->x=0;
this->coef=0;
this->left=nullptr;
this->right=nullptr;
}
treeNode(const int& c,const int& x)
{
this->x = x;
this->coef = c;
left = nullptr;
right = nullptr;
}
};
这是我正在学习的课程:
class tree{
private:
treeNode* root;
void postorder(treeNode* p);
void inorder(treeNode* p);
void preorder(treeNode* p);
void destroyTree(treeNode* p);
void copyTree(treeNode* original,treeNode* ©cat);
public:
tree();
tree(const int& c,const int& x);
tree(const tree& myTree);
~tree();
void insert(int c,int xval);
const tree & operator=(const tree &myTree);
void print_postorder();
void print_inorder();
void print_preorder();
};
【问题讨论】:
-
tree anothertree() ;it works- 嗯,这声明了一个返回tree,而不是tree对象的函数。它真的如你所愿吗? -
@Charles Bailey 它使默认树的根为空。
标签: c++ tree binary-search-tree