非递归深度优先搜索算法 [英] Non recursive Depth first search algorithm

查看:218
本文介绍了非递归深度优先搜索算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个非二叉树非递归深度优先搜索算法。任何帮助是非常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.

这篇关于非递归深度优先搜索算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