数据结构与算法-索引堆及其优化

💝💝💝首先,欢迎各位来到我的博客,很高兴能够在这里和您见面!希望您在这里不仅可以有所收获,同时也能感受到一份轻松欢乐的氛围,祝你生活愉快!

引言

索引堆是一种特殊的数据结构,它结合了堆和索引数组的优点,能够高效地支持动态优先队列操作。索引堆通常用于实现优先队列,特别适用于需要频繁修改元素优先级的场景。本文将深入探讨索引堆的基本原理、实现步骤,并通过具体的案例代码详细说明索引堆的每一个细节。

一、索引堆的基本概念

索引堆是一种特殊的堆数据结构,它包含两个主要部分:

  1. 堆数组:存储堆中的元素。
  2. 索引数组:存储堆中元素在堆数组中的位置。

索引堆具有以下特性:

  • 堆序性质:堆中的元素满足最大堆或最小堆的性质。
  • 索引映射:通过索引数组可以快速定位到堆中元素的位置。
  • 动态调整:支持高效地插入、删除和修改元素的优先级。

二、索引堆的操作

索引堆支持以下主要操作:

  1. 插入元素:将新元素添加到堆数组的末尾,并更新索引数组。
  2. 删除元素:删除指定索引对应的元素,并调整堆和索引数组。
  3. 修改优先级:修改指定索引对应元素的优先级,并调整堆以保持堆序性质。
    在这里插入图片描述

三、索引堆的实现

接下来,我们将通过一个示例来详细了解索引堆的实现步骤。

1. 示例数组

考虑一个整数数组 values = [5, 2, 4, 6, 1, 3],以及对应的索引数组 indices = [0, 1, 2, 3, 4, 5]

2. 索引堆的构建

构建索引堆的过程包括:

  1. 初始化:将数组中的元素按顺序放入堆数组,并将索引放入索引数组。
  2. 构建最大堆:从最后一个非叶子节点开始,向下调整以保持堆序性质。
