数据结构-堆 Java实现。 实现堆自动增长

 /**
* 数据结构-堆。 自动增长
*
* @author caiyao */
public class Heap<T extends Comparable> { private Object[] node; private static final int DEFAULT_SIZE = 10; private int size = 0; private int capacity; private Type type; public Heap(Type type){
this(type,DEFAULT_SIZE);
} public Heap(Type type, int initCapacity){
node = new Object[initCapacity];
this.capacity = initCapacity;
this.type = type;
} /**
* 插入
* @param newNode
*/
public void insert(T newNode){
ensureCapacity(size + 2); // 新节点和空着的0号节点
node[size + 1] = newNode;
upAdjust();
size ++;
}
private void upAdjust(){
for(
int currentNodeIndex = size + 1;
(
currentNodeIndex > 1 && ((T)node[currentNodeIndex]).compareTo(node[currentNodeIndex / 2]) < 0 && type == Type.MIN
) ||
(
currentNodeIndex > 1 && ((T)node[currentNodeIndex]).compareTo(node[currentNodeIndex / 2]) > 0 && type == Type.MAX
);
currentNodeIndex = currentNodeIndex / 2
){
Object tempValue = node[currentNodeIndex];
node[currentNodeIndex] = node[currentNodeIndex / 2];
node[currentNodeIndex / 2] = tempValue;
}
}
private void ensureCapacity(int newSize){
if(newSize > DEFAULT_SIZE && newSize > this.capacity){
grow();
}
}
private void grow(){
int newSize = capacity + (capacity >> 1); // 扩大50%容量
node = Arrays.copyOf(node,newSize);
}
/**
* 返回堆顶
* @return
*/
public T top(){
return (T)node[0];
} /**
* 返回堆顶并从堆中移除
* @return
*/
public T pop(){
T top = (T)node[0];
downAdjust();
node[size] = null;
return top;
}
private void downAdjust(){
node[0] = node[size - 1];
for(
int currentNode = 1;
;
){
// 小根堆 + 左子树
if(type == Type.MIN && currentNode * 2 <= size && ((T)node[currentNode * 2]).compareTo(node[currentNode]) < 0){
Object tempValue = node[currentNode];
node[currentNode] = node[currentNode * 2];
node[currentNode * 2] = tempValue;
currentNode = currentNode * 2;
}
// 小根堆 + 右子树
else if(type == Type.MIN && currentNode * 2 + 1 <= size && ((T)node[currentNode * 2 + 1]).compareTo(node[currentNode]) < 0){
Object tempValue = node[currentNode];
node[currentNode] = node[currentNode * 2 + 1];
node[currentNode * 2 + 1] = tempValue;
currentNode = currentNode * 2 + 1;
}
// 大根堆 + 左子树
else if(type == Type.MAX && currentNode * 2 <= size && ((T)node[currentNode * 2]).compareTo(node[currentNode]) > 0){
Object tempValue = node[currentNode];
node[currentNode] = node[currentNode * 2];
node[currentNode * 2] = tempValue;
currentNode = currentNode * 2;
}
// 大根堆 + 右子树
else if(type == Type.MAX && currentNode * 2 + 1 <= size && ((T)node[currentNode * 2 + 1]).compareTo(node[currentNode]) > 0){
Object tempValue = node[currentNode];
node[currentNode] = node[currentNode * 2 + 1];
node[currentNode * 2 + 1] = tempValue;
currentNode = currentNode * 2 + 1;
}
else{
break;
}
}
}
/**
* 遍历
*/
public void traverse(){
for(int i = 1; i <= size; i ++){
System.out.println(node[i]);
}
}
public enum Type {
MIN,
MAX
} public static void main(String[] args){
Heap demo = new Heap<Integer>(Type.MIN);
demo.insert(1);
demo.insert(10);
demo.insert(8);
demo.insert(18);
demo.insert(2);
demo.insert(6);
demo.insert(9);
demo.insert(0);
demo.insert(0);
demo.insert(0);
demo.insert(0);
demo.insert(0);
demo.traverse();
}
}

