python 短路法提高二叉堆插入效率

在学习 《problem solving with algorithms and data structure using python》 中的二叉堆时,其插入数据方法是将这个数据放在列表的尾部,然后通过一次次与父节点进行比较,并且交换,实现顺序的改变,原代码如下:

二叉堆插入新数据

    def insert(self, newItem):
        self.heapList.append(newItem)
        self.currentSize = self.currentSize + 1
        self.percUp(self.currentSize)

    def percUp(self, index):
        while index // 2 > 0:
            if self.heapList[index] < self.heapList[index//2]:
                tmp = self.heapList[index // 2]
                self.heapList[index // 2] = self.heapList[index]
                self.heapList[index] = tmp

            index = index // 2

percUp 方法,将新元素逐级对比后,向上移动。然而在分析代码过程中发现,当新代码通过数次交换后,不再大于父节点的值,而新的下标取的是父节点。由于插入新数据是在已构建好的数据顺序基础上进行,原有的数据已经是二叉堆,再进一步检查,则不会发生任何交换。
因此,通过设置短路,将检查中断,返回调用处,而不是从底层检查到根节点。实现代码如下:

二叉堆短路插入检查

    def insert(self, newItem):
        self.heapList.append(newItem)
        self.currentSize = self.currentSize + 1
        self.percUp(self.currentSize)

    def percUp(self, index):
        while index // 2 > 0:
            if self.heapList[index] < self.heapList[index//2]:
                tmp = self.heapList[index // 2]
                self.heapList[index // 2] = self.heapList[index]
                self.heapList[index] = tmp

                index = index // 2

            # if more than parent ,quit
            else:
                return

原文见《problem-solving-with-algorithms-and-data-structure-using-python 中文版》
二叉堆的实现

也许仅仅是性能小小的提升,但对于海量数据处理处理而言,仍是比较可观的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值