2017-11-12 7 views
0

私は、各ノードがその子のベクトルへの参照を持つノードオブジェクトを持っています。私は、ツリーをどのように横断し、特定の条件を満たしている場合に "表示された"ブール値をtrueに設定するかを調べるのに問題があります。私は待ち行列を利用すべきだと思っていたが、正確にはどういうことか分からない。各ノードの子にのみ参照を持つツリーを移動する

これは、ノードクラス

class Tag { 
public: 
enum TAGNAME { 
    HTML, HEAD, BODY, TITLE, DIV, P, BR, SPAN, CONTENT 
}; 

std::vector<Tag*> _children; 
const std::string _name; 
const std::string _id; 
std::string _content; 
const TAGNAME _tagname; 
bool _displayed; 

// Must create tags with the tag name and ID upfront. 
Tag(const std::string& name, const std::string& id = "") : _name(name), _id(id), _content(""), _tagname(TAGNAME::CONTENT), _displayed(false) {} 
Tag(const std::string& name, const TAGNAME& tagname, const std::string& id = "") : _name(name), _id(id), _content(""), _tagname(tagname), _displayed(false) {} 
}; 
+0

[ヘルプページ](http://stackoverflow.com/help)、特に[ここではどのトピックについて聞くことができますか?]]( http://stackoverflow.com/help/on-topic)と[[どのような種類の質問を避けるべきですか?]](http://stackoverflow.com/help/dont-ask)を参照してください。また、[ツアーを受けてください](http://stackoverflow.com/tour)と[良い質問をする方法を読む](http://stackoverflow.com/help/how-to-ask)もご覧ください。最後に、[最小限の完全で検証可能な例](http://stackoverflow.com/help/mcve)の作成方法を学んでください。 –

+0

これをチェックしてください:http://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ – imvpn22

+0

これはバイナリツリーのものですが、私のタグは3人以上の子供を持つことができます。これは私が問題を抱えている、それぞれの子供を横断するところです。 –

答えて

0

再帰は通常、ツリーを走査する最も簡単な方法です。このようなもの:

void check_all(Tag& tag) { 
    // ... check conditions and set _displayed 
    for (Tag* child : tag._children) { 
     check_all(*child); 
    } 
} 
関連する問題