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);
}
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: