py-bst icon indicating copy to clipboard operation
py-bst copied to clipboard

levelorder() doesn't work

Open quanhanyu opened this issue 7 years ago • 1 comments

Hi I found that levelorder( ) in bstree.py has some errors.

File "E:\Python27\lib\site-packages\pybst\bstree.py", line 164, in levelorder if visit.left: AttributeError: 'NoneType' object has no attribute 'left'

quanhanyu avatar Aug 19 '17 01:08 quanhanyu

fix it in class BSTree in bstree.py

def levelorder(self):
       """
       T.levelorder(...) -> Sequence. Produces a sequence of the Nodes
       in T, obtained in levelorder.
       """
       q = collections.deque()
       q.appendleft(self.Root)
       lst = []
       while len(q) != 0:
           removed = q.pop()
           lst.append(removed)
           # visit = self.get_node(removed, self.Root)
           # if visit.left:
           #     q.appendleft(visit.left)
           # if visit.right:
           #     q.appendleft(visit.right)

           if removed.left:
               q.appendleft(removed.left)
           if removed.right:
               q.appendleft(removed.right)

       return lst

alexJhao avatar May 25 '21 03:05 alexJhao