我正在尝试使用红黑树解决spoj(http://www.spoj.com/problems/SBANK/)中的SBANK问题。当我尝试提交解决方案时,我不断收到RUNTIME(SIGSEGV)错误。
此外,我无法在任何在线判断中使用此红黑树的实现(下面给出的代码)(不断得到相同的错误)。我试过调试代码但找不到它的来源。
有人可以帮我调试吗?
#include<bits/stdc++.h>
using namespace std;
struct node
{
string key;
int rep;
char color;
struct node *parent,*llink, *rlink;
};
class rbtree
{
struct node *root;
public:
rbtree()
{
root = NULL;
}
void left_rotate(node *nn)
{
struct node *temp = nn->rlink;
nn->rlink = temp->llink;
if(temp->llink != NULL)
temp->llink->parent = nn;
temp->parent = nn->parent;
if(temp->parent == NULL)
root = temp;
else if(nn == nn->parent->llink)
nn->parent->llink = temp;
else
nn->parent->rlink = temp;
temp->llink = nn;
nn->parent = temp;
temp = NULL;
delete temp;
}
void right_rotate(node *nn)
{
struct node *temp = nn->llink;
nn->llink = temp->rlink;
if(temp->rlink != NULL)
temp->rlink->parent = nn;
temp->parent = nn->parent;
if(temp->parent == NULL)
root = temp;
else if(nn = nn->parent->llink)
temp = nn->parent->llink;
else
temp = nn->parent->rlink;
temp->rlink = nn;
nn->parent = temp;
temp = NULL;
delete temp;
}
void insert_fixup(struct node *nn)
{
while(nn != root && nn->parent->color == 'r')
{
struct node *uncle;
if(nn->parent == root)
uncle = NULL;
else if(nn->parent == nn->parent->parent->llink)
uncle = nn->parent->parent->rlink;
else
uncle = nn->parent->parent->llink;
if(uncle != NULL && uncle->color == 'r')
{
nn->parent->color = 'b';
uncle->color = 'b';
nn->parent->parent->color = 'r';
nn = nn->parent->parent;
}
else
{
if(nn->parent == nn->parent->parent->llink)
{
if(nn == nn->parent->llink)
{
char ch = nn->parent->parent->color;
nn->parent->parent->color = nn->parent->color;
nn->parent->color = ch;
right_rotate(nn->parent->parent);
}
else
{
char ch = nn->parent->parent->color;
nn->parent->parent->color = nn->color;
nn->color = ch;
left_rotate(nn->parent);
right_rotate(nn->parent->parent);
}
}
else
{
if(nn == nn->parent->rlink)
{
char ch = nn->parent->parent->color;
nn->parent->parent->color = nn->parent->color;
nn->parent->color = ch;
left_rotate(nn->parent->parent);
}
else
{
char ch = nn->parent->parent->color;
nn->parent->parent->color = nn->color;
nn->color = ch;
right_rotate(nn->parent);
left_rotate(nn->parent->parent);
}
}
}
}
root->color = 'b';
}
void insert(string key)
{
struct node *cur,*parent,*newnode;
newnode = new node;
newnode->key = key;
newnode->rep = 1;
newnode->color = 'r';
newnode->llink = NULL;
newnode->rlink = NULL;
newnode->parent = NULL;
if(root == NULL)
root = newnode;
else
{
cur = root;
while(cur != NULL)
{
parent = cur;
if(cur->key == key)
{
cur->rep++;
return;
}
else if(cur->key > key)
cur = cur->llink;
else
cur = cur->rlink;
}
if(parent->key > key)
{
parent->llink = newnode;
newnode->parent = parent;
}
else
{
parent->rlink = newnode;
newnode->parent = parent;
}
}
insert_fixup(newnode);
newnode = NULL;
delete newnode;
}
void print()
{
inorder(root);
}
void inorder(node *nn)
{
if(nn != NULL)
{
inorder(nn->llink);
cout<<nn->key<<" "<<nn->rep<<endl;
inorder(nn->rlink);
}
nn = NULL;
delete nn;
}
void destroy(node *nn)
{
if(nn != NULL)
{
destroy(nn->llink);
destroy(nn->rlink);
if( nn != root)
delete nn;
}
}
void del()
{
destroy(root);
delete root;
}
};
int main()
{
int t;
scanf("%d",&t);
int n;
string s;
char c;
while(t--)
{
rbtree tree;
scanf("%d",&n);
scanf("%c",&c);
while(n--)
{
getline(cin,s);
s.erase(s.end()-1);
tree.insert(s);
}
tree.print();
tree.del();
printf("\n");
}
return 0;
}