I have been having trouble implementing the insertion method of the red-black tree. I am following a template for the assignment and the part that seems to causing the seg fault is when using the standard binary tree insert before doing all the rotations. The rotation method was provided in class. Whenever I remove the binary insert it works all the way through but of course it will only be able to insert three elements (root and left and right)
The comments for cout were just to pinpoint where the errors start occurring. It could also be in the left and right rotate. I will include them underneath the following code. As stated, it was provided in class because we are supposed to inherit this code to create other code but I am not even able to get past inserting 3 elements correctly.
I also tried to delete the x pointer because it is a new node but that also did not solve the issue. i know we are supposed to delete them once used but these are inside the red-black tree so we shouldn't need to be deleted correct?
template <class myType>
void redBlackTree<myType>::insert(myType value)
{
cout << "beginning insert" << endl;
if(search(value) == true){
cout << "returning" << endl;
return;}
cout << "creating new node" << endl;
nodeType<myType> *x = new nodeType<myType>;
x->right = NULL;
x->left = NULL;
x->parent = NULL;
x->keyValue = value;
x->color = RED;
if(root == NULL)
{
root = x;
x->color = BLACK;
return;
}
nodeType<myType> *curr = root;
cout << "setting this x: " << x->keyValue << endl;
/* code that seems to be causing the seg fault. Able to get to 4 elements inside before the seg fault comes.
while(true)
{
if(curr->keyValue > x->keyValue)
{
if(curr->left == NULL)
break;
curr = curr->left;
}
else
{
if(curr->right == NULL)
break;
curr = curr->right;
}
}
*/
if(curr->keyValue > x->keyValue)
{
x->parent = curr;
curr->left = x;
x->color = RED;
}
else
{
x->parent = curr;
curr->right = x;
x->color = RED;
}
while(x != root && x->parent->color == RED)
{
if(x->parent == x->parent->parent->left)
{
cout << "in if" << endl;
if(x->parent->parent->right != NULL && x->parent->parent->color == RED)
{
cout << "in if of if" << endl;
x->parent->color = BLACK;
x->parent->parent->right->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
}
else
{
cout << "in else of if" << endl;
if(x == x->parent->right)
{
x = x->parent;
leftRotate(x);
}
cout << "past else of if" << endl;
x->parent->color = BLACK;
x->parent->parent->color = RED;
rightRotate(x->parent->parent);
cout << "about to exit" << endl;
}
}
else
{
cout << "in else" << endl;
if(x->parent->parent->left != NULL && x->parent->parent->color == RED)
{
x->parent->color = BLACK;
x->parent->parent->left->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
}
else
{
if(x == x->parent->right)
{
x = x->parent;
rightRotate(x);
}
x->parent->color = BLACK;
x->parent->parent->color = RED;
leftRotate(x->parent->parent);
}
}
if(x == root)
x->color = BLACK;
cout << "looping" << endl;
}
cout << "out of exit" << endl;
};
left rotate
template <class myType>
void redBlackTree<myType>::leftRotate(nodeType<myType> *x)
{
nodeType<myType> *y = y->right;
x->right = y->left;
if(x->right != NULL)
root = y;
else if(x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
x->left = x;
x->parent = y;
};
right rotate
template <class myType>
void redBlackTree<myType>::rightRotate(nodeType<myType> *y)
{
nodeType<myType> *x = y->left;
x->left = x->right;
if(x->right != NULL)
x->right->parent = y;
x->parent = y->parent;
if(x->parent == NULL)
root = x;
else if(y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
x->right = y;
y->parent = x;
};
Aucun commentaire:
Enregistrer un commentaire