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. 【HAOI2016】放棋子

    题面 题解 任意两个障碍不在同一列 要求你放$N$个棋子也满足每行只有一枚棋子,每列只有一枚棋子的限制. 这™不就是个错排吗??? $$ h_i=(n-1)(h_{i-1}+h_{i-2}),h_1= ...

  2. [SHOI2008]仙人掌图

    [SHOI2008]仙人掌图 LG传送门 还不会仙人掌的同学可以看看我对仙人掌知识的一些梳理. 题意就是求仙人掌的直径,直径定义为图中最短路径最长的两点间的最短路径长度. 按照套路,先考虑求树的直径我 ...

  3. Storm 第四章 Storm常见问题

    1.集群如何启动,任务如何执行? java -server nimbus,supervisor client--->createTopology(序列化)--->提交jar到nimbusi ...

  4. Eclipse无法查看源码 提示source not found

    学习项目代码的时候想要看一下Cookie的源码,已经确定下载到了本地,可是Ctrl+左键点进去,提示source not found(如下图),百度了以后,大家普遍认为需要安装反编译插件jad. 看了 ...

  5. Git的原理简介和常用命令

    Git和SVN是我们最常用的版本控制系(Version Control System, VCS),当然,除了这二者之外还有许多其他的VCS,例如早期的CVS等.顾名思义,版本控制系统主要就是控制.协调 ...

  6. 【JUC源码解析】ForkJoinPool

    简介 ForkJoin 框架,另一种风格的线程池(相比于ThreadPoolExecutor),采用分治算法,工作密取策略,极大地提高了并行性.对于那种大任务分割小任务的场景(分治)尤其有用. 框架图 ...

  7. 由 Session 和 Cookie 的区别说起

    Session 和 Cookie 有什么区别? 最近面试被问到这个问题,和面试官一番讨论了解到面试官心里的答案后,我不太满意. 面对上面的问题,如果是刚毕业时的我,一定会毫不犹豫说出 Cookie 是 ...

  8. selenium select 选择下拉框

    实战百度首页设置,浏览偏好设置. 打开首页,在非登录的情况下,查看分析页面元素,我们可以看到,我们首先要点击的是设置, 接着点击,搜索设置, 然后select选择下拉框. select_by_inde ...

  9. buglly热更新集成遇到的那些坑

    首先说明使用热修复的意义,那就是你的apk包发出去了,万一发生了紧急异常需要修复,怎么办?这时候再发包上市场审核,也是有点慢了吧?而且错误发生在apk中,无法通过后台接口修复,这时候你就需要一个强大的 ...

  10. linux的date命令使用指定时间的加减方法与异常

    在一般网页里,date命令减时间方法为: date -d '-100 days' 我的需求是,在指定时间上减8小时.按一般理解来看,命令写成如下样子(有异常错误的写法): date -d " ...