二叉树,构造函数
Binary Tree, constructor
我正在尝试将字符串传递给二叉树的构造函数,并使第一个字符成为根。但是,无论出于何种原因,我都无法将第一个值分配给root的值,而是整个程序崩溃。有人知道它为什么崩溃吗?
class PrefixTree
{
private:
struct TreeNode
{
char character;
TreeNode * left;
TreeNode * right;
};
TreeNode* root;
public:
PrefixTree()
{
root = NULL;
}
PrefixTree(string value)
{
cout<<"wait";
if (value[0] == '*')
{
cout << "Out 1"<<endl;
root->character = value;
cout << "Out 2"<<endl;
root->left = NULL;
cout << "Out 3"<<endl;
root->right = NULL;
}
}
和主要:
int main()
{
PrefixTree n("*abc");
return 0;
}
您需要
为root
分配内存,而您没有这样做。
PrefixTree(string value)
{
root = new TreeNode;
// . . .
}
另外,不要忘记在析构函数中delete
它(并正确处理副本)。
相关文章:
- 从父数组测试用例构造二叉树失败
- 签入二叉树的函数是平衡C++
- 错误表达式必须是插入函数(二叉树 C++)中的可修改 Ivalue
- C++二叉树打印函数实现
- 查找二叉树的父节点函数
- 二叉树顺序遍历错误:没有用于调用的匹配函数
- 二叉树,构造函数
- C++编译时构造的二叉搜索树
- 从序遍历和预序遍历构造二叉树的时间复杂性
- 复制构造函数和析构函数八叉树c++
- 创建一个函数,该函数返回一个新的相同二叉树,并向所有预先存在的叶添加新节点
- 我试图证明一个给定的树是一个二进制搜索树.我将输入一个二叉树,我只想让函数返回true
- 二叉树搜索的实现和函数声明c++
- 为什么我的函数不返回计数?(二叉树)
- 随机二叉树中的插入函数
- 遍历二叉树的函数
- 二叉树无效函数
- 二叉树 - 复制构造函数
- 节点和二叉树构造函数和析构函数分段
- 如何在c++函数中构造二叉树并返回它?