c++ - 红黑树插入代码显示段错误 11

标签 c++ c data-structures tree red-black-tree-insertion

当我输入数字时,这显示段错误 11。 请帮忙。 我已经被困在这个问题里2个小时了。 我尝试了很多东西但无法完成它。 请帮忙。 rbInsert 或旋转函数可能有问题,但我找不到它。非常感谢任何帮助。

#include <iostream>
#include <cstdlib>

using namespace std;

// enum nodeColor
// {
//  red,
//  black
// };

struct rbNode
{
    int data;
    char color;
    struct rbNode *left,*right,*parent;
};

struct rbNode *root=NULL;

struct rbNode *newNode(int data)
{
    struct rbNode *newnode=(struct rbNode*)malloc(sizeof(struct rbNode));
    newnode->data=data;
    newnode->color='r';
    newnode->left=newnode->right=newnode->parent=NULL;
    return newnode;
}

void swapColor(struct rbNode *a,struct rbNode *b)
{
    struct rbNode *temp=a;
    a=b;
    b=temp;
}


void leftRotate(struct rbNode *root,struct rbNode* x)
{
    struct rbNode *y;
    y=x->right;
    x->right=y->left;
    if(y->left!=NULL)
    {
        y->left->parent=x;
    }
    y->parent=x->parent;

    if(x->parent==NULL)
    {
        y=root;
    }
    if(x->parent->left->data==x->data)y=x->parent->left;
    else y=x->parent->right;
    y->left=x;
    x->parent=y;
}

void rightRotate(struct rbNode *root,struct rbNode* x)
{
    struct rbNode *y;
    y = x->left; 
    x->left = y->right; 
    if ( y->right != NULL){
        y->right->parent = x;
    }
    y->parent = x->parent;
    if( x->parent == NULL){
        root = y;
    } 
    else if( x->data == x->parent->left->data){
        x->parent->left = y;
    }
    else x->parent->right = y;
    y->right = x; 
    x->parent = y; 

    return;

}

void rbInsertFix(struct rbNode *root,struct rbNode *newnode)
{
    struct rbNode *uncle;
    while(newnode->parent->color=='r')
    {
        if(newnode->parent->data==newnode->parent->parent->left->data) 
        {
            uncle=newnode->parent->parent->right;
            if(uncle->color=='r')
            {
                newnode->parent->color='b';
                uncle->color='b';
                newnode=newnode->parent->parent;
            }
            else if(uncle->color=='b')
            {

                if(newnode->parent->left->data==newnode->data)
                {
                    rightRotate(root,newnode->parent->parent);
                    swapColor(newnode->parent->parent,newnode->parent);
                }   

                else if(newnode->parent->right->data==newnode->data)
                {
                    leftRotate(root,newnode->parent);
                    rightRotate(root,newnode->parent->parent);
                    swapColor(newnode->parent->parent,newnode->parent);
                }

            }
        }   

        else 

        {   
            uncle=newnode->parent->parent->left;
            if(uncle->color=='r')
            {
                newnode->parent->color='b';
                uncle->color='b';
                newnode=newnode->parent->parent;
            }
            else if(uncle->color=='b')
            {

                if(newnode->parent->left->data==newnode->data)
                {
                    rightRotate(root,newnode->parent->parent);
                    leftRotate(root,newnode->parent->parent);
                    swapColor(newnode->parent->parent,newnode->parent);
                }   

                else if(newnode->parent->right->data==newnode->data)
                {
                    leftRotate(root,newnode->parent);
                    swapColor(newnode->parent->parent,newnode->parent);
                }

            }

        }

    }
root->color='b';
}



void rbInsert(struct rbNode **root,int data){
struct rbNode *z = (struct rbNode*)malloc(sizeof(struct rbNode));
z->data = data;
z->left = NULL;
z->right = NULL;
z->color = 'r';
struct rbNode *x = *root;
struct rbNode *y;
if ( root == NULL ){
    *root = z;
    (*root)->color = 'b';
    return;
}
while ( x != NULL){
    y = x;
    if ( z->data < x->data){
        x = x->left;
    }
    else x = x->right;
}
z->parent = y;
if ( y == NULL){
    *root = z;
}
else if( z->data < y->data ){
    y->left = z;
}
else y->right = z;
rbInsertFix(*root,z);

//cout << "yo";
return;
}

void inOrderTraversal(struct rbNode* root)
{
    if(root==NULL)
        return;
    inOrderTraversal(root->left);
    cout << root->data << root->color;
    inOrderTraversal(root->right);
}

int main(int argc, char const *argv[])
{
    int loop = 1;
    while(loop)
    {
        printf("\nRed Black Tree Management - Enter your choice : ");
        printf("\n1\tInsert into RBT\n2\tDisplay RBT inorder\n");
        int choice;
        int data;
        scanf("%d",&choice);
        switch(choice){
            case 1:
            printf("\nEnter the integer you want to add : ");
            scanf("%d",&data);
            rbInsert(&root,data);
            break;

            case 2:
            printf("\nInorder tree traversal left-root-right\n");
            inOrderTraversal(root);
            break;

            default:
            printf("\nInvalid Choice\n");
        }
        printf("\nPress '0' to terminate and '1' to continue : ");
        scanf("%d",&loop);
    }       
    return 0;
}

最佳答案

运行这个程序时我遇到了一个主要的问题。当从界面添加新节点时,由于这一行,我立即收到段错误:

L85 while(newnode->parent->color=='r')

我将开始修复这个 NULL 取消引用问题,然后从那里开始,但使用 gdb。这是一个非常有用的工具。只需使用调试信息进行编译(使用 -g 标志)并运行 gdb [out-file-name]

从那里在段错误位置设置断点并开始调试。

关于c++ - 红黑树插入代码显示段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41044675/

相关文章:

data-structures - 推/拉数据流模型的优缺点是什么?

c++ - 在解码嵌套的元素列表时使用 ei_decode_list

c++ - boost multi_index 容器的显式实例化

c++ - 返回列表索引的更优雅的方式

c - 如何使用c获得大文件大小

algorithm - 如何从一个位置找到所有可能可达的数字?

c++ - 具有 lambda 的多态访问者

c - 将指针数组传递给 GTK 相关函数

c - 参数类型错误

java - 包移除()方法