Problem
A 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