红黑树插入代码显示分段故障11

时间:2016-12-08 16:49:37

标签: 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;
}

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:1)

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

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;
}

我让你找出为什么做练习。