C++ 显示分段故障的红黑树插入代码11
当我输入数字时,显示分段故障11。 请帮忙。 我已经被困在这两个小时了。 我试过很多东西,但都没能通过。 请帮忙。 可能rbInsert或旋转函数有问题,但我找不到。非常感谢您的帮助C++ 显示分段故障的红黑树插入代码11,c++,c,data-structures,tree,red-black-tree-insertion,C++,C,Data Structures,Tree,Red Black Tree Insertion,当我输入数字时,显示分段故障11。 请帮忙。 我已经被困在这两个小时了。 我试过很多东西,但都没能通过。 请帮忙。 可能rbInsert或旋转函数有问题,但我找不到。非常感谢您的帮助 #include <iostream> #include <cstdlib> using namespace std; // enum nodeColor // { // red, // black // }; struct rbNode { int data; c
#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;
}
在运行这个程序时,我遇到了一个主要的第一个问题。当从接口添加新节点时,我立即得到一个segfault,因为这行代码:
L85 while(newnode->parent->color=='r')
我将开始修复这个空解引用问题,然后从那里开始使用gdb。这是一个非常有用的工具。只需使用调试信息编译,使用-g标志并运行gdb[out file name]
在SEGFULT位置设置断点,然后开始调试。其他问题:这是错误的,此函数不会交换任何内容:
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;
}
问题与此函数中的问题相同:
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;
}
<>我让你知道为什么做练习.< /p>为什么你在C++代码中使用SCANF?这基本上是C代码。并阅读警告:在z->parent=y行;编译器抱怨y可能未初始化。如何调用newNode和swapColor?问题可能就在那里。请阅读:C/C++语言不存在。你现在使用的语言是C语言,有一些C++的东西。在所有的代码中都有很多未初始化的指针。通常有很多问题,但是我认为这是OP开始使用调试工具和学习不同的调试方法的好机会。