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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:41:38+00:00 2026-06-18T12:41:38+00:00

I’ve been working on the input from file and think I have the logic

  • 0

I’ve been working on the input from file and think I have the logic right, but my nodes aren’t linking properly. I’m able to set the root correctly and the program is able to walk through the string and load the nodes properly, just not link them. Can anyone help me sort through my logic and figure out the problem?

The input string is (A (B (D G) E) (C () F)).

    struct node
    {
     string data;
     node* left;
     node* right;
    };

    void tree::build_tree(string &input, int i, node *n)
    {
     if(i > input.length())
          return *n = NULL;

     if(input[i] == '(')
     {
      string data; string temp;
      int prev_i = i;

     //get_data retrieves the identifier
     data = get_data(input, temp, i+1);

     //get_data_num retrieves the new position in the string
     i = get_data_num(input, temp, i+1);

     if(input[prev_i] == '('&& input[i] == ')')
     {
      i += 1;
      *n = NULL;
     }
     else
     {
      // Allocate a new node and assign the data and 
      // set the pointer to the branches to null
      *n = new node;
     (*n)->data = data;
     (*n)->left = NULL;
     (*n)->right = NULL;

     if(input[i] == ' ')
     {i += 1; }

     //Pass the address of the nodes
     build_tree(input, i, &(*n)->left);
     build_tree(input, i, &(*n)->right);
     }

   }

   else if(isalnum(input[i]) || input[i] == '_' || input[i] == '-')
   {
     string data; string temp;
     int prev_i = i;

     data = get_data(input, temp, i);
     i = get_data_num(input, temp, i);

     if(input[prev_i] == '('&& input[i] == ')')
     {
      i += 1;
      *n = NULL;
     }
     else
     {
      *n = new node;
      (*n)->data = data;
      (*n)->left = NULL;
      (*n)->right = NULL;

      if(input[i] == ' ')
      { i += 1; }

     build_tree(input, i, &((*n)->left));
     build_tree(input, i, &((*n)->right));
   }
  }

   else if(input[i] == ' ')
   {
    i += 1;
   }

    else if(input[i] == ')')
    {
     i += 1;
     *n = NULL;
    }

    else
    {
     cout << "The input tree is not in the correct format!" << endl;
    }
    }
  • 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-18T12:41:40+00:00Added an answer on June 18, 2026 at 12:41 pm

    I believe the issue is that you are not setting the value of the left and right pointers. You are passing the values of the pointers. You need to pass a pointer to the pointers (left and right), to set the value in the structure. The other alternative is to use references instead of pointers.

    Here are the modifications I came up with for the code you supplied:

    • Changed the call to build_tree for the node argument be a pointer to
      a pointer.
    • Changed assignments of values accordingly.
    • Changed call to build_tree to pass the address of left and right (to
      get a pointer to a pointer).
    • Remove the assignment/conditions to set the root_node. So when you
      call the build_tree you need to pass in the address of root. This
      will set the node just like all of the nodes which follow, so it does not need
      to be a special case.
    • Added assignment of NULL for left and right in case there is not a
      branch (may not need to do this, but I feel it is good practice to
      make sure all items have some initial values).
    void tree::build_tree(string &input, int i, node **n)
    {
    
      if(input[i] == '(')
      {
        string data; string temp;
    
        //get_data retrieves the identifier
        data = get_data(input, temp, i+1);
    
        //get_data_num retrieves the new position in the string
        i = get_data_num(input, temp, i+1);
    
        // Allocate a new node and assign the data and 
        // set the pointer to the branches to null
        *n = new node;
        (*n)->data = data;
        (*n)->left = NULL;
        (*n)->right = NULL;
    
        if(input[i] == ' ')
        { i += 1; }
    
        // Pass the address of the nodes 
        build_tree(input, i, &(*n)->left);
        build_tree(input, i, &(*n)->right);
      }
    
      else if(isalnum(input[i]) || input[i] == '_' || input[i] == '-')
      {
        string data; string temp;
        data = get_data(input, temp, i);
        i = get_data_num(input, temp, i);
    
        *n = new node;
        (*n)->data = data;
        (*n)->left = NULL;
        (*n)->right = NULL;
    
        if(input[i+1] == ' ')
        { i += 1; }
    
        build_tree(input, i, &((*n)->left));
        build_tree(input, i, &((*n)->right));
      }
    
      else if(input[i] == ' ')
      {
        i += 1;
      }
    
      else if(input[i] == ')')
      {
        *n = NULL;
      }
    
      else
      {
       cout << "The input tree is not in the correct format!" << endl;
      }
    }
    

    Then for the initial call,

    build_tree(testString,0,&root);

    Since the get_data and get_data_num were not supplied, I was not able to test the changes which were made.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
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
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The
I have a reasonable size flat file database of text documents mostly saved in
I have a view passing on information from a database: def serve_article(request, id): served_article

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.