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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:37:12+00:00 2026-06-02T07:37:12+00:00

I built a binary tree using a root pointer. Isn’t the root pointer supposed

  • 0

I built a binary tree using a root pointer.

Isn’t the root pointer supposed to be changed in all functions, since I declared it globaly? How do I achieve this?

Thanks

//Pseudocode (left, right pointers declared elsewhere)

Node * root = new Node;


Node * BST::BuildTree(int label)
{
         root->left = changed;
        root->right = changed;
}

Node * BST::GetNode(int label)
{


  BTNode *ptr = root;

  cout << root->right;  //This gives me a seg fault since root is still NULL and not changed
}
  • 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-02T07:37:13+00:00Added an answer on June 2, 2026 at 7:37 am

    This is because you haven’t assign a valid address to root, in your code.

    It should point to a valid node:

    void BST::CreateRoot()
    {
      root = new (std::nothrow) Node;
    }
    

    And in C++, you should use unnamed namespace when you want to use a “global variable”. The benefit is that it’ll prevent name conflicts which can easily be introduced by global variables.

    namespace
    {
      Node * root = NULL;
    }
    

    If you’re coding in C, I’d end up here. But since you’re using C++, there’s one thing more.

    You should try your best to avoid using global variables, especially when you have lots of them which depend on each other. Usually, you can create a singleton class.

    But I don’t think your case is that complicated to use a singleton. Just add an extra input parameter to your functions to indicate which node you want to operate on.

    // @param1(node) can be root or whatever node you want
    Node * BST::BuildTree(Node *node, int label)
    {
      node->left = changed;
      node->right = changed;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so I have a Binary Search Tree built using only C structs and
Is there a built-in binary search tree in .NET 4.0, or do I need
I have a binary tree based mathematical expression parser I built, which works great
Is there a built-in function in R for calling binary operators/functions over a list
I built binary tree with: data Tree a = Empty | Node a (Tree
I am trying to create a complete binary tree using a linked list, instead
Hey all, so I am trying to build a simple binary tree that has
I'm looking for a built-in Binary Search Tree implementation in .NET 4. Is there
I need to build a list of all the file extensions of binary files
root@everton-laptop:/opt/tinyos-1.x/apps/Blink# make pc compiling Blink to a pc binary ncc -o build/pc/main.exe -g -O0

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.