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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:45:12+00:00 2026-05-15T19:45:12+00:00

I’m writing a program in C++ that uses genetic techniques to optimize an expression

  • 0

I’m writing a program in C++ that uses genetic techniques to optimize an expression tree.

I’m trying to write a class Tree which has as a data member Node root. The node constructor generates a random tree of nodes with +,-,*,/ as nodes and the integers as leaves.

I’ve been working on this awhile, and I’m not yet clear on the best structure. Because I need to access any node in the tree in order to mutate or crossbreed the tree, I need to keep a dicionary of the Nodes. An array would do, but it seems that vector is the recommended container.

vector<Node> dict;

So the Tree class would contain a vector dict with all the nodes of the tree (or pointers to same), the root node of the tree, and a variable to hold a fitness measure for the tree.

class Tree
    {
    public:
        typedef vector<Node>dict;
        dict v;
        Node *root;
        float fitness;

        Tree(void);
        ~Tree();
    };

class Node
    {
    public:
        char *cargo;
        Node *parent;
        Node *left;
        Node *right;
        bool entry;
        dict v;
        Node(bool entry, int a_depth, dict v, Node *pparent = 0);
                };

Tree::Tree()
    {
     Node root(true,  tree_depth, v);
    };

There seems to be no good place to put typedef vector<Node>dict;, because if it goes in the definition of Tree, it doesn’t know about Node, and will give an error saying so. I havn’t been able to find a place to typedef it.

But I’m not even sure if a vector is the best container. The Nodes just need to be indexed sequentally. The container would need to grow as there could be 200 to 500 Nodes.

  • 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-15T19:45:13+00:00Added an answer on May 15, 2026 at 7:45 pm

    I think a standard Binary Tree should do… here is an example of a (binary) expression tree node:

    const int NUMBER = 0,    // Values representing two kinds of nodes.
              OPERATOR = 1;
    
    struct ExpNode {  // A node in an expression tree.
    
        int kind;        // Which type of node is this?
                         //   (Value is NUMBER or OPERATOR.)
        double number;   // The value in a node of type NUMBER.
        char op;         // The operator in a node of type OPERATOR.
        ExpNode *left;   // Pointers to subtrees,
        ExpNode *right;  //     in a node of type OPERATOR.
    
        ExpNode( double val ) {
              // Constructor for making a node of type NUMBER.
           kind = NUMBER;
           number = val;
        }
    
        ExpNode( char op, ExpNode *left, ExpNode *right ) {
              // Constructor for making a node of type OPERATOR.
           kind = OPERATOR;
           this->op = op;
           this->left = left;
           this->right = right;
        }
    
    }; // end ExpNode
    

    So when you’re doing crossover or mutation and you want to select a random node you just do the following:

    1. Count the number of nodes in the tree (only need to do this ones in the constructor).
    2. Select a random index from 0 to the size of the tree.
    3. Visit each node and subtract 1 from the random index until you reach zero.
    4. Return the node when the index is 0.

    In this case you don’t need to know anything about the parent of the node. So mating/mutation should look like this:

    select nodeX
    select nodeY
        if( Rand(0,1) == 1 )
            nodeY->left = nodeX;
        else
            nodeY->right = nodeX;
    

    And that should be it…

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

Sidebar

Ask A Question

Stats

  • Questions 481k
  • Answers 481k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think I can answer my own question. I was… May 16, 2026 at 6:34 am
  • Editorial Team
    Editorial Team added an answer Check this : http://jqueryui.com/demos/datepicker/dropdown-month-year.html its part of datepicker plug in… May 16, 2026 at 6:34 am
  • Editorial Team
    Editorial Team added an answer change position:fixed; to position:absolute; and add: #top_tab_container { position:relative; }… May 16, 2026 at 6:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.