C ++分段错误-自平衡树插入

问题描述

我正在尝试解决Hackerrank(https://www.hackerrank.com/challenges/self-balancing-tree/problem)上的自平衡树问题,并且在最近3个测试案例中,我一直遇到分段错误。我看到以前曾发布过相同的问题,并尝试对我的案例采用相同的方法(检查nullptr),但到目前为止没有成功。以下代码是用C ++编写的,如果你们能帮助我,我将不胜感激。谢谢!

JCheckBox checkBox = (JCheckBox)e.getSource();

编辑:

出于调试目的,我使用了在线gdb并将其添加到以下遍历方法和主要功能之上的代码中:

/* Node is defined as :
typedef struct node
{
    int val;
    struct node* left;
    struct node* right;
    int ht;
} node; */

node * newNode(int val)
{
    node * newNode = new node;
    
    newNode->val = val;
    newNode->left = nullptr;
    newNode->right = nullptr;
    newNode->ht = 0;
    
    return newNode;
}

int getHeight(node* root)
{
    if(root == nullptr) {
        return -1;
    } else {
        return root->ht;
    }
}

node* rightRotate(node* root)
{
    node* temp = root->left;
    root->left = temp->right;
    temp->right = root;

    root->ht = 1 + max(getHeight(root->left),getHeight(root->right));
    temp->ht = 1 + max(getHeight(temp->left),getHeight(temp->right));

    return temp;
}

node* leftRotate(node* root)
{
    node* temp = root->right;
    root->right = temp->left;
    temp->left = root;

    root->ht = 1 + max(getHeight(root->left),getHeight(temp->right));

    return temp;
}

node * insert(node * root,int val)
{
    if(root == nullptr) {
        root = newNode(val); 
        return root;
    }
    if(val > root->val) {
        root->right = insert(root->right,val);
    }
    else if(val < root->val) {
        root->left = insert(root->left,val);
    }
    else{ return 0; }
    
    root->ht = 1 + max(getHeight(root->left),getHeight(root->right));
  
    int balance = getHeight(root->left) - getHeight(root->right);
    
    if(root->left != nullptr && balance > 1) {//Left subtree disbalanced
        if(val < root->left->val) { 
            //Left-Left case: perform a right rotation on the disb. node
            return rightRotate(root);
        }
        else {
            //Left-Right case: perfom a left rotation on the disb. node left subtree
            //and a right rotation on the disb. node
            root->left = leftRotate(root->left);
            return rightRotate(root);

        }
    }
    if(root->right != nullptr && balance < -1) {//Right subtree disbalanced
        if(val > root->right->val) { 
            //Right-Right case: perform a left rotation on the disb. node
            return leftRotate(root);
        }
        else {
            //Right-Left case: perfom a right rotation on the disb. node right subtree
            //and a left rotation on the disb. node
            root->right = rightRotate(root->left);
            return leftRotate(root);
        }
    }
    return root;
}

尝试按此顺序插入值2、4和3后,我们将得到不平衡的树,因为右子树的高度为1,而左子树的高度为-1(叶节点为nullptr),则平衡因子应小于-1。进一步的分析表明,由于出现失衡的节点是失衡节点(根2)的右子节点的左子节点,因此我们有RIGHT-LEFT的情况。然后,我们必须在不平衡节点的右子节点上执行右旋转,然后在不平衡节点本身上执行左旋转,并且树的结局应如下所示:

void inorder(node* root) {
    if(root == NULL) {
        return;
    } else {
        inorder(root->left);
        cout << root->val << " ";
        inorder(root->right);
    }
}

int main()
{
    node* root=NULL;

    root=insert(root,2);
    root=insert(root,4);
    root=insert(root,3);
    
    inorder(root);
    return 0;
}

解决方法

感谢所有尝试帮助我解决这一问题的人,事实证明我的愚蠢程度没有限制。我相信我已经弄清楚了我做错了什么。 这个问题的问题在于检查右子树是否不平衡的部分,如果是,则进行必要的旋转。在下面的代码段中,应该是

root->right = rightRotate(root->right);

代替

root->right = rightRotate(root->left);