AVL树插入方法导致分割故障
AVL tree insert method cause segmentation fault
编辑: insert()
似乎现在正在工作,但是打印树存在问题。
void AVL::preorder(Node *n) {
if(n != nullptr) {
cout << n->value << ", ";
preorder(n->left);
preorder(n->right);
}
}
void AVL::printPreorder() {
preorder(node);
}
我的问题是,在我将第一个值插入树上后,我无法插入更多。当我尝试插入1个以上的值时,程序会随分段故障而断开。我在这里找不到问题。看起来它在if()
语句中破裂了,但我不知道这是什么问题。我首先仅使用struct
编写了程序,它起作用了,但是我必须将其修改为整个课程,其中内部有一个结构。编辑:添加main.cpp
int main() {
AVL avl;
avl.insert(2);
avl.insert(6); // breaks
return 0;
}
avl.h
class AVL {
public:
struct Node {
int value;
int height;
Node* left;
Node* right;
};
Node* node;
AVL();
~AVL();
int getHeight(Node *tree);
Node *newNode(int val);
Node *rightRotate(Node *y);
Node *leftRotate(Node *x);
int getBalance(Node *b);
Node *insertVal(Node *node, int thisval);
void preorder(Node *n);
void printPreorder();
Node *minValNode(Node *minValNode);
Node *remove(Node *n, int thisval);
Node *insert(int val);
};
.cpp
AVL::AVL() {
node = nullptr;
}
AVL::Node* AVL::insertVal(Node *node, int thisval) {
if(node == nullptr) {
return newNode(thisval);
}
if(thisval < node->value) {
node->left = insertVal(node, thisval);
}
else if(thisval > node->value) {
node->right = insertVal(node, thisval);
}
else {
return node;
}
node->height = max(getHeight(node->left), getHeight(node->right)) + 1;
int balance = getBalance(node);
if(balance > 1 && thisval < node->left->value) {
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
return leftRotate(node);
}
if(balance > 1 && thisval > node->left->value) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
AVL::Node* AVL::insert(int val) {
return insertVal(node, val);
}
int AVL::getHeight(Node *tree) {
if(tree == nullptr) {
return 0;
} else {
return tree->height;
}
}
AVL::Node* AVL::newNode(int val) {
node = new Node;
node->value = val;
node->height = 1;
node->left = nullptr;
node->right = nullptr;
return node;
}
AVL::Node* AVL::rightRotate(Node *y) {
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return x;
}
AVL::Node* AVL::leftRotate(Node *x) {
Node *y = x->right;
Node *T2 = y->left;
x->right = T2;
y->left = x;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
int AVL::getBalance(Node *b) {
if(b == nullptr) {
return 0;
} else {
return getHeight(b->left) - getHeight(b->right);
}
}
所以,我长期以来一直忘记了AVL算法的细节,但我可以解释您的代码在哪里崩溃。您有一个无限递归环,导致堆栈溢出。
AVL::Node* AVL::insertVal(Node *node, int thisval) {
if (node == nullptr) {
return newNode(thisval);
}
if (thisval < node->value) {
node->left = insertVal(node, thisval);
}
else if (thisval > node->value) {
node->right = insertVal(node, thisval);
}
else {
return node;
}
在此代码中,使用的是您提供的thisval > node->value
是True,因此insertval
再次称为,具有完全相同的参数。因此,整个过程重复并重复,然后重复直到堆叠溢出为止。
我想你的意思是这个
AVL::Node* AVL::insertVal(Node *node, int thisval) {
if (node == nullptr) {
return newNode(thisval);
}
if (thisval < node->value) {
node->left = insertVal(node->left, thisval);
}
else if (thisval > node->value) {
node->right = insertVal(node->right, thisval);
}
else {
return node;
}
使用调试器在大约两分钟内发现了此错误。在您开发的这个阶段,这可能是您可以学会使用的最有用的东西。
相关文章:
- 分割故障C++矩阵
- C ,二进制树的高度,而不是检查我的子树是否为空,而是在检查我的子树是否是叶子节点.抛出分割故障
- 我正在为此代码分割故障
- 在执行strcpy时获取分割故障错误
- 空隙指针分割故障
- Xcode会产生分割故障,而不是例外
- OPENCV C 中的分割故障误差
- 分割故障:核心倾倒C 矢量对字符串:
- C 2D数组导致分割故障(核心倾倒)
- 分割故障/错误读数变量-C 列表
- DLSYM导致分割故障
- 使用C 中的操作员删除的分割故障
- 使用Linux X64下的Libmozjs-52(SpidermonKey)进行分割故障
- 使用长变量时分割故障(11)错误
- C - 返回字符串时的分割故障
- 创建VertexArray时的分割故障
- 向量分割故障的向量
- 从std :: fileSystem ::路径对象的类中退出时的分割故障
- C 矢量分割故障
- 基于向量的数据格式的分割故障误差