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

来源:互联网 发布:签名设计图软件 编辑:程序博客网 时间:2024/06/16 09:24

在学习 《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 中文版》
二叉堆的实现

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