AVL树插入方法导致分割故障

AVL tree insert method cause segmentation fault

本文关键字:分割 故障 方法 插入 AVL      更新时间:2023-10-16

编辑: 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;
    }

使用调试器在大约两分钟内发现了此错误。在您开发的这个阶段,这可能是您可以学会使用的最有用的东西。