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. Gitlab+Jenkins学习之路(十二)之Maven的私有仓库Nexus

    1.什么是Nexus? 在前面进行maven项目的构建中,可以看到在构建的过程中需要安装maven的依赖插件,如图: 而在maven的默认配置中是在官网的中央仓库和第三方的maven仓库进行下载,速度 ...

  2. ssm 配置事务回滚

    参考:https://blog.csdn.net/Mint6/article/details/78363761 在 applicationContext.xml 中配置好了事务和数据源等必须要用到的配 ...

  3. ModelForm解密

     一.复用model表和字段 models.py文件 class User(models.Model): username = models.CharField(max_length=32) emai ...

  4. Python中的dict字典的用法

    Python中的字典特点: 速度快,内部使用二分查找的方式 可以用来存储大量的关系型数据 字典是无序的 字典的定义方式: dic = dict(name =”zhangsan”,  age = 19) ...

  5. (2017)你最不建议使用的Python Web框架?

    https://www.sohu.com/a/164042813_737973   挺有意思的 经过一周的Django学习,以及对比,最终选定了以Flask入手来学习Python web开发.

  6. [算法总结] 20 道题搞定 BAT 面试——二叉树

    本文首发于我的个人博客:尾尾部落 0. 几个概念 完全二叉树:若二叉树的高度是h,除第h层之外,其他(1~h-1)层的节点数都达到了最大个数,并且第h层的节点都连续的集中在最左边.想到点什么没?实际上 ...

  7. [转载]使用mpvue搭建一个初始小程序

    1. 初始化一个 mpvue 项目 现代前端开发框架和环境都是需要 Node.js 的,如果没有的话,请先下载 nodejs 并安装. 然后打开命令行工具: # 1. 先检查下 Node.js 是否安 ...

  8. docker 下载安装与配置

    # mac离线安装dockerhttps://download.docker.com/mac/stable/24312/Docker.dmg # windows离线安装dockerhttp://mir ...

  9. Spring入门学习笔记(3)——事件处理类

    目录 Spring中的事件处理 Spring内建事件 监听Context事件 Example 自定义Spring事件 Spring中的事件处理 ApplicationContext 是Spring的核 ...

  10. 天马行空云计算(二)-Hardware&Hypervisor介绍

    天马行空云计算系列一介绍了总体抽象视图,本篇展开Hardware&Hypervisor 介绍.如下是介绍大纲: 本篇将基于上述架构从如下方面介绍说明 Linux设备驱动 因为上述提到的一些硬件 ...