class MinHeap{
private ArrayList<Integer> arr;
private int DEFAULT_LEN = 10;
public MinHeap(){
arr = new ArrayList<Integer>(DEFAULT_LEN);
} //Use an existing array to build min heap
public MinHeap(ArrayList<Integer> input){
this.arr = input;
buildMinHeap();
} public MinHeap(int[] input){
arr = new ArrayList<Integer>();
for(int i = 0; i < input.length; i ++){
arr.add(input[i]);
}
buildMinHeap();
} //由于需要维持完全二叉树的形态,需要先将要插入的结点x放在最底层的最右边,插入后满 足完全二叉树的特点;
//然后把x依次向上调整到合适位置满足堆的性质.
//时间:O(logn)。 “结点上浮”
public void insert(int val){//bot - top
arr.add(val);
botupModifyHeap(arr.size() - 1);
} //当删除顶点的数值时,原来的位置就会出现一个孔,填充这个孔的方法就是,
//把最后的叶子的值赋给该孔并下调到合适位置,然后该叶子删除。
public int deleteTop(){//top - bot
int result = arr.get(0);
arr.set(0, arr.get(arr.size() - 1));
arr.remove(arr.size() - 1);
upbotModifyHeap(0);
return result;
} public int swapTop(int val){
int result = arr.get(0);
arr.set(0, val);
upbotModifyHeap(0);
return result;
} @Override
public String toString(){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < arr.size(); i ++){
sb.append(arr.get(i) + " ");
}
return sb.toString();
} private void buildMinHeap(){
for(int i = arr.size() / 2 - 1; i > -1; i --){//bottom-up build min heap
upbotModifyHeap(i);
}
} private void upbotModifyHeap(int curIndex){//top-bot modify min heap
int left = curIndex * 2 + 1;
int right = curIndex * 2 + 2;
int smallest = curIndex;
if(left < arr.size() && arr.get(left) < arr.get(smallest))
smallest = left;
if(right < arr.size() && arr.get(right) < arr.get(smallest))
smallest = right;
if(smallest != curIndex){
swap( smallest, curIndex);
upbotModifyHeap(smallest);
}
} private void botupModifyHeap(int curIndex){//bottom-up modify min heap
if(curIndex == 0) return;
int parentIndex = curIndex / 2;
if(arr.get(parentIndex) > arr.get(curIndex)){
swap(parentIndex,curIndex);
botupModifyHeap(parentIndex);
}
} private void swap(int aa, int bb){
int tmp = arr.get(aa);
arr.set(aa, arr.get(bb));
arr.set(bb, tmp);
}
}

Test case:

int[] input = new int[]{7,8,9,10,11,12};

MinHeap minHeap = new MinHeap(input);
// System.out.println(minHeap);
for(int i = 0; i < input.length; i ++){
minHeap.insert(i + 1);
System.out.print(minHeap.deleteTop() + " ");
}
for(int i = 0; i < input.length; i ++){
System.out.print(minHeap.deleteTop() + " ");
} Output:
1 2 3 4 5 6 7 8 9 10 11 12
        MinHeap m2 = new MinHeap();
int[] i2 = new int[]{4,7,9,3,6,5,1,2,8};
for(int i = 0; i < i2.length; i ++)
m2.insert(i2[i]);
for(int i = 0; i < i2.length; i ++)
System.out.print(m2.deleteTop() + " "); Output:
1 2 3 4 5 6 7 8 9

implement min heap的更多相关文章

  1. find K maximum value from an unsorted array(implement min heap)

    Maintain a min-heap with size = k, to collect the result. //Find K minimum values from an unsorted a ...

  2. Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java

    Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...

  3. 纸上谈兵:堆(heap)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 堆(heap)又被为优先队列(priority queue).尽管名为优先队列,但 ...

  4. 堆(Heap)和二叉堆(Binary heap)

    堆(Heap) The operations commonly performed with a heap are: create-heap: create an empty heap heapify ...

  5. binary heap

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  6. 纸上谈兵: 堆 (heap)

    纸上谈兵: 堆 (heap)   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 堆(heap)又被为优先队列(priority ...

  7. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  8. What is a heap?--reference

    A heap is a partially sorted binary tree. Although a heap is not completely in order, it conforms to ...

  9. heap creation

    There two methods to construct a heap from a unordered set of array. If a array has size n, it can b ...

随机推荐

  1. 5249: [2018多省省队联测]IIIDX

    5249: [2018多省省队联测]IIIDX 链接 分析: 贪心. 将给定的权值从大到小排序,从第一个往后挨个赋值,考虑第i个位置可以赋值那些树.首先满足前面必须至少有siz[i]个权值没选,如果存 ...

  2. POJ-2299 Ultra-QuickSort (树状数组)

    题目链接:Ultra-QuickSort 题意: 给出了一个序列,序列中有n个数,现在每次操作能交换相邻的两个数,要求操作几次可以将这个序列转换为一个从小到大排序的序列. 题解: 我的解法是先把所有的 ...

  3. [坑况]——webpack搭建前端环境踩过的坑啊

    前言 嘿哈,webpack搭建前端环境踩过的坑啊! 第一个:完全不知所措 webpack4 下面用不了HtmlWebpackPlugin 和 ExtractTextPlugin 解决方案: html- ...

  4. 【PaPaPa】集成B/S主流技术的MVC5项目 - 实干派:说做就做,我们已经起航,你还在观望吗

    我们是谁 我们是C#爱好者,互相分享技术,一起学习一起成长一起做一个项目. 我们是开源爱好者,从我们手上出来的代码都会托管在源代码管理平台(oschina),到目前为止不收费,将来也不会出现任何收费情 ...

  5. Hacknet 玩后感

    这款游戏的主题是黑客模拟.玩家需要帮助雇主搞定各种乱七八糟的需求. 你需要使用各种工具和各种linux命令行来获取对方电脑的Root权限.然后就是各种增删改查... 解密部分设计的太过简单,导致玩的时 ...

  6. docker 一篇文章学习容器化

      什么是镜像?什么是容器?   一句话回答:镜像是类,容器是实例   docker 基本操作命令:   删除所有container: docker rm $(docker ps -a -q)   删 ...

  7. Python 3 利用 Dlib 实现摄像头实时人脸检测和平铺显示

    1. 引言 在某些场景下,我们不仅需要进行实时人脸检测追踪,还要进行再加工:这里进行摄像头实时人脸检测,并对于实时检测的人脸进行初步提取: 单个/多个人脸检测,并依次在摄像头窗口,实时平铺显示检测到的 ...

  8. GIT问题(一)——push冲突

  9. Azure web site和web job的config文件加密方式

    1.分析 由于Azure Web AppService平台的特殊性,所以在C#中原先的config加密方法DataProtectionConfigurationProvider和RSAProtecte ...

  10. kubeadm 线上集群部署(二) K8S Master集群安装以及工作节点的部署

    PS:所有机器主机名请提前设置好 在上一篇,ETCD集群我们已经搭建成功了,下面我们需要搭建master相关组件,apiverser需要与etcd通信并操作 1.配置证书 将etcd证书上传到mast ...