def breadth_first(self, starting_point=None): """ This internal method is a generator that will output breadth first traversal of a binary tree(left child, right child, parent), one value at a time. """ if self.length == 0: raise IndexError("You can't breadth-first traverse an empty Tree.") from dll import DoublyLinkedList unvisited = DoublyLinkedList() if starting_point is None: starting_point = self.root elif self.contains(starting_point) is False: raise IndexError('Starting point is not in the Tree.') unvisited.push(starting_point) visited = [] while unvisited.size() > 0: current = unvisited.shift() if current not in visited: visited.append(current) if current.left: unvisited.push(current.left) if current.right: unvisited.push(current.right) yield current.val
def breadth_first_traversal(self, starting_point): """Steps through the graph breadth-first. Expects a starting point == value of a node in the graph.""" from dll import DoublyLinkedList dll = DoublyLinkedList() if self.has_node(starting_point) is False: raise IndexError("That starting point is not in the graph.") dll.push(starting_point) result = [] while dll.size() > 0: working_node = dll.shift() if working_node not in result: result.append(working_node) for node in self.neighbors(working_node): dll.push(node) return result