2017-11-12 1 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). 마지막으로 [Minimal, Complete, Verifiable Example] (http://stackoverflow.com/help/mcve)을 만드는 방법을 배우십시오. –

+0

다음을 확인하십시오. http://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ – imvpn22

+0

이진 트리에 대한 것이지만 내 태그에는 세 개 이상의 자식이있을 수 있습니다. 여기에서 나는 각 어린이를 횡단하는 데 어려움을 겪고 있습니다. –

답변

0

재귀는 일반적으로 트리를 탐색하는 가장 쉬운 방법입니다 노드 클래스에게 있습니다. 이런 식으로 뭔가 :

void check_all(Tag& tag) { 
    // ... check conditions and set _displayed 
    for (Tag* child : tag._children) { 
     check_all(*child); 
    } 
} 
관련 문제