title: How to implement heap traversal in Python
body: <p>I just made a heap class in python and am still working in Tree traversal. When I invoked <code>inoder function</code>, I got error said <code>None is not in the list</code>. In my three traversal functions, they all need <code>left</code> and <code>right</code> function. I assume that the problem is in these two functions, but I don't know how to fix it.</p>
  
  <pre><code>class myHeap:
      heapArray = []
  
      def __init_(self):
              self.heapArray = []
  
      def __str__(self):
              string = " ".join(str(x) for x in self.heapArray)
              return string
  
      def makenull(self):
              self.heapArray = []
  
      def insert(self, x):
              self.heapArray.append(x)
              self.upheap(self.heapArray.index(x))
  
      def parent(self, i):
              p = (i - 1) / 2
              p = int(p)
              if(p &gt;= 0):
                      return self.heapArray[p]
  
              else:
                      return None
  
      def left(self, i):
              l = (i + 1) * 2 - 1
              l = int(l)
              if(l &lt; len(self.heapArray)):
                      return self.heapArray[l]
              else:
                      return
  
      def right(self, i):
              r = (i + 1) * 2
              r = int(r)
              if(r &lt; len(self.heapArray)):
                      return self.heapArray[r]
              else:
                      return None
      def swap(self, a, b):
              temp = self.heapArray[a]
              self.heapArray[a] = self.heapArray[b]
              self.heapArray[b] = temp
  
      def upheap(self, i):
              if(self.parent(i) and self.heapArray[i] &lt; self.parent(i)):
                      p = (i - 1) / 2
                      p = int(p)
                      self.swap(i, p)
                      i = p
                      self.upheap(i)
              else:
                      return
  
      def downheap(self, i):
              if(self.left(i) and self.right(i)):
                      if(self.left(i) &lt;= self.right(i)):
                              n = self.heapArray.index(self.left(i))
                              self.swap(i, n)
                              self.downheap(n)
                      else:
                              n = self.heapArray.index(self.right(i))
                              self.swap(i, n)
                              self.downheap(n)
              elif(self.left(i)):
                      n = self.heapArray.index(self.left(i))
                      self.swap(i, n)
                      self.downheap(n)
              elif(self.right(i)):
                      n = self.heapArray.index(self.right())
                      self.swap(i,n)
                      self.downheap(n)
              else:
                      return
  
      def inorder(self, i):
              if(self.heapArray[i] != None):
                      self.inorder(self.heapArray.index(self.left(i)))
                      print(self.heapArray[i], end=" ")
                      self.inorder(self.heapArray.index(self.right(i)))
  
      def preorder(self, i):
              if(self.heapArray[i] != None):
                      print(self.heapArray[i], end=" ")
                      self.preorder(self.heapArray.index(self.left(i)))
                      self.preorder(self.heapArray.index(self.right(i)))
  
      def postorder(self, i):
              if(self.heapArray[i]!= None):
                      self.postorder(self.heapArray.index(self.left(i)))
                      self.postorder(self.heapArray.index(self.right(i)))
                      print(self.heapArray[i], end=" ")
  
      def min(self):
              return self.heapArray[0]
  
      def deletemin(self):
              self.swap(0, len(self.heapArray) - 1)
              self.heapArray.pop
              self.downheap(0)
  
  def main():
      heap = myHeap()
      heap.insert(0)
      heap.insert(15)
      heap.insert(7)
      heap.insert(8)
      heap.insert(1)
      heap.insert(2)
      heap.insert(22)
      print(heap)
      print(heap.heapArray[0])
      heap.inorder(0)
      heap.preorder(0)
      heap.postorder(0)
  
  if __name__ == "__main__":
      main()
  </code></pre>