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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:04:14+00:00 2026-06-13T01:04:14+00:00

I’m new to C++ so am still learning. I am trying to write an

  • 0

I’m new to C++ so am still learning. I am trying to write an algorithm to build a tree recursively, I would usually write it according to Method 1 below, however, as when the function returns it makes a (I hope deep) copy of the RandomTreeNode, I am concerned about calling it recursively and would therefore prefer method 2. Am I correct in my thinking?

Method 1

RandomTreeNode build_tree(std::vector<T>& data, const std::vector<funcion_ptr>& functions){
        if(data.size() == 0 || data_has_same_values(data)){
            RandomeTreeNode node = RandomTreeNode();
            node.setData(node);
            return node;
        }

        RandomTreeNode parent = RandomTreeNode();
        vector<T> left_data = split_data_left(data);
        vector<T> right_data = split_data_right(data);
        parent.set_left_child(build_tree(left_data));
        parent.set_right_child(build_tree(right_data));
        return parent;
    }

Method 2

void build_tree(RandomTreeNode& current_node, vector<T> data){
    if(data.size() == 0 || data_has_same_values(data)){
        current_node.setData(node);
    }

    vector<T> left_data = split_data_left(data);
    vector<T> right_data = split_data_right(data);

    RandomTreeNode left_child = RandomTreeNode();
    RandomTreeNode right_child = RandomTreeNode();
    current_node.set_left_child(left_child);
    current_node.set_right_child(right_child);

    build_tree(left_child, left_data);
    build_tree(right_child, right_data);

}
  • 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-13T01:04:15+00:00Added an answer on June 13, 2026 at 1:04 am

    There are several improvements.

    • First, you’re copying a vector. As I understand the name of your functions, you’re splitting a vector in two blocks ([left|right] and not [l|r|lll|r|...]). So, instead of passing a vector each time, you can just pass index to specify the ranges.

    • The method 2, if well implemented, will be more efficient in memory. So, you should improve the idea behind it.

    • Last, you can use an auxilliary function, which will be more suited to the problem (a mix between method 1 and method 2).

    Here is some sample code:

    // first is inclusive
    // last is not inclusive
    void build_tree_aux(RandomTreeNode& current_node, std::vector<T>& data, int first, int last)
    {
        if(last == first || data_has_same_values(data,first,last))
        {
            current_node.setData(data,first,last);
            // ...
        }
    
        // Find new ranges
        int leftFirst = first;
        int leftLast = split_data(data,first,last);
        int rightFirst = leftLast;
        int rightLast = last;
    
        // Instead of copying an empty node, we create the children
        // of current_node, and then process these nodes
        current_node.build_left_child();
        current_node.build_right_child();
    
        // Recursion, left_child() and right_child() returns reference
        build_tree_aux(current_node.left_child(),data,leftFirst,leftLast);
        build_tree_aux(current_node.right_child(),data,rightFirst,rightLast);
        /*
            // left_child() and right_child() are not really breaking encapsulation,
            // because you can consider that the child nodes are not really a part of
            // a node.
            // But if you want, you can do the following:
            current_node.build_tree(data,leftFirst,leftLast);
            // Where RandomTreeNode::build_tree simply call build_tree_aux on the 2 childrens
        */
    }
    
    RandomTreeNode build_tree(std::vector<T>& data)
    {
        RandomTreeNode root;
    
        build_tree_aux(root,data,0,data.size());
    
        return root;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.