-1

当我输入数字时,这显示分段错误 11。请帮忙。我已经被困在这两个小时了。我尝试了很多东西,但无法通过它。请帮忙。可能是 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;
}
4

2 回答 2

1

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

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

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

从那里在段错误位置设置一个断点,然后开始调试。

于 2016-12-08T17:02:40.533 回答
1

Amoung 其他问题:这是错误的,此函数不会交换任何内容:

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

问题与此函数相同:

void swapNumbers(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
}

我让你找出原因作为练习。

于 2016-12-08T17:04:56.327 回答