c++ - AVL 树插入 - 段错误

标签 c++ segmentation-fault avl-tree

我正在尝试使用他们的编辑器在 Hackerrank ( https://www.hackerrank.com/challenges/self-balancing-tree) 上解决这个问题。以下是我写的C++函数代码:

node* makeNewNode (int data)
{
    node* temp= new node();
    temp->val=data;
    temp->left=NULL;
    temp->right=NULL;
    temp->ht=0;
    return temp;
}

//------------------------------------------------------------
int height(node* temp)
{
    if(temp==NULL)
        return -1;
    return temp->ht;
}

//------------------------------------------------------------
int balanceFactor(node* root)
{
    if(root==NULL)
        return 0;
    return height(root->left)-height(root->right);
}

//------------------------------------------------------------
node* rightrotation(node* root)
{
    node* temp1 = root->left;
    node* temp = temp1->right;
    temp1->right = root;
    root->left = temp;
    root->ht = max(height(root->left), height(root->right)) + 1;
    temp1->ht = max(height(temp1->left), height(temp1->right)) + 1;
    return temp1;
}

//------------------------------------------------------------
node* leftrotation(node* root)
{
    node* temp = root->right;
    node* temp1 = temp->left;
    temp->left = root;
    root->right = temp1;
    root->ht = max(height(root->left), height(root->right)) + 1;
    temp->ht = max(height(temp->left), height(temp->right)) + 1;
   return temp;
}

//------------------------------------------------------------
node* insert( node* root, int data)
{
    if(root == NULL)
        return makeNewNode(data);

    if(data<root->val)
        root->left = insert(root->left, data);
    else if(data>root->val)
        root->right = insert(root->right, data);
    else
        return root;

    root->ht = 1 + max(height(root->left), height(root->right));
    int balance = balanceFactor(root);

    if(data<root->left->val && balance>1)
        return rightrotation(root);

    if(data>root->right->val && balance<-1)
        return leftrotation(root);

    if(balance>1 && data > root->left->val)
    {
        root->left = leftrotation(root->left);
        return rightrotation(root);
    }

    if(balance<-1 && data < root->right->val)
    {
        root->right = rightrotation(root->right);
        return leftrotation(root);
    }

    return root;
}

我在 if(balance>1 && data > root->left->val) 行中遇到段错误,但我不知道为什么。在进入此之前,我尝试检查 root->left is null ,但即使这样也会出现段错误。

因为我使用的是 Hackerrank 内置编辑器,所以主要功能已得到处理。

尽管如此,出于调试目的,我添加了以下 main():

int main()
{
node* root=NULL;
root=insert(root,1);
root=insert(root,2);
root=insert(root,3);
return 0;
}

我尝试了在线 gdb (www.onlinegdb.com),它显示了

Program received signal SIGSEGV, Segmentation fault.                                                                                     
0x0000000000400aa2 in insert (root=0x603010, data=2) at main.cpp:86                                                                      
86          if(data<root->left->val && balance>1)     

请帮忙。

最佳答案

你不能写

if(data<root->left->val && balance>1)

如果root或者如果 root->left可以是nullptr .

如果您的 AVL 树只有一个根并且您在正确的节点上插入,那么 root->left == nullptrroot->right是你插入的节点。

所以执行会继续data < root->left->val它将产生一个段错误。您需要插入更多条件,例如

if(root->left != nullptr && data<root->left->val && balance>1)

这里是修改后的插入函数:

node* insert( node* root, int data)
{
    ...
    int balance = balanceFactor(root);

    if(root->left && data<root->left->val && balance>1) // here was the segmentation fault with gdb
        return rightrotation(root);

    if(root->right && data>root->right->val && balance<-1)
        return leftrotation(root);

    if(balance>1 && root->left && data > root->left->val)
    { ... }

    if(balance<-1 && root->right && data < root->right->val)
    { ... }

    return root;
}

关于c++ - AVL 树插入 - 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40707018/

相关文章:

c++ - 在 C++ 中进行字符串连接后崩溃

c++ - 在 C++ 中创建自定义比较器

c++ - 前向声明与包含

c - "Cannot allocate memory"在 C 中使用 shmat 命令的共享内存问题

c - 在c中克隆avl树时出现总线错误

c++ - 编译C++代码报错 "undefined reference to",原来的makefile代码编译通过

c++ - 指针的地址是什么意思?

c - 多字符字符常量,分配数组值时出现段错误

c - 如何在没有段错误的情况下增加 C 中的字符指针?

c++ - 如何识别 AVL 树中的扰动节点?