基于python的广度优先遍历,使用队列实现。
以下是实现代码
def listDirWidthFirst(path): #根节点首先入队
Q = queue.Queue()
Q.put(path)
while(not Q.empty()):
try:
path_new = Q.get()
print(path_new)
dir_d = os.listdir(path_new)
sorted(dir_d,reverse=False)
for i in dir_d:
p = path_new+"/"+i
Q.put(p) #位于同一目录下入队
except Exception as e:
pass