class IndexedHeap:
    def __init__(self):
        self.heap = []  # 堆数组
        self.indices = []  # 索引数组
        self.index_map = {}  # 索引映射

    def heapify(self, i):
        largest = i
        left = 2 * i + 1
        right = 2 * i + 2

        # 如果左孩子大于根
        if left < len(self.heap) and self.heap[left] > self.heap[largest]:
            largest = left

        # 如果右孩子大于当前最大的
        if right < len(self.heap) and self.heap[right] > self.heap[largest]:
            largest = right

        # 如果最大的不是根
        if largest != i:
            # 更新索引映射
            self.index_map[self.indices[i]] = largest
            self.index_map[self.indices[largest]] = i

            # 交换堆数组和索引数组
            self.heap[i], self.heap[largest] = self.heap[largest], self.heap[i]
            self.indices[i], self.indices[largest] = self.indices[largest], self.indices[i]

            self.heapify(largest)

    def build_max_heap(self):
        # 从最后一个非叶子节点开始
        for i in range(len(self.heap) // 2 - 1, -1, -1):
            self.heapify(i)

    def insert(self, value, index):
        self.heap.append(value)
        self.indices.append(index)
        self.index_map[index] = len(self.heap) - 1
        self.up_heap(len(self.heap) - 1)

    def up_heap(self, i):
        parent = (i - 1) // 2

        # 如果当前节点比父节点大
        if parent >= 0 and self.heap[i] > self.heap[parent]:
            # 更新索引映射
            self.index_map[self.indices[i]] = parent
            self.index_map[self.indices[parent]] = i

            # 交换堆数组和索引数组
            self.heap[i], self.heap[parent] = self.heap[parent], self.heap[i]
            self.indices[i], self.indices[parent] = self.indices[parent], self.indices[i]

            self.up_heap(parent)

    def delete(self, index):
        i = self.index_map[index]
        last_index = len(self.heap) - 1

        # 更新索引映射
        self.index_map[self.indices[last_index]] = i
        del self.index_map[index]

        # 交换堆数组和索引数组
        self.heap[i], self.heap[last_index] = self.heap[last_index], self.heap[i]
        self.indices[i], self.indices[last_index] = self.indices[last_index], self.indices[i]

        # 删除最后一个元素
        del self.heap[last_index]
        del self.indices[last_index]

        # 调整堆
        if i < len(self.heap):
            self.heapify(i)
            self.down_heap(i)

    def down_heap(self, i):
        largest = i
        left = 2 * i + 1
        right = 2 * i + 2

        # 如果左孩子大于根
        if left < len(self.heap) and self.heap[left] > self.heap[largest]:
            largest = left

        # 如果右孩子大于当前最大的
        if right < len(self.heap) and self.heap[right] > self.heap[largest]:
            largest = right

        # 如果最大的不是根
        if largest != i:
            # 更新索引映射
            self.index_map[self.indices[i]] = largest
            self.index_map[self.indices[largest]] = i

            # 交换堆数组和索引数组
            self.heap[i], self.heap[largest] = self.heap[largest], self.heap[i]
            self.indices[i], self.indices[largest] = self.indices[largest], self.indices[i]

            self.down_heap(largest)

# 示例数组
values = [5, 2, 4, 6, 1, 3]
indices = [0, 1, 2, 3, 4, 5]

# 创建索引堆实例
heap = IndexedHeap()

# 插入元素
for i, value in enumerate(values):
    heap.insert(value, indices[i])

# 构建最大堆
heap.build_max_heap()

# 打印堆数组和索引数组
print("Heap Array:", heap.heap)
print("Index Array:", heap.indices)
3. 修改优先级

修改优先级的过程包括:

  1. 更新堆数组:更新指定索引对应的元素值。
  2. 调整堆:根据更新后的值,调整堆以保持堆序性质。
def change_priority(self, index, new_value):
    i = self.index_map[index]
    old_value = self.heap[i]

    # 更新堆数组
    self.heap[i] = new_value

    # 如果新值比旧值大,则上浮调整
    if new_value > old_value:
        self.up_heap(i)
    # 如果新值比旧值小,则下沉调整
    else:
        self.down_heap(i)

# 修改索引为2的元素的优先级
heap.change_priority(2, 8)

# 打印堆数组和索引数组
print("Heap Array after changing priority:", heap.heap)
print("Index Array after changing priority:", heap.indices)
4. 删除元素

删除元素的过程包括:

  1. 交换元素:将要删除的元素与堆的最后一个元素交换。
  2. 调整堆:重新调整堆以保持堆序性质。
# 删除索引为1的元素
heap.delete(1)

# 打印堆数组和索引数组
print("Heap Array after deletion:", heap.heap)
print("Index Array after deletion:", heap.indices)

五、总结

索引堆是一种非常实用的数据结构,尤其适用于需要频繁修改元素优先级的应用场景。在实际编程中,索引堆可以用于实现高效的优先队列,例如在图算法、任务调度等领域有着广泛的应用。通过上述实现,你可以根据自己的需求进一步扩展和优化索引堆的功能。


喜欢博主的同学,请给博主一丢丢打赏吧↓↓↓您的支持是我不断创作的最大动力哟!感谢您的支持哦😘😘😘
打赏下吧

💝💝💝如有需要请大家订阅我的专栏【数据结构与算法】哟!我会定期更新相关系列的文章
💝💝💝关注!关注!!请关注!!!请大家关注下博主,您的支持是我不断创作的最大动力!!!

数据结构与算法相关文章索引文章链接
数据结构与算法-插入排序数据结构与算法-插入排序
数据结构与算法-希尔排序数据结构与算法-希尔排序
数据结构与算法-归并排序数据结构与算法-归并排序
数据结构与算法-随机快速排序数据结构与算法-随机快速排序
数据结构与算法-双路快速排序数据结构与算法-双路快速排序
数据结构与算法-三路排序数据结构与算法-三路排序
数据结构与算法-关于堆的基本存储介绍数据结构与算法-关于堆的基本存储介绍
数据结构与算法-关于堆的基本排序介绍数据结构与算法-关于堆的基本排序介绍
数据结构与算法-优化堆排序数据结构与算法-优化堆排序

❤️❤️❤️觉得有用的话点个赞 👍🏻 呗。
❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!😄😄😄
💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍
🔥🔥🔥Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧!🌙🌙🌙

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟小码

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值