当我输入数字时,这显示分段错误 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;
}