BFS_PYTHON_FROM_WEB - unvisited = list(set(visitable)) if...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 def bfs(root,visitable,children=iter): """Iterator traverses a tree in breadth-first order. The first argument should be the tree root visitable should be an iterable with all searchable nodes children should be a function which takes a node and return an iterator of the node's children. """ queue = [] # makes a shallow copy, makes it a collection, removes duplicates
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: unvisited = list(set(visitable)) if root in unvisited: unvisited.remove(root) queue.append(root) while len(queue) > 0: node = queue.pop(0) yield node for child in children(node): if child in unvisited: unvisited.remove(child) queue.append(child) return...
View Full Document

Ask a homework question - tutors are online