gpt4 book ai didi

c++ - 尝试在二叉树中分配值时出现段错误

转载 作者:行者123 更新时间:2023-11-27 22:51:37 25 4
gpt4 key购买 nike

我正在为我的 C++ 类做一个 BinaryTree 项目,但我一直遇到段错误。它不断打破我试图在我的 TreeNode 类中的 setValue() 函数中将 x 分配给值的地方。有谁知道为什么会这样?

这是我的 TreeNode 类:

#ifndef TREENODE_H
#define TREENODE_H
#include <iostream>

template<typename T>
class TreeNode{
private:
T value;
TreeNode* left;
TreeNode* right;
public:
TreeNode();
TreeNode(const T&);
~TreeNode();
T& getValue();
TreeNode* getLeft() const;
TreeNode* getRight() const;
void setValue(const T&);
void setLeft(TreeNode*);
void setRight(TreeNode*);
};
template<typename T>
TreeNode<T>::TreeNode()
{
value = 0;
left = NULL;
right = NULL;
}
template<typename T>
TreeNode<T>::TreeNode(const T& x)
{
value = x;
left = NULL;
right = NULL;
}
template<typename T>
TreeNode<T>::~TreeNode()
{
if(left!=NULL)
delete left;
if(right!=NULL)
delete right;
}
template<typename T>
void TreeNode<T>::setValue(const T& x)
{
value=x;
}
template<typename T>
T& TreeNode<T>::getValue()
{
return value;
}
template<typename T>
TreeNode<T>* TreeNode<T>::getLeft() const
{
return left;
}
template<typename T>
TreeNode<T>* TreeNode<T>::getRight() const
{
return right;
}
template<typename T>
void TreeNode<T>::setLeft(TreeNode* x)
{
if(left!=NULL)
delete left;
left=new TreeNode;
left=x;
}
template<typename T>
void TreeNode<T>::setRight(TreeNode* x)
{
if(right!=NULL)
delete right;
right=new TreeNode;
right=x;
}

#endif

这是我的 BinaryTree 类:

#ifndef BINARYTREE_H
#define BINARYTREE_H
#include "TreeNode.h"
#include <iostream>

template<typename T>
class BinaryTree:public TreeNode<T>{
public:
BinaryTree();
BinaryTree(const T&);
~BinaryTree();
BinaryTree(const BinaryTree&);
void insert(const T&);
void inorder_traversal();
private:
TreeNode<T>* root;
TreeNode<T>* getRoot();
void setRoot(const T&);
void destroyTree(TreeNode<T>*);
};
template<typename T>
BinaryTree<T>::BinaryTree()
{
root = new TreeNode <T>;
root = NULL;
}
template<typename T>
BinaryTree<T>::BinaryTree(const T& x)
{
root = new TreeNode <T>(x);
}
template<typename T>
BinaryTree<T>::BinaryTree(const BinaryTree<T>& A)
{
root=A.getRoot();
}
template<typename T>
void BinaryTree<T>::destroyTree(TreeNode<T>* leaf)
{
if(leaf!=NULL)
{
destroyTree(leaf->getLeft());
destroyTree(leaf->getRight());
delete leaf;
}
}
template<typename T>
BinaryTree<T>::~BinaryTree()
{
if(root!=NULL)
destroyTree(root);
}
template<typename T>
TreeNode<T>* BinaryTree<T>::getRoot()
{
return root;
}
template<typename T>
void BinaryTree<T>::setRoot(const T& x)
{
if(root!=NULL)
{
delete root;
root=new TreeNode<T>;
}
root->setValue(x);
}
template<typename T>
void BinaryTree<T>::insert(const T& x)
{
if (root == NULL)
setRoot(x);
else
{
TreeNode<T>* rootTemp = new TreeNode < T > ;
rootTemp = root;
bool insertionComplete = false;
while (!insertionComplete)
{
if (x < rootTemp->getValue())
{
if ((rootTemp->getLeft()) == NULL)
{
(rootTemp->getLeft())->setValue(x);
insertionComplete = true;
}
else
rootTemp = rootTemp->getLeft();
}
else
{
if ((rootTemp->getRight()) == NULL)
{
(rootTemp->getRight())->setValue(x);
insertionComplete = true;
}
else
rootTemp = rootTemp->getRight();
}
}
root = rootTemp;
delete rootTemp;
}
}

#endif

这是我尝试运行的简单 main.cpp:

#include "TreeNode.h"
#include "BinaryTree.h"
#include <iostream>

int main()
{
BinaryTree<int> x(2);
std::cout << "first\n";
x.insert(1);
std::cout<<"END\n";
return 0;
}

最佳答案

在你的类(class)里,你有这段代码

if (x < rootTemp->getValue())
{
if ((rootTemp->getLeft()) == NULL)
{
(rootTemp->getLeft())->setValue(x);
insertionComplete = true;
}
else
rootTemp = rootTemp->getLeft();
}
else
{
if ((rootTemp->getRight()) == NULL)
{
(rootTemp->getRight())->setValue(x);
insertionComplete = true;
}
else
rootTemp = rootTemp->getRight();
}

当对象为 NULL 时,您基本上是在调用 setValue() 的地方。在使用它们之前,您必须从 TreeNode 对象初始化 left 和/或 right

关于c++ - 尝试在二叉树中分配值时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36690170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com