文字列のツリーを再帰的に構築する単純な関数があります。関数には文字列「***」が与えられ、結果のツリーは次のようになります。
***
/ | \
X** *X* **X
/ \ / \ / \
XX* X*X XX* *XX X*X *XX
/ \ / \ / \
XXX XXX XXX XXX XXX XXX
問題は、私の関数がツリーの最も左側 (X**、XX*、XXX) しか作成していないことです。
関数は次のとおりです。
//Takes in the string "***"
void buildTree(string tree){
if (tree == "XXX") return;
else{
string newTree;
for (int i=0; i<tree.size(); i++){
if(tree[i] == '*'){
newTree = tree;
newTree[i] = 'X';
cout << newTree << endl;
return buildTree(newTree);
}
}
}
}