Algorithmic Heights第12题:Building a Heap

"本文介绍如何从给定数组中按照二叉最大堆的性质进行排序,通过从末尾开始"泡泡"每个元素到正确的位置,实现数组的堆化过程。重点讲解了如何利用数组特性模拟二叉树操作,适合理解堆数据结构的基础算法入门。"
摘要由CSDN通过智能技术生成

Problem

binary heap is a binary tree based data structure that is often used to implement priority queues. Binary heaps, in turn, can be easily implemented using an array if the underlying tree is a complete binary tree. The tree nodes have a natural ordering: row by row, starting at the root and moving left to right within each row. If there are  nodes, this ordering specifies their positions  within the array. Moving up and down the tree is easily simulated on the array, using the fact that node number  has parent  and children  and .

The goal of this problem is to build a heap from the given array. For this, go from the end to the beginning of a given array and let each element "bubble up".

Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.

Given: A positive integer  and array  of integers from  to .

Return: A permuted array  satisfying the binary max heap property: for any , .

二进制堆是一个二叉树,通常用于实现基于数据结构的优先级队列。如果基础树是完整的二进制树,则可以使用数组轻松实现二进制堆。树节点具有自然的顺序:逐行,从根开始,并在每一行中从左向右移动。如果有 节点,此顺序指定其位置 在数组中。使用节点号这一事实,可以轻松地在阵列上模拟树的上下移动 有父母  和孩子  和 。

这个问题的目标是从给定的数组中构建一个堆。为此,从给定数组的末尾开始,让每个元素“冒泡”。

资料来源:Dasgupta,Papadimitriou,Vazirani的算法。麦格劳-希尔。2006年

给定:正整数 和数组  来自的整数  至 。

返回:排列数组满足二进制最大堆属性:对于任何, 

Sample Dataset

5
1 3 5 7 2

Sample Output

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值