Implement heap using Java
public class HeapImpl {
private int CAPACITY = 10;
private int size = 0;
private int[] data;
public HeapImpl() {
data = new int[CAPACITY];
}
//some helper methods
private int getLeftChildIndex(int index) {
return index * 2 + 1;
}
private int getRightChildIndex(int index) {
return index * 2 + 2;
}
private int getParentIndex(int index) {
return (index - 1) / 2;
}
private boolean hasLeftChild(int index) {
return getLeftChildIndex(index) < size;
}
private boolean hasRightChild(int index) {
return getRightChildIndex(index) < size;
}
private boolean hasParent(int index) {
return getParentIndex(index) >= 0;
}
private void ensureCapacity() {
if (size == CAPACITY) {
data = Arrays.copyOf(data, CAPACITY * 2);
CAPACITY = CAPACITY * 2;
}
}
private void swap(int index1, int index2) {
int temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
public int poll() {
if (size == 0) {
throw new IllegalStateException();
}
int item = data[0];
data[0] = data[size - 1];
heapifyDown();
return item;
}
private void heapifyDown() {
int index = 0;
while (hasLeftChild(index)) {
int smallest = getLeftChildIndex(index);
if (hasRightChild(index) && data[getRightChildIndex(index)] < data[smallest]) {
smallest = getRightChildIndex(index);
}
if (data[index] < data[smallest]) {
break;
} else {
swap(smallest, index);
index = smallest;
}
}
}
public void add(int item) {
ensureCapacity();
data[size++] = item;
heapifyUp();
}
private void heapifyUp() {
int index = size - 1;
while (hasParent(index) && data[getParentIndex(index)] > data[index]) {
swap(index, getParentIndex(index));
index = getParentIndex(index);
}
}
// public static void main(String [] args){
// HeapImpl heap = new HeapImpl();
// heap.add(2);
// heap.add(4);
// heap.add(5);
// heap.add(0);
// heap.add(9);
// heap.add(100);
// heap.add(15);
// System.out.println(heap.poll());
// System.out.println(heap.poll());
// System.out.println(heap.poll());
// System.out.println(heap.poll());
//
// }
}
Implement heap using Java的更多相关文章
- Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...
- Stack & Heap in Java
Stack and Heap 都是Java用来在RAM中存放数据的地方.Java自动管理堆和栈,用户不能直接的设置堆或栈. Stack:存在于栈中的数据,其大小与生存周期是确定的,栈中的数据可以共享 ...
- javadataAbout stack and heap in JAVA(2)
改章节个人在上海喝咖啡的时候突然想到的...近期就有想写几篇关于javadata的笔记,所以回家到之后就奋笔疾书的写出来发表了 The stack is much faster than the he ...
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space(Java堆空间内存溢出)解决方法
http://hi.baidu.com/619195553dream/blog/item/be9f12adc1b5a3e71f17a2e9.html问题描述Exception in thread &q ...
- Implement strStr() leetcode java
题目: Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...
- 认识Java Core和Heap Dump
什么是Java Core和Heap Dump Java程序运行时,有时会产生Java Core及Heap Dump文件,它一般发生于Java程序遇到致命问题的情况下. 发生致命问题后,Java进程有时 ...
- Java heap dump触发和分析(转)
为了分析java应用的内存泄漏,使用thread dump往往解决不了问题.使用jstat[eg:jstat-gcutil pid 1000 5]工具查看运行的java应用的heap size,per ...
- 关于java.lang.OutOfMemoryError: Java heap space的错误分析
今天无意间遇到这个错误:java.lang.OutOfMemoryError: Java heap space 问题出现原因:使用a标签实现快速下载[当然已经实现了,但想了想还是要归纳解决这类问题] ...
- java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)
下面是从网上找到的关于堆空间溢出的错误解决的方法: java.lang.OutOfMemoryError: Java heap space ============================== ...
随机推荐
- SSM-Spring-10:Spring中cglib动态代理
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 何为动态代理,就不扯皮了,上一篇博客刚刚提到,那cglib动态代理又怎么写,我拿个和上个例子相似的案例来写 具 ...
- python环境搭建-requests的简单安装(适合新手)
安装完python之后,一定要记住安装后的路径.这是我当前的路径. 下面是requests的安装步骤: 我们这里直接用pip安装(这样比较适合新手),新版python自带pip,python3.6.1 ...
- Rest接口和Thymeleaf的两个坑
spring boot thymeleaf 热部署 在使用spring boot 开发的时候,使用了Thymeleaf 作为前端的模板开发,发现在调试过程中,改动了Thymeleaf模板后,需要重新启 ...
- junit+mock+spring-test构建后台单元测试
from:从0开始,构建前后端分离应用 1. 一些基本概念 1.1 为什么要进行单元测试?我自己的理解是 1.能够快速发现问题.避免衍生BUG的出现 在对一些现有代码进行修改时,或者修改现有B ...
- JAVA WEB项目中生成验证码及验证实例(附源码及目录结构)
[我是一个初学者,自己总结和网上搜索资料,代码是自己敲了一遍,亲测有效,现将所有的目录结构和代码贴出来分享给像我一样的初学者] 作用 验证码为全自动区分计算机和人类的图灵测试的缩写,是一种区分用户是计 ...
- 如何添加“在这里打开PowerShell”到Windows中的上下文菜单
It was only a matter of time, right? Due to my recent infatuation passionate love affair with PowerS ...
- JUnit 异常处理
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testfindAll], {ExactMatcher ...
- BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈
BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...
- CocoaPods 安装 使用&常见操作错误
CocoaPods 安装 使用 1.开启 terminal 2.移除现有 Ruby 默认源 $ gem sources --remove https://rubygems.org/ 3.使用新的源 $ ...
- HttpClient4 TIME_WAIT和CLOSE_WAIT
最近,公司的接口服务器(客户端,向外发送数据)频繁出现了connect timeout 以及readtime out 的情况,经过运维平台检测,并没有网络延时的情况.于是,开始怀疑连接池出了问题. 使 ...