How to write rotate function for red black tree?

I wrote a function for balancing a red-black tree, but I can’t figure out how I can write functions to rotate. I tried to implement it like this, but the tests still fail. Maybe there will be any tips on how you can improve this?

bool Tree::rotateTreeRight(TreeNode* x, TreeNode* y)
{
  x->left = y->right;
    if (y->right != nullptr) {
      y->right->parent = x;
    }
    y->parent = x->parent;
    if (x->parent == nullptr) {
      this->root = y;
    } else if (x == x->parent->right) {
      x->parent->right = y;
    } else {
      x->parent->left = y;
    }
    y->right = x;
    x->parent = y;
    return true;
}
bool Tree::rotateTreeLeft(TreeNode *x, TreeNode* y)
{
x->right = y->left;
    if (y->left != nullptr) {
      y->left->parent = x;
    }
    y->parent = x->parent;
    if (x->parent == nullptr) {
      this->root = y;
    } else if (x == x->parent->left) {
      x->parent->left = y;
    } else {
      x->parent->right = y;
    }
    y->left = x;
    x->parent = y;
return true;
}

Source: Windows Questions C++

LEAVE A COMMENT