Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im trying to traverse a BFS but I continue to get an error. I believed I have fo

ID: 3913003 • Letter: I

Question

Im trying to traverse a BFS but I continue to get an error. I believed I have followed the instructions correctly but I continue to get an error. Can someone tell me what I am doing wrong and solve my problem with an explatation?

1. create a Q with the root element, and an empty list

2. while there are nodes in the Q grab the first one and add it to the result list

3. if there is a node to the left, add that to the Q

4. if there is a node to the right, add that to the Q

This is the test case I need to pass.

This is the error I am getting AssertionError: Lists differ: [] != [20, 10, 30, > 25, 35]

Explanation / Answer

Execute the following program there is no error in the program: class binary_search_tree: def __init__(self, init=None): self.__value = self.__left = self.__right = None if init: for i in init: self.add(i) def __iter__(self): if self.__left: for node in self.__left: yield (node) yield (self.__value) if self.__right: for node in self.__right: yield (node) def __str__(self): return (','.join(str(node) for node in self)) def BFS(self): if self.__value is None: return [None] Q = [] Q.append(self) while Q: node = Q.pop(0) if node.__left is not None: Q.append(node.__right) if node.__right is not None: Q.append(node.__right) return Q