0

I just made a heap class in python and am still working in Tree traversal. When I invoked inoder function, I got error said None is not in the list. In my three traversal functions, they all need left and right function. I assume that the problem is in these two functions, but I don't know how to fix it.

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 >= 0):
                    return self.heapArray[p]

            else:
                    return None

    def left(self, i):
            l = (i + 1) * 2 - 1
            l = int(l)
            if(l < len(self.heapArray)):
                    return self.heapArray[l]
            else:
                    return

    def right(self, i):
            r = (i + 1) * 2
            r = int(r)
            if(r < 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] < 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) <= 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()
Allen
  • 59
  • 9
  • It might be easier to have functions to compute leftindex() and rightindex(), and just access the underlying array directly with those indexes when you need the values. You are computing the left-child-index O(1), so just return that, instead of returning the value of the left child, then performing an O(n) search on the list to find the left-child-index which you already know. – Kenny Ostrom Jul 19 '16 at 19:57

2 Answers2

0

When you follow the tree to its left child, and there is no left child, then you should be done with that path. You are guaranteed to eventually get to None left child, which should be your base case to end recursion.

Instead you look up the value of the left child (using its index), then you reverse compute the index you already have from that value (hope there are no duplicates). Since there will eventually be None left child, when you try to reverse compute the index of None, you find there is no None in the "self.heapArray" and get that exact error "None is not in list"

Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
0

Imagine what happens when you call inorder on a leaf node. It enters the body of the if statement and tries to get the left and right children of the leaf node-- but there are no children-- so it chokes when self.left(i) evaluates to None and is fed into the index method. You need to revise the way you end the recursion to check whether the node has a left and right child.

Trevor Merrifield
  • 4,541
  • 2
  • 21
  • 24