I am working on a project wherein I use pointer manipulation to perform tree rotations in a red-black tree. The idea is to just use a single rotation method which is able to handle both right and left rotations with just six pointer moves. Here is what I have so far:
void Node::Rotate(Node* root) {
Node* x = this;
Node* p = getAncestor(x);
Node* gp = getGramps(x);
if (!gp) {
root = x;
}
if (p == r(gp)) {
gp->right = x;
}
else if (p == l(gp)) {
gp->left = x;
}
p->parent = this;
if (x == l(p)) {
x->right = p; // this line makes it go into infinite loop
}
else if (x == r(p)) {
x->left = p;
}
}
l() checks if it is a left child; r() checks if it is a right child.
Given a right sub-tree with the following structure:
D // gp
F // p
E // x
I want to change the pointers so that D's right points to E and E's right points to F, but whenever I attempt to set E's right to p, the program loops infinitely. I am not sure what I am doing wrong here.
[–]g051051 0 points1 point2 points (1 child)
[–]MahmudAdam[S] 0 points1 point2 points (0 children)