BFS-DFS概要总结

BFS, DFS最概要的总结

转自stackoverflow

DFS:

1
2
3
4
5
6
list nodes_to_visit = {root};
while( nodes_to_visit isn't empty ) {
currentnode = nodes_to_visit.take_first();
nodes_to_visit.prepend( currentnode.children );
//do something
}

BFS:

1
2
3
4
5
6
list nodes_to_visit = {root};
while( nodes_to_visit isn't empty ) {
currentnode = nodes_to_visit.take_first();
nodes_to_visit.append( currentnode.children );
//do something
}

注意唯一的区别就是DFS是prepend,添加在最前。而BFS是append,添加在最后。