def levelorder(self):
     temp = self._root
     q = queue()
     print(temp._data, end=' ')
     q.push(temp)
     while not q.is_empty():
         temp = q.pop()
         if temp._left:
             print(temp._left._data, end=' ')
             q.push(temp._left)
         if temp._right:
             print(temp._right._data, end=' ')
             q.push(temp._right)
Ejemplo n.º 2
0
 def bfs(self, source):
     i = source
     q = queue()
     q.push(source)
     l = [0]*self._vertices
     l[i] = 1
     print(i, end= '->')
     while not q.is_empty():
         i = q.pop()
         for j in range(self._vertices):
             if self._adjmatrix[i][j] == 1 and l[j] == 0:
                 print(j, end='->')
                 q.push(j)
                 l[j] = 1