我要寻找一个非二叉树非递归深度优先搜索算法。任何帮助是非常AP preciated。
I am looking for a Non recursive Depth first search algorithm for a non binary tree. Any help is very much appreciated.
DFS:
list nodes_to_visit = {root};
while( nodes_to_visit isn't empty ) {
currentnode = nodes_to_visit.first();
nodes_to_visit.prepend( currentnode.children );
//do something
}
BFS:
list nodes_to_visit = {root};
while( nodes_to_visit isn't empty ) {
currentnode = nodes_to_visit.first();
nodes_to_visit.append( currentnode.children );
//do something
}
两者的对称性是相当凉爽。
The symmetry of the two is quite cool.
更新:作为指出,第()
删除并返回列表中的第一个元素
Update: As pointed out, first()
removes and returns the first element in the list.
上一篇:圆形无锁缓冲圆形