如何在Python中实现堆遍历

How to implement heap traversal in Python

我刚刚在 python 中创建了一个堆 class,并且仍在使用树遍历。当我调用 inoder function 时,出现错误 None is not in the list。在我的三个遍历函数中,都需要leftright函数。我假设问题出在这两个函数中,但我不知道如何解决它。

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()

当您沿着树向左走 child,并且没有左边 child,那么您应该完成了那条路。你保证最终会None离开child,这应该是你结束递归的基本情况。

相反,您查找左侧 child 的值(使用其索引),然后从该值反向计算您已有的索引(希望没有重复项)。由于最终会剩下Nonechild,当你试图逆向计算None的索引时,你会发现"self.heapArray"中没有None并且得到确切的错误 "None is not in list"

想象一下当您在叶节点上调用 inorder 时会发生什么。它进入 if 语句的主体并尝试获取叶节点的左右子节点——但没有子节点——因此当 self.left(i) 计算为 None 时它会阻塞并被送入index 方法。您需要修改结束递归的方式来检查节点是否有左右子节点。