Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 965859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:03:58+00:00 2026-05-16T02:03:58+00:00

I am working on writing red-black tree myself. But when I test the rotation

  • 0

I am working on writing red-black tree myself. But when I test the rotation that involves root to be rotated, somewhat it loses reference.

Tree structure:

          45             
        /    \
      40x      70        
     /  \     /
    7   41   50           
   / \
  6  39

The rotate logic says:
“Rotate with 45(root) as top, in the direction that raises X (i.e. 40)”

So this means right rotate and result should look like:

     40x  
    /   \
   7     45
 / \     / \
 6  39  41  70
           /
          50

Assuming that node 45 is grandparent and 7 is parent and 41 is current. (I know the order doesn’t make sense but please ignore, this is because I’ve rotated once already)

Code:

  //current is node 45
    //parent is node 7
    //grandparent is node 45 (root)

    //first detach cross node (i.e. 41)
    Node crossNode2 = grandparent.left.right;
    grandparent.left.right = null; //detach


                   45             
                /     \
              40x      70        
             /  \      /
            7   null   50           
           / \
          6  39

    grandparent.left = null;




                   45             
                 /    \
               null   70        
                      /
                    50           

    current.right = grandparent;

          40
        /    \
      7        45
     /  \     /  \
   6    39   null 70
                  /
                 50

    grandparent.left = crossNode2; //attach


         40  
        /   \
       7     45
     / \     / \
     6  39  41  70
               /
              50

But somehow this code does not work. When I tested:

preorder
45, 39, 70, 50
breadth-first
45, 39, 70, 50

So I think the result is actually:

  45
 /  \
39  70
     /
    50

Could anyone give me tips what’s wrong with my rotation code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:03:59+00:00Added an answer on May 16, 2026 at 2:03 am

    Step to do a right rotation on node Q:

    1. Let P = Q’s left child.
    2. Q’s left child = P’s right child
    3. P replaces Q as its parent’s child
    4. P’s right child = Q

    You’re missing the bolded step in your supplied code. I think your problem is you’re treating rotations involving the root node as a special case. Obviously you can’t do this if Q is the root and its parent is null. Try creating a “head” node who’s right node is the root. This allows rotations involving the root to work using normal algorithms.

    public static void rotateRight(Node node) {
        assert(!node.isLeaf() && !node.left.isLeaf());
        final Node child = node.left;
        node.setLeft(child.right);
        if (node.isRightChild())
             node.parent.setRight(child);
        else node.parent.setLeft(child);
        child.setRight(node);
    }
    

    Node that setRight and setLeft keep the parent reference updated as well as updating right and left. The node.isRightNode() call can be just (node.parent.right == node).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on writing a FileSystemWatcher using Mono but for some reason the callback
I am working on writing a Bash function to start a server that needs
I've been working on writing a library in my spare time to familiarize myself
I'm working on writing a test to be added to an automated test script,
I'm working on writing a non-trivial unit test (perhaps better called a UI test?).
I'm working on writing a splash screen that returns a game mode (int) and
I'm working on writing a snippet for ModX that will find all document with
I'm working on writing a function in Clojure that will process a file character
SO I am working on writing an inline assembly function in c that reverses
I'm working on writing a Pong game and I've run across a problem. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.