title: Level Order Traversal - Tree
body: <p>I need to define a function called <code>level_order_travel</code> which takes a tree as an output, <code>a</code>, and prints a list of all the nodes in the list in level order.</p>
  
  <p>The following code here shows this:</p>
  
  <pre><code>def create_tree(node_list, index=1):
      if index &gt;= len(node_list) or node_list[index] is None:
          return None
      d = node_list[index]
      l = index * 2
      r = l + 1
      tree = BinaryTree(d)
      tree.set_left(create_tree(node_list, l))
      tree.set_right(create_tree(node_list, r))
      return tree
  
  def level_order_travel(a):
  ###
  
  def test():
      list_of_leaves = [None, 10, 5, 15, None, None, 11, 22]
      my_tree = create_tree(list_of_leaves )
  
      print("Breadth first =", level_order_travel(my_tree))
  
  test()
  </code></pre>
  
  <p>This is my BinaryTree class:</p>
  
  <pre><code>class BinaryTree:
  
      def __init__(self, data):
          self.data = data
          self.left = None
          self.right = None
  
      def get_left(self):
          return self.left
  
      def get_right(self):
          return self.right
  
      def set_left(self, tree):
          self.left = tree
  
      def set_right(self, tree):
          self.right = tree
  
      def set_data(self, data):
          self.data = data
  
      def get_data(self):
          return self.data
  
      def create_string(self, spaces): 
          info = ' ' * spaces + str(self.data) 
          if self.left != None: 
              info += '\n(l)' + self.left.create_string(spaces+4) 
          if not self.right == None: 
              info += '\n(r)' + self.right.create_string(spaces+4) 
          return info       
  
      def __str__(self): 
          representation = self.create_string(0) 
          return representation
  </code></pre>
  
  <p>This is my Queue class:</p>
  
  <pre><code>class Queue:
      def __init__(self):
          self.items = []
  
      def is_empty(self):
          return self.items == []
  
      def enqueue(self, item):
          self.items.insert(0,item)
  
      def dequeue(self):
          return self.items.pop()
  
      def size(self):
          return len(self.items)
  
      def peek(self):
          return self.items[self.size() - 1]
  </code></pre>
  
  <p>This is my attempt so far:</p>
  
  <pre><code>def level_order_travel(a):
      root = a.get_data()
      q = Queue()
      q.enqueue(root)
      list_of_leaves = []
      if root is None:
          return []
      else:
          if a.get_left() is not None:
              q.enqueue(a.get_left().get_data())
          if a.get_right() is not None:
              q.enqueue(a.get_right().get_data())
      while q.is_empty() == False:
          list_of_leaves.append(q.dequeue())
      return list_of_leaves
  </code></pre>
  
  <p>This should produce the following output: </p>
  
  <pre><code>[10, 5, 15, 11, 22] 
  </code></pre>
  
  <p>but instead it produces the following output:</p>
  
  <pre><code>[10, 5, 15]
  </code></pre>
  
  <p>Any help is appreciated. Thank you.</p>