【发布时间】:2016-12-30 14:08:45
【问题描述】:
我需要一些关于我正在尝试编写的代码的帮助。我的想法是调用一个递归函数(在主函数中),它计算给定根的子节点,我通过引用传递该根,但是当递归函数结束时,子节点不会被存储。如果有人能告诉我我做错了什么,我将不胜感激。代码如下:
struct TreeNode
{
string label;
TreeNode* parent;
vector<TreeNode*> children;
string value;
};
void id3(vector<string> attributes, vector<int> attributes_sizes, vector<Data> set, TreeNode &root)
{
// .. some other code goes here
for(int i = 0; i < current_attribute_size; i++)
{
if(final_entropy[i].second == 0.0 && final_result[i].second.size() > 0)
{
TreeNode child;
child.label = final_result[i].first;
child.parent = &root;
root.children.push_back(&child);
child.value = final_result[i].second[0].node_caps;
}
else if(final_result[i].second.size() > 0 && attributes.size() > 0)
{
TreeNode child;
child.label = final_result[i].first;
child.parent = &root;
root.children.push_back(&child);
id3(attributes, attributes_sizes, final_result[i].second, child);
}
}
return;
}
int main()
{
// .. some other code goes here for the other arguments passed to id3
TreeNode root;
id3(attributes, attributes_sizes, educational, root);
return 0;
}
我不认为其他功能有问题,所以我也不会复制它们。
【问题讨论】: