void depth_first_traversal ( Tree const& tree, typename Tree::node_descriptor node, // Only subtree rooted at this node is traversed PreOp const& pre_op = null_op{}, InOp const& in_op = null_op{}, PostOp const& post_op = null_op{} ) { pre_op(node); auto children = tree.children(node); auto count = children.size(); size_t index = 0; for(auto c : children) { depth_first_traversal(tree, c, pre_op, in_op, post_op); ++index; if(index != count) { in_op(node); } } post_op(node); }
void depth_first_traversal ( Tree const& tree, PreOp const& pre_op = null_op{}, InOp const& in_op = null_op{}, PostOp const& post_op = null_op{} ) { depth_first_traversal(tree, tree.get_root(), pre_op, in_op, post_op); }
void depth_first_traversal(Graph *g, int vertex, Set *s_visited) { int i; if(set_exists(s_visited, vertex) > 0) { return; } set_add(s_visited, vertex); for(i = 0; i < g->size; i++) { if(i == vertex || !is_connected(g, vertex, i)) continue; depth_first_traversal(g, i, s_visited); } printf("Visiting %d\n", vertex); }
void main() { int i, j, k1, k2, w, count; Set *s_visited; Graph *g = init_graph(10); for(i = 0; i < 10; i++) { k1 = (int) rand() % 10; k2 = (int) rand() % 10; w = (int) rand() % 100 + 1; add_edge(g, k1, k2, w); } graph_display(g); s_visited = set_create(); printf("Depth first traveral\n***********************\n"); depth_first_traversal(g, 0, s_visited); }