【发布时间】:2015-06-04 08:47:45
【问题描述】:
将不胜感激。
我有树形结构,并希望按出现的顺序打印节点,就层次结构而言。
例如我想遍历 N1 的所有子节点:
[N0, N1[N2, N3, N4[N5], N6]
我希望得到
N1 N2 N3 N4 N5
但是我收到了一些不同的东西,使用这个 sn-p :
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> MyGraph;
typedef boost::graph_traits<MyGraph>::vertex_descriptor MyVertex;
class MyVisitor : public boost::default_dfs_visitor
{
public:
void discover_vertex(MyVertex v, const MyGraph& g) const
{
cerr << v << endl;
return;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyGraph g;
boost::add_edge(0, 1, g);
boost::add_edge(0, 2, g);
boost::add_edge(0, 3, g);
boost::add_edge(2, 4, g);
MyVisitor vis;
//2 - N2 node
boost::depth_first_search(g, boost::visitor(vis).root_vertex(2) );
return 0;
}
输出是:
2
0
1
3
4
但我希望(2 个和所有孩子)
2
4
请帮忙。
谢谢。
【问题讨论】:
-
你收到的,究竟是什么?因为那个“sn-p”并没有做任何事情,真的
-
咳咳——抱歉。固定的!完整的 sn-p 添加了输出样本。