分段错误发生在insert函数中,特别是在第一个*root = x;
语句中的if
处。功能已经布局,我们只需要实现它们,所以我不能添加另一个参数或任何东西。我已经坚持了一段时间,并且不知道它可能是什么。根指针被分配了内存,所以我真的不知道问题是什么。感谢任何帮助。谢谢。
#include "treeNode.h"
#include <iomanip>
template <class V>
class tree {
TreeNode<V> * root;
TreeNode<V> * cur;
int size;
public:
// default constructor
// by default, the tree is empty
tree(){
root = nullptr;
size = 0;
}
// search value x in tree rooted at node t
bool treeSearch(V x, TreeNode<V>* t){
if(t == nullptr) return false;
if(t->getDatum() == x) return true;
return treeSearch(x, t->getLeft()) || treeSearch(x, t->getRight());
}
bool treeSearch(V x){
treeSearch(x, root);
}
// binary search value x in tree rooted at node t
bool treeBSearch(V x, TreeNode<V>* t){
if(t == nullptr) return false;
if(t->getDatum() == x) return true;
if(t->getDatum() > x)
return treeBSearch(x, t->getLeft());
else
return treeBSearch(x, t->getRight());
}
bool treeBSearch(V x){
return treeBSearch(x, root);
}
// check node t is leaf
bool isLeaf(TreeNode<V>* t){
if(t != nullptr && t->getLeft() == nullptr && t->getRight() == nullptr){
return true;
}else{
return false;
}
}
// find the height of the tree rooted at node t
int height(TreeNode<V>* t){
if(t == nullptr) return -1;
if(isLeaf(t)) return 0;
return 1 + std :: max(height(t->getLeft()), height(t->getRight()));
}
int height(){
return height(root);
}
// find the number of nodes of tree rooted at t
int nNodes(TreeNode<V>* t){
if(t == nullptr) return 0;
return 1 + nNodes(t->getLeft()) + nNodes(t->getRight());
}
int nNodes(){
return nNodes(root);
}
// insert value x to the current tree object
void insert(V x){
if(root == nullptr){
*root = x;
}
else{
cur = root;
while(cur != nullptr){
if(cur->getDatum() < x){
if(cur->getLeft() == nullptr){
cur->setDatum(x);
cur->setLeft(cur);
}
else{
cur = cur->getLeft();
}
}
else{
if(cur->getRight() == nullptr){
cur->setLeft(cur);
}
else{
cur = cur->getRight();
}
}
}
}
}
// print out the values of tree rooted at x
// it shows the hierarchy of the tree
// it will be useful for debugging
void print(TreeNode<V> * x, int indent){
if(x == nullptr) return;
if (x->getRight() != nullptr) {
print(x->getRight(), indent+4);
}
if (indent != 0) {
cout << std::setw(indent) << ' ';
}
if(x->getRight() != nullptr){
cout << " /\n" << std::setw(indent) << ' ';
}
cout << x->getDatum() << endl;
if (x->getLeft() != nullptr) {
cout << std::setw(indent) << ' ' <<" \\\n";
print(x->getLeft(), indent+4);
}
}
void print(){
int count = 0;
print(root, count);
}
};
这是treeNode.h文件,其中包含每个函数的构造函数。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
template <class T>
class TreeNode{
T datum;
TreeNode<T>* left, * right;
public:
// constructor with datum value, left and right are nullptr
TreeNode(T x){
datum = x;
left = nullptr;
right = nullptr;
}
// constructor with datum value, left and right values
TreeNode(T x, TreeNode<T>* lft, TreeNode<T>* rgt){
datum = x;
left = lft;
right = rgt;
}
//destructor releases left and right nodes, if not nullptr
~TreeNode(){
if (left) {
delete left;
}
if (right) {
delete right;
}
}
// get datum value
T getDatum(){
return datum;
}
// get left pointer
TreeNode<T>* getLeft(){
return left;
}
// get right pointer
TreeNode<T>* getRight(){
return right;
}
// set the left pointer
void setLeft(TreeNode<T>* p){
left = p;
}
// set the right pointer
void setRight(TreeNode<T>* p){
right = p;
}
};
答案 0 :(得分:5)
这应该是显而易见的:
if(root == nullptr){
*root = x;
^ ~~~~ dereferencing null pointer causes UB
}
root是nullptr
,但你取消它并分配 - 这是UB未定义的行为,这个特定的行为导致段错误。
如何解决它 - 你应该代替取消引用空指针,为它赋值:
if(root == nullptr){
root = new TreeNode<V>( x );
}