implement min heap
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的更多相关文章
- 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 ...
- Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...
- 纸上谈兵:堆(heap)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 堆(heap)又被为优先队列(priority queue).尽管名为优先队列,但 ...
- 堆(Heap)和二叉堆(Binary heap)
堆(Heap) The operations commonly performed with a heap are: create-heap: create an empty heap heapify ...
- binary heap
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- 纸上谈兵: 堆 (heap)
纸上谈兵: 堆 (heap) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 堆(heap)又被为优先队列(priority ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- What is a heap?--reference
A heap is a partially sorted binary tree. Although a heap is not completely in order, it conforms to ...
- heap creation
There two methods to construct a heap from a unordered set of array. If a array has size n, it can b ...
随机推荐
- 27-ATM+购物车程序
1.需求 本章作业: 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录每 ...
- Openstack入门篇(十七)之Cinder服务-->安装并配置一个本地存储节点
怎样为块存储服务安装并配置存储节点.为简单起见,这里配置一个有一个空的本地块存储设备的存储节点.这个向导用的是 /dev/sdb,此处选用linux-node1节点作为存储节点,需要在vmware中添 ...
- MySQL易忘知识点梳理
一.零碎知识 1.mysql where子句区分大小写:WHERE BINARY 2.判断是否为null,只能用is null,is not null,不能用=null或!=null 3.函数 4.S ...
- Python面向对象之封装、property特性、绑定方法与非绑定方法
一.封装 ''' 1.什么封装 封:属性对外是隐藏的,但对内是开放的(对内是开放的是因为在类定义阶段这种隐藏已经发生改变) 装:申请一个名称空间,往里装入一系列名字/属性 2.为什么要封装 封装数据属 ...
- FileDialog对象
返回表示文件对话框实例的 FileDialog 对象. 语法 expression. FileDialog( _fileDialogType_ ) expression:表示 Application ...
- vim使用技巧(插入,删除,查找,复制,粘贴,剪切)
原文链接:http://blog.csdn.net/qq_38646470/article/details/79643000 编程人员很喜欢的编辑器:vim 先搞清楚vim的三种模式: 1.命令模式: ...
- 【Unity Shader】(六) ------ 复杂的光照(上)
笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题. [Unity Sha ...
- [Unity Shader] 切线空间的法线贴图
切线空间的法线贴图,可以这样理解: #纹理坐标是从0到1,它的坐标是x向右,y向下 #顶点坐标是从-1到-1,坐标是x向右,y向上 1 由表面上某点的切线Tangent.副切线Bitangent.法线 ...
- CocoStuff—基于Deeplab训练数据的标定工具【五、训练成果分析】
一.说明 本文为系列博客第五篇,主要展示训练的结果,以及对训练进行分析. *注:暂未进行大量的数据训练以及IoU测算,目前只做到使用Matlab将训练结果的mat文件可视化. 二. *占坑
- UVA 816 Abbott's Revenge 紫书
紫书的这道题, 作者说是很重要. 但看着题解好长, 加上那段时间有别的事, 磨了几天没有动手. 最后,这道题我打了五遍以上 ,有两次被BUG卡了,找了很久才找到. 思路紫书上有,就缺少输入和边界判断两 ...