Perform Right Rotation on a Binary Search Tree in C++



Binary Search Tree (BST) is a special binary tree in which the left subtree of a node contains only nodes with values less than the node's value, and the right subtree contains only nodes with values greater than the node's value. In this article, we will learn how to perform a right rotation on a BST node using C++.

What is Right Rotation in BST?

Right rotation is a type of tree rotation technique that is used to balance a binary search tree. In the right rotation around a node, the node is moved to the right in such a way that the left child of the node becomes the new root of the subtree. The right child of the new root becomes the left child of the old root. These types of rotations are commonly used in self-balancing trees like AVL trees and Red-Black trees.

For example, given below is a unbalanced binary search tree:

     30
    /
   20
  /
10

After performing a right rotation on node 30, the tree become balanced (ie, the height of left sub tree and right sub tree is same):

   20
  /  \
10   30

Implement Right Rotation in C++

To implement a right rotation, we just need to update the root of the subtree, its left child, and the pointers accordingly. Below are some key points that will make you understand the method:

  • Structure: Each node contains a data value and two pointers to the left and right child.
  • Logic: Identify the node to rotate, assign its left child as the new root, move the right subtree of the new root to the left of the old root, and return the new root.

Steps to Perform Right Rotation on a Node

Following are steps/algorithm to perform right rotation on a node in a BST:

  • Create a BST node structure with data, left, and right pointers.
  • Identify the node where right rotation needs to be performed.
  • Store the left child of the node in a temporary variable.
  • Assign the right child of the left node to be the left child of the current node.
  • Make the current node the right child of the left node.
  • Return the new root after rotation.

C++ Program to Perform Right Rotation on a BST

In the below code, we have implemented a binary search tree and performed right rotation on the root node.

#include <iostream>
using namespace std;

// Define a node of the binary search tree
struct Node {
    int data;
    Node* left;
    Node* right;

    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};

// Function to perform right rotation
Node* rightRotate(Node* root) {
    if (root == nullptr || root->left == nullptr)
        return root;

    Node* newRoot = root->left;
    root->left = newRoot->right;
    newRoot->right = root;

    return newRoot;
}

// Inorder traversal to display BST
void inorder(Node* root) {
    if (root == nullptr) return;
    inorder(root->left);
    cout << root->data << " ";
    inorder(root->right);
}

int main() {
    // Creating an unbalanced BST
    Node* root = new Node(30);
    root->left = new Node(20);
    root->left->left = new Node(10);

    cout << "Inorder before rotation: ";
    inorder(root);
    cout << endl;

    // Perform right rotation at root
    root = rightRotate(root);

    cout << "Inorder after right rotation: ";
    inorder(root);

    return 0;
}

The output of above code will be:

Inorder before rotation: 10 20 30
Inorder after right rotation: 10 20 30

Time and Space Complexity

Time Complexity: O(1), as rotation involves a constant number of pointer changes.

Space Complexity: O(1), as no additional space is used except for temporary pointers.

Updated on: 2025-05-16T19:07:33+05:30

798 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements