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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:40:36+00:00 2026-05-25T12:40:36+00:00

I start with the top node of a tree 5 layers deep, and recursively

  • 0

I start with the top node of a tree 5 layers deep, and recursively call getvalue() on each one. Each node is linked to two nodes on the next layer. I’m pretty sure that this is not my issue since I double checked the algorithm on paper. Once I get to layer 3, however, it gives me a segmentation fault. With valgrind, I figured out that it was raised when I tried to print the class variable oper. I have no idea where to go with this, so your help is greatly appreciated.
This is the code:

class Node {
    public:
        vector<Node> children;
        long constval;
        char oper;
        void setconst();
        Node();
        void copy(const Node*);
        int getvalue();
    private:
        int mult(int,int);
        int div(int,int);
        int add(int,int);
        int sub(int,int);
};

Node::Node() {
    bool c = false;
    vector<char> operations;
    operations.push_back('m');
    operations.push_back('a');
    operations.push_back('s');
    operations.push_back('d');
    operations.push_back('c');
    constval = rand();
    int randnum = rand() % 5;
    cout << randnum << "\n";
    oper = operations[randnum];
}

int Node::getvalue() {
    cout << oper << '\n';
    if (oper == 'm') {
        return Node::mult(children[0].getvalue(), children[1].getvalue());
    }
    else if (oper == 'd') {
        return Node::div(children[0].getvalue(), children[1].getvalue());
    }
    else if (oper == 'a') {
        return Node::add(children[0].getvalue(), children[1].getvalue());
    }
    else if (oper == 's') {
        return Node::sub(children[0].getvalue(), children[1].getvalue());
    }
    else if (oper == 'c') {
        return constval;
    }
}

EDIT:
Here is my initializing algo:

class Individual {
    public:
        vector< vector<Node> > nodes;
        vector< vector<Node> > getrand();
        void replace(vector< vector<Node> >);
        void mutate(double);
        double run();
        Individual();
};

Individual::Individual() {
    nodes.resize(5);
    nodes[0].resize(1);
    int size = 2;
    for(int i = 1; i < 5; i++) {
        nodes[i].resize(size);
        size = size * 2;
    }
    vector<char> operations;
    operations.push_back('a');
    operations.push_back('s');
    operations.push_back('d');
    operations.push_back('m');
    nodes[0][0].oper = operations[rand() % 4];
    for(int x = 0; x < nodes[4].size(); x++) {
        nodes[4][x].setconst();
    }
    for(int i = 0; i < 4; i++) {
        for(int x = 0; x < nodes[i].size(); x++) {
            nodes[i][x].children.push_back(nodes[i+1][x*2]);
            nodes[i][x].children.push_back(nodes[i+1][x*2+1]);
        }
    }   
}
  • 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-25T12:40:36+00:00Added an answer on May 25, 2026 at 12:40 pm
    vector<Node> children;
    

    I have the same opinion as Andrey; that I also don’t like the use of vector as child nodes container. If your data structure is a simple binary tree, why not simply use

    Node* leftChild;
    Node* rightChild;
    

    as the data members of class Node?

    Also, please provide the code for the creation of your tree. It is likely that you have made some mistake there since segmentation fault is very likely due to improper creation of data structure and you may not have realised it.

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

Sidebar

Related Questions

Some databases support commands such as: SELECT TOP 10 START AT 10 * FROM
I'm building up a grid of images, and I start at the top, creating
To start, I know there are two kinds of JSON serialization currently built into
How to make text view text will start from top. It is showing after
I installed Linpack on a 2-Node cluster with Xeon processors. Sometimes if I start
How can I make the cursor start at the top left of the JTextField?
When did oracle start supporting top: select top ? p2_.PRODUCT_ID from PRODUCT?
Start a new Silverlight application... and in the code behind (in the Loaded event),
Start with a series of MS Office extensions built in C++ as COM objects.
I start by creating a string variable with some non-ascii utf-8 encoded data on

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.