首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python中实现堆遍历

问如何在Python中实现堆遍历
EN

Stack Overflow用户
提问于 2016-07-19 19:19:03
回答 2查看 1.5K关注 0票数 0

我刚刚在python中创建了一个堆类,并且仍然在遍历Tree。当我调用inoder function时,我得到了错误,None is not in the list说。在我的三个遍历函数中,它们都需要left和right函数。我假设问题在这两个函数中,但我不知道如何修复它。

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-19 19:47:00

当你跟随树到它的左子,而没有左子,那么你应该完成这条路。您可以保证最终没有一个左子,这应该是结束递归的基本情况。

相反,您查找左子值(使用其索引),然后从该值反向计算您已经拥有的索引(希望没有重复的索引)。由于最终将没有剩下的子程序,所以当您试图反向计算None的索引时,您会发现"self.heapArray“中没有任何一个,并且得到了"None不在列表中”的确切错误。

票数 0
EN

Stack Overflow用户

发布于 2016-07-19 19:50:30

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38466796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档