数据结构-堆 Java实现的更多相关文章

  1. java数据结构----堆

    1.堆:堆是一种树,由它实现的优先级队列的插入和删除的时间复杂度都是O(logn),用堆实现的优先级队列虽然和数组实现相比较删除慢了些,但插入的时间快的多了.当速度很重要且有很多插入操作时,可以选择堆 ...

  2. 数据结构(java语言描述)

    概念性描述与<数据结构实例教程>大同小异,具体参考:http://www.cnblogs.com/bookwed/p/6763300.html. 概述 基本概念及术语 数据 信息的载体,是 ...

  3. 基本数据结构——堆(Heap)的基本概念及其操作

    基本数据结构――堆的基本概念及其操作 小广告:福建安溪一中在线评测系统 Online Judge 在我刚听到堆这个名词的时候,我认为它是一堆东西的集合... 但其实吧它是利用完全二叉树的结构来维护一组 ...

  4. 20172332 2017-2018-2 《程序设计与数据结构》Java哈夫曼编码实验--哈夫曼树的建立,编码与解码

    20172332 2017-2018-2 <程序设计与数据结构>Java哈夫曼编码实验--哈夫曼树的建立,编码与解码 哈夫曼树 1.路径和路径长度 在一棵树中,从一个结点往下可以达到的孩子 ...

  5. C 数据结构堆

    引言 - 数据结构堆 堆结构都很耳熟, 从堆排序到优先级队列, 我们总会看见它的身影. 相关的资料太多了, 堆 - https://zh.wikipedia.org/wiki/%E5%A0%86%E7 ...

  6. 数据结构:JAVA实现二叉查找树

    数据结构:JAVA实现二叉查找树 写在前面 二叉查找树(搜索树)是一种能将链表插入的灵活性与有序数组查找的高效性结合在一起的一种数据结构. 观察二叉查找树,我们发现任何一个节点大于左子节点且小于其右子 ...

  7. 数据结构与算法——常用高级数据结构及其Java实现

    前文 数据结构与算法--常用数据结构及其Java实现 总结了基本的数据结构,类似的,本文准备总结一下一些常见的高级的数据结构及其常见算法和对应的Java实现以及应用场景,务求理论与实践一步到位. 跳跃 ...

  8. 【数据结构】Java版

    有趣有内涵的文章第一时间送达! 喝酒I创作I分享 生活中总有些东西值得分享 @醉翁猫咪 想你吴亦凡;赵丽颖 - 想你 你是程序猿对吗?会写代码的那种? 我是打字猿?会打代码的那种? 现在告诉大家一个很 ...

  9. 数据结构 - 堆(Heap)

    数据结构 - 堆(Heap) 1.堆的定义 堆的形式满足完全二叉树的定义: 若 i < ceil(n/2) ,则节点i为分支节点,否则为叶子节点 叶子节点只可能在最大的两层出现,而最大层次上的叶 ...

随机推荐

  1. Linux 动态加载共享库

  2. [物理学与PDEs]第3章习题6 Lagrange 坐标下的一维理想磁流体力学方程组的数学结构

    试讨论 Lagrange 形式下的一维理想磁流体力学方程组 (5. 33)-(5. 39) 的类型. 解答: 由 (5. 33), (5. 39) 知 $$\bex 0=\cfrac{\p p}{\p ...

  3. spring Bean的完整生命周期

    spring 容器中的bean的完整生命周期一共分为十一步完成. 1.bean对象的实例化 2.封装属性,也就是设置properties中的属性值 3.如果bean实现了BeanNameAware,则 ...

  4. POJ 2318 TOYS (叉积+二分)

    题目: Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...

  5. vue 报错总结

    关闭vue-cli 默认eslint规则: 找到 build -> webpack.base.config.js ,删除箭头指向代码 1.Newline required at end of f ...

  6. 【原创】大数据基础之ElasticSearch(1)简介、安装、使用

    ElasticSearch 6.6.0 官方:https://www.elastic.co/ 一 简介 ElasticSearch简单来说是对lucene的分布式封装,增加了shard(每个shard ...

  7. R leaflet

    setRepositories()#1 chooseCRANmirror()#2 ibrary(leaflet)#学习地址:http://rstudio.github.io/leaflet/marke ...

  8. 安装和启动tftp-server服务器及可能出现Redirecting to /bin/systemctl restart xinetd.service问题的解决方式

    安装和启动tftp-server服务器及可能出现Redirecting to /bin/systemctl restart xinetd.service问题的解决方式 1)首先,检查服务器已安装的tf ...

  9. Spring Boot项目部署到外部Tomcat服务器

    2017年04月27日 23:33:52 阅读数:7542 前言 Spring Boot项目一般都是内嵌tomcat或者jetty服务器运行,很少用war包部署到外部的服务容器,即使放到linux中, ...

  10. form组件

    def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['hobby'].choices ...