1 | //======================================================================= |
---|
2 | // Copyright 2002 Indiana University. |
---|
3 | // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek |
---|
4 | // |
---|
5 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
6 | // accompanying file LICENSE_1_0.txt or copy at |
---|
7 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | //======================================================================= |
---|
9 | |
---|
10 | #include <boost/graph/graph_as_tree.hpp> |
---|
11 | #include <boost/graph/adjacency_list.hpp> |
---|
12 | #include <boost/cstdlib.hpp> |
---|
13 | |
---|
14 | class tree_printer { |
---|
15 | public: |
---|
16 | template <typename Node, typename Tree> |
---|
17 | void preorder(Node, Tree&) { |
---|
18 | std::cout << "("; |
---|
19 | } |
---|
20 | template <typename Node, typename Tree> |
---|
21 | void inorder(Node n, Tree& t) |
---|
22 | { |
---|
23 | std::cout << get(boost::vertex_name, t)[n]; |
---|
24 | } |
---|
25 | template <typename Node, typename Tree> |
---|
26 | void postorder(Node, Tree&) { |
---|
27 | std::cout << ")"; |
---|
28 | } |
---|
29 | |
---|
30 | }; |
---|
31 | |
---|
32 | int main() |
---|
33 | { |
---|
34 | using namespace boost; |
---|
35 | typedef adjacency_list<vecS, vecS, directedS, |
---|
36 | property<vertex_name_t, std::string> > graph_t; |
---|
37 | typedef graph_traits<graph_t>::vertex_descriptor vertex_t; |
---|
38 | |
---|
39 | graph_t g; |
---|
40 | |
---|
41 | vertex_t a = add_vertex(g), |
---|
42 | b = add_vertex(g), |
---|
43 | c = add_vertex(g); |
---|
44 | |
---|
45 | add_edge(a, b, g); |
---|
46 | add_edge(a, c, g); |
---|
47 | |
---|
48 | typedef property_map<graph_t, vertex_name_t>::type vertex_name_map_t; |
---|
49 | vertex_name_map_t name = get(vertex_name, g); |
---|
50 | name[a] = "A"; |
---|
51 | name[b] = "B"; |
---|
52 | name[c] = "C"; |
---|
53 | |
---|
54 | typedef iterator_property_map<std::vector<vertex_t>::iterator, |
---|
55 | property_map<graph_t, vertex_index_t>::type> parent_map_t; |
---|
56 | std::vector<vertex_t> parent(num_vertices(g)); |
---|
57 | typedef graph_as_tree<graph_t, parent_map_t> tree_t; |
---|
58 | tree_t t(g, a, make_iterator_property_map(parent.begin(), |
---|
59 | get(vertex_index, g))); |
---|
60 | |
---|
61 | tree_printer vis; |
---|
62 | traverse_tree(a, t, vis); |
---|
63 | |
---|
64 | return exit_success; |
---|
65 | } |
---|