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 ...
随机推荐
- mtr语言真是逆天了
实践证明,设计一个语言,还不是简单的解释没一行哦
- HTTP 请求/响应报文结构
请求报文和响应报文都是由以下4部分组成: 1.请求行/响应行 2.请求头/响应头 3.空行 4.消息主体(请求体/响应体) 请求报文结构 请求行 格式为:Method Request-URI HTTP ...
- Docker创建数据卷
当程序在容器中运行时,需要与其他容器中的程序或者容器外部的程序进行文件读写操作时,就需要进行数据的交换:容器内部的文件系统,是一个临时层,当容器停止运行并被删除时,这个临时层就会被一同丢弃:为了达到从 ...
- 行为事件(ActionChains)源码详解
ActionChains简介 actionchains是selenium里面专门处理鼠标相关的操作如:鼠标移动,鼠标按钮操作,按键和上下文菜单(鼠标右键)交互. 这对于做更复杂的动作非常有用,比如悬停 ...
- [操作系统]makefile
makefile文件保存了编译器和连接器的参数选项,还表述了所有源文件之间的关系(源代码文件需要的特定的包含文件,可执行文件要求包含的目标文件模块及库等). 创建程序(make程序)首先读取makef ...
- Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第5节: 同线程回收对象
Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第五节: 同线程回收对象 上一小节剖析了从recycler中获取一个对象, 这一小节分析在创建和回收是同线程的 ...
- mkfs命令详解
mkfs命令-->make filesystem的缩写:用来在特定的分区建立Linux文件系统 [命令作用] 该命令用来在特定的分区创建linux文件系统,常见的文件系统有ext2,ex ...
- BugPhobia开发篇章:绩效管理的层次优化
0x00 :用0x00去书写一段故事 If you weeped for the missing sunset, you would miss all the shining stars 绩效管理,恐 ...
- 奔跑吧DKY——团队Scrum冲刺阶段-Day 5
今日完成任务 谭鑫:继续解决背景音乐的问题,修改游戏中的bug. 黄宇塘:背景图片需重做,开始制作人物图片和背景图. 赵晓海:制作人物图及背景图. 方艺雯:制作人物图,编写博客. 王禹涵:继续解决背景 ...
- 新手学ajax2
今天主要解决了一个困扰两天的ajax问题,就是关于从服务器获取数据时的同步和异步问题 , xhr.open("GET", url,false): 这里有三个参数“GET”表示获取的 ...