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

  • Home
  • SEARCH
  • 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 8942947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:39:11+00:00 2026-06-15T11:39:11+00:00

So I’m trying to write a basic red black tree in c++. This is

  • 0

So I’m trying to write a basic red black tree in c++. This is my first real transition from C to C++ and I’m running into a scope issue. I’m trying to use a global variable to keep track of the root of the tree because I’m lazy and don’t want to have every function pass the old or new root back. Anyways, I have the pointer (named root) declared outside of any function or class and in my mind, should be visible to anyone. When I compile, I get:

main.cpp: In member function ‘void node::ins(int)’:
main.cpp:23:4: error: ‘root’ was not declared in this scope
main.cpp: In member function ‘void node::case3()’:
main.cpp:110:4: error: ‘root’ was not declared in this scope

So, the functions inside the class can’t see the global variable but the main function can. What do I have to do to let the functions inside the class see this variable? Thanks in advance. Max

#include <iostream>
#include <cstdio>
using namespace std;

const int BLACK = 0;
const int RED = 1;

class node{
  public:
node *parent = NULL;
node *left = NULL;
node *right = NULL;
int color = RED;
int num = -1;
int isRoot(void){
    if(parent == NULL) return 1;
    else             return 0;
};
void ins(int newNum){
    if(isRoot() && num == -1){//if first insertion
        num = newNum;
        color = BLACK;
        root = this;
    }else if(newNum < num)
        if(left == NULL){
            left = new node;
            left->parent = this;
            left->num = newNum;
            left->fixColor();
        }else
            left->ins(newNum);
    else
        if(right == NULL){
            right = new node;
            right->parent = this;
            right->num = newNum;
            right->fixColor();
        }else
            right->ins(newNum);
};  
int uncleColor(void){
    if(parent != NULL){
        if(parent->parent != NULL){
            node *grandparent = parent->parent;
            if(grandparent->left == parent)
                if(grandparent->right == NULL)
                    return BLACK;
                else
                    return grandparent->right->color;
            else{
                if(grandparent->left == NULL)
                    return BLACK;
                else
                    return grandparent->left->color;
            }
        }else
            cout << "[Error] Grandparent DNE, at root\n";
    }else
        cout << "[Error] Parent DNE, at root\n";
    return -1;
};
void fixColor(void){
    if(isRoot()){
        color = BLACK;
    }else if(color == RED){ 
        if(parent->color == RED){//if parent is black, nothing violated
            int uncle =  uncleColor();
            if(uncle == RED){//uncle is red too
                case1();
                parent->parent->fixColor();//call recursivly on grandparent
            }else if(uncle == BLACK){//uncle is black
                case2();//case 2 will then call case 3
            }else
                cout << "[Error] fixColor uncle color mismatch\n";
        }
    }else
        cout << "[Error] fixcolor node is black?\n";
};
void case1(void){
    node *grandparent = parent->parent;
    node *uncle = NULL;
    if(grandparent->left == parent)
        uncle = grandparent->right;
    else
        uncle = grandparent->left;
    uncle->color = BLACK;
    parent->color = BLACK;
    grandparent->color = RED;
};
void case2(void){
    node *grandparent = parent->parent;
    if(this == parent->right && parent == grandparent->left){
            rotate_left();
            left->case3();
    }else if(this == parent->left && parent == grandparent->right){
            rotate_right();
            right->case3();
    }else
        case3();
};
void case3(void){
    node *grandparent = parent->parent;
    parent->color = BLACK;
    color = RED;
    if(this == parent->left)
            grandparent->rotate_right();
    else
            grandparent->rotate_left();
    if(parent->isRoot())
        root = parent;
};
void rotate_left(void){
    node *grandparent = parent->parent;
    grandparent->left = this;
    parent->right = this->left;
    this->left = parent;
    parent->parent = this;
    parent = grandparent;
};
void rotate_right(void){
    node *grandparent = parent->parent;
    grandparent->right = this;
    parent->left = this->right;
    this->right = parent;
    parent->parent = this;
    parent = grandparent;
};
void del(int val){

};
};

node *root = NULL;

int main(int argc, char **argv){
root = new node;
char READ_task;
int READ_val;
FILE *txtPtr = NULL;
if((txtPtr = fopen(argv[1],"r")) == NULL){printf("[Error] Unable to Load File: '%s'\nExiting...\n",argv[1]);}
else{
    while(fscanf(txtPtr, "%c%d", &READ_task, &READ_val) == 2){
        if(READ_task == 'i')
            root->ins(READ_val);
        else if(READ_task == 'd')
            root->del(READ_val);
        else
            cout << "Instruction from file not i or d\n";
    }
}
return 0;
}
  • 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-06-15T11:39:12+00:00Added an answer on June 15, 2026 at 11:39 am

    You have to declare it in each translation unit that uses it:

    extern node* root;
    

    In your case, you’ll also need a forward declaration:

    class node;
    extern node* root;
    
    class node{
    //..........
    

    Note that this style isn’t idiomatic C++, it’s just C with some C++ features. I’d start learning C++ with a book.

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string

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.