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 4114738
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:29:37+00:00 2026-05-20T22:29:37+00:00

I have been trying to create a red black tree that only implements an

  • 0

I have been trying to create a red black tree that only implements an insert, search, and in-order traversal method so that I can compare it to a similar AVL tree I made previously. I have used all of the algorithms that are found in the Cormen text: Introduction to Algorithms, but for some reason I can’t seem to get it to work correctly.

For example, when I insert a, b, then c and try to do an in-order traversal, I am losing c. I have gone over the algorithms in the text about 10 times to make sure I have everything right and I can’t seem to find any mistakes.

Can anyone tell me if I am doing the insertFix() method correctly, as well as the rotations.

Below is my header file to give you an idea of how I set up the nodes:

class RBT
{
private:
    struct node
    {
        int count;                  // counts the number of times the string has been inserted
        std::string  data;          // Storage for String Data
        node         *parent;       // pointer to this node's parent
        node         *LCH;          // pointer to this node's left child
        node         *RCH;          // pointer to this node's right child
        bool         isRed;         // bool value to specify color of node
    };

    node *root;                     // pointer to the tree's root node
    node *nil;                      // nil node used to implement RBT(aka sentinel)
    void traverse(node *t);         // perform an in-order traversal
    int  height(node *p);           // gets height of tree
    int  totalNodes(node *p);       // gets total nodes in tree
    int  totalWords(node *p);       // gets total words in tree
    void insertFix(node *z);        // fixes tree if RBT rules are broken
    void RR(node *z);               // performs Right rotation at z
    void LR(node *z);               // performs Left rotation at z

public:
    int  insert(std::string str);   // tries to add str to tree.  Returns the new count for str
    int  search(std::string str);   // searches for str.  Returns count for str, 0 if not found
    void list();                    // prints in-order traversal of tree  
    void getHeight();               // prints the height of tree
    void getTotal();                // prints the total number of nodes in the tree, as well as total number of words
    void getComparisons();          // prints the number of comparisons used
    RBT();                          // constructor -- just builds an empty tree with a NULL root pointer
    int  numComp;                   // tracks number of comparisons, only counts for search and insert commands
};

And here is my insertFix() method, which is ran after a normal insertion that you would find in any ordinary binary search tree:

void RBT::insertFix(node *z)
{
    // Private method to fix any rules that might be broken in the RBT.
    // Takes a starting node z, as an input parameter and returns nothing,
    // except for a happy feeling knowing the you are not breaking any RBT laws.
    // Definitions of placeholder nodes used in this method:
    // z  = z
    // y  = left or right uncle of z  

    node *y;

    while (z->parent->isRed)
    {
        if(z->parent == z->parent->parent->LCH)
        {
            y = z->parent->parent->RCH;
            if(y->isRed)
            {
                z->parent->isRed = false;
                y->isRed = false;
                z->parent->parent->isRed = true;
                z = z->parent->parent;
            }
            else
            {
                if( z == z->parent->RCH)
                {
                    z = z->parent;
                    RBT::LR(z);
                }
                z->parent->isRed = false;
                z->parent->parent->isRed = true;
                RBT::RR(z);
            }
        }
        else
        {
            y = z->parent->parent->LCH;
            if(y->isRed)
            {
                z->parent->isRed = false;
                y->isRed = false;
                z->parent->parent->isRed = true;
                z = z->parent->parent;
            }
            else
            {
                if( z == z->parent->LCH)
                {
                    z = z->parent;
                    RBT::RR(z);
                }
                z->parent->isRed = false;
                z->parent->parent->isRed = true;
                RBT::LR(z);
            }
        }
    }
    root->isRed = false;
}

Below are my two rotation methods, one is a Right Rotation (RR), and the other is a Left Rotation (LR):

void RBT::LR(node *x)
{
    // Method to perform a Left Rotation at Node z. Takes a node pointer
    // as a parameter. Returns void.

    node *y; // y is x's right child
    y = x->RCH;
    x->RCH = y->LCH;
    if (y->LCH != nil) {y->LCH->parent = x;}
    y->parent = x->parent;
    if (x->parent == nil) {root = y;}
    else
    {
        if (x == x->parent->LCH) {x->parent->LCH = y;}
                            else {x->parent->RCH = y;}
    }
    y->LCH = x;
    x->parent = y;
}

void RBT::RR(node *x)
{
    // Method to perform a Left Rotation at Node z. Takes a node pointer
    // as a parameter. Returns void.

    node *y; // y is x's left child
    y = x->LCH;
    x->LCH = y->RCH;
    if (y->RCH != nil) {y->RCH->parent = x;}
    y->parent = x->parent;
    if (x->parent == nil) {root = y;}
    else
    {
        if (x == x->parent->RCH) {x->parent->RCH = y;}
                            else {x->parent->LCH = y;}
    }
    y->RCH = x;
    x->parent = y;
}

I know this is a lot of code to look over, but I am at my wits end trying to find out what I am doing wrong. I have a feeling I am messing up the pointers somewhere in one of the rotations but I can’t figure out where. Any help would be really appreciated!

  • 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-20T22:29:37+00:00Added an answer on May 20, 2026 at 10:29 pm

    In the insertFix() function, in this section:

            else
            {
                if( z == z->parent->RCH)
                {
                    z = z->parent;
                    RBT::LR(z);
                }
                z->parent->isRed = false;
                z->parent->parent->isRed = true;
                RBT::RR(z);
            }
    

    you should change

                RBT::RR(z);
    

    to

                RBT::RR(z->parent->parent);
    

    Same as the left rotate case in the same function:

                RBT::LR(z);
    

    to

                RBT::LR(z->parent->parent);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to create a simple app that parse an entire channel
I have been trying to create a torrent site but I'm stuck with the
Hello guys! I have been trying to create a if/else statement code in jQuery
Probably a silly question here but I have been trying to find this for
I have been trying to learn Java for the past few days so my
NOTE: I am trying to find the name of the specific LRU algorithm, not
I am trying to use Getopt::Long add command line arguments to my script (seen
I am writing a Java applet which loads dll's created in unmanaged C++. I
So basically when I try to draw more a mesh inside an FBX file

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.