Depth First Search - Trees & Graphs

I have a data structure question. Don't know if this is the right place to put it. Anyways here it goes:
I am not understanding why there is a difference in the order we print values in a tree and graph
In a tree we go to the leaf node, start printing from there backtracking towards the root.
In case of a graph, we print the node and then go to the next node(child), till we reach the last connected node.
Can someone tell me if there is anything wrong with my understanding? If not, what is it that I am missing?
In case of a graph, we print the node and then go to the next node(child), till we reach the last connected node.
Not quite.
1
2
3
4
5
6
7
8
9
10
stack.push(head);
while (!stack.empty()){
    current = stack.pop();
    if (visited.contains(current))
        continue;
    visited.add(current);
    callback(current);
    foreach (child in current)
        stack.push(child);
}
For breadth-first, use FIFO instead of LIFO.
Topic archived. No new replies allowed.