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

  1 /**
  2  * 数据结构-堆。 自动增长
  3  *
  4  * @author caiyao  5  */
  6 public class Heap<T extends Comparable> {
  7
  8     private Object[] node;
  9
 10     private static final int DEFAULT_SIZE = 10;
 11
 12     private int size = 0;
 13
 14     private int capacity;
 15
 16     private Type type;
 17
 18     public Heap(Type type){
 19         this(type,DEFAULT_SIZE);
 20     }
 21
 22     public Heap(Type type, int initCapacity){
 23         node = new Object[initCapacity];
 24         this.capacity = initCapacity;
 25         this.type = type;
 26     }
 27
 28     /**
 29      * 插入
 30      * @param newNode
 31      */
 32     public void insert(T newNode){
 33         ensureCapacity(size + 2); // 新节点和空着的0号节点
 34         node[size + 1] = newNode;
 35         upAdjust();
 36         size ++;
 37     }
 38     private void upAdjust(){
 39         for(
 40                 int currentNodeIndex = size + 1;
 41                 (
 42                         currentNodeIndex > 1 && ((T)node[currentNodeIndex]).compareTo(node[currentNodeIndex / 2]) < 0 && type == Type.MIN
 43                 ) ||
 44                 (
 45                         currentNodeIndex > 1 && ((T)node[currentNodeIndex]).compareTo(node[currentNodeIndex / 2]) > 0 && type == Type.MAX
 46                 );
 47                 currentNodeIndex = currentNodeIndex / 2
 48                 ){
 49             Object tempValue = node[currentNodeIndex];
 50             node[currentNodeIndex] = node[currentNodeIndex / 2];
 51             node[currentNodeIndex / 2] = tempValue;
 52         }
 53     }
 54     private void ensureCapacity(int newSize){
 55         if(newSize > DEFAULT_SIZE && newSize > this.capacity){
 56             grow();
 57         }
 58     }
 59     private void grow(){
 60         int newSize = capacity + (capacity >> 1); // 扩大50%容量
 61         node = Arrays.copyOf(node,newSize);
 62     }
 63     /**
 64      * 返回堆顶
 65      * @return
 66      */
 67     public T top(){
 68         return (T)node[0];
 69     }
 70
 71     /**
 72      * 返回堆顶并从堆中移除
 73      * @return
 74      */
 75     public T pop(){
 76         T top = (T)node[0];
 77         downAdjust();
 78         node[size] = null;
 79         return top;
 80     }
 81     private void downAdjust(){
 82         node[0] = node[size - 1];
 83         for(
 84                 int currentNode = 1;
 85                 ;
 86                 ){
 87                 // 小根堆 + 左子树
 88                 if(type == Type.MIN && currentNode * 2 <= size && ((T)node[currentNode * 2]).compareTo(node[currentNode]) < 0){
 89                     Object tempValue = node[currentNode];
 90                     node[currentNode] = node[currentNode * 2];
 91                     node[currentNode * 2] = tempValue;
 92                     currentNode = currentNode * 2;
 93                 }
 94                 // 小根堆 + 右子树
 95                 else if(type == Type.MIN && currentNode * 2 + 1 <= size && ((T)node[currentNode * 2 + 1]).compareTo(node[currentNode]) < 0){
 96                     Object tempValue = node[currentNode];
 97                     node[currentNode] = node[currentNode * 2 + 1];
 98                     node[currentNode * 2 + 1] = tempValue;
 99                     currentNode = currentNode * 2 + 1;
100                 }
101                 // 大根堆 + 左子树
102                 else if(type == Type.MAX && currentNode * 2 <= size && ((T)node[currentNode * 2]).compareTo(node[currentNode]) > 0){
103                     Object tempValue = node[currentNode];
104                     node[currentNode] = node[currentNode * 2];
105                     node[currentNode * 2] = tempValue;
106                     currentNode = currentNode * 2;
107                 }
108                 // 大根堆 + 右子树
109                 else if(type == Type.MAX && currentNode * 2 + 1 <= size && ((T)node[currentNode * 2 + 1]).compareTo(node[currentNode]) > 0){
110                     Object tempValue = node[currentNode];
111                     node[currentNode] = node[currentNode * 2 + 1];
112                     node[currentNode * 2 + 1] = tempValue;
113                     currentNode = currentNode * 2 + 1;
114                 }
115                 else{
116                     break;
117                 }
118         }
119     }
120     /**
121      * 遍历
122      */
123     public void traverse(){
124         for(int i = 1; i <= size; i ++){
125             System.out.println(node[i]);
126         }
127     }
128     public enum Type {
129         MIN,
130         MAX
131     }
132
133     public static void main(String[] args){
134         Heap demo = new Heap<Integer>(Type.MIN);
135         demo.insert(1);
136         demo.insert(10);
137         demo.insert(8);
138         demo.insert(18);
139         demo.insert(2);
140         demo.insert(6);
141         demo.insert(9);
142         demo.insert(0);
143         demo.insert(0);
144         demo.insert(0);
145         demo.insert(0);
146         demo.insert(0);
147         demo.traverse();
148     }
149 }

转载于:https://www.cnblogs.com/caiyao/p/9362212.html

数据结构-堆 Java实现相关推荐

  1. java堆 数据结构 堆_Java中的紧凑堆外结构/组合

    java堆 数据结构 堆 在上一篇文章中,我详细介绍了代码对主内存的访问方式的含义. 从那时起,我就在Java中可以做什么以实现更可预测的内存布局提出了很多疑问. 有些模式可以使用数组支持的结构来应用 ...

  2. java堆 数据结构 堆_快速堆数据结构

    java堆 数据结构 堆 In this tutorial, we'll be discussing and implementing Heap data structures in Swift. 在 ...

  3. 经典数据结构——堆的实现

    一.完全二叉树 堆是一种完全二叉树,什么是完全二叉树? 简单的说,一棵满二叉树表示的是所有节点全部饱和,最后一层全部占满: 而完全二叉树指的是满二叉树的最后一层,所有叶子节点都从左往顺序排满: 完全二 ...

  4. 【数据结构】Java实现常用数据结构

    [数据结构]Java算法基础 一.前言 KMP算法 汉诺塔 八皇后(分治算法) 马踏棋盘算法(骑士周游问题)图的深度优先算法+贪心算法优化 第一章.稀疏数组和队列 1.1 稀疏数组 基本介绍 当一个数 ...

  5. 递归下降分析器的设计java_数据结构(Java版)教与学(48和60学时教学大纲)

    baba<数据结构>课程教学大纲课程中文名称: 数据结构.课程英文名称:Data Structures.课程类别:专业基础课 必修.课程学分数:4(16学时为1学分)课程学时数:讲课48学 ...

  6. 【数据结构】Java版

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

  7. 算法和数据结构(Java语言)

    算法和数据结构(Java语言) 持续更新中- 线性结构和非线性结构 线性结构 线性结构作为最常用的数据结构,其特点是数据元素之间存在一对一的线性关系 线性结构有两种不同的存储结构,即顺序存储结构和链式 ...

  8. 尚硅谷Java数据结构和java算法,韩顺平数据结构和算法课后作业01

    尚硅谷Java数据结构和java算法,韩顺平数据结构和算法课后作业第一题 要求: 1)在前面的基础上,将稀疏数组保存到磁盘上,比如map.data 2) 恢复原来的数组时,读取map.data进行恢复 ...

  9. 数据结构——堆(C++)

    数据结构--堆 文章目录 数据结构--堆 堆与堆排序 二叉树 满二叉树 完全二叉树 二叉堆 堆的存储 堆的插入删除 具体的实现 堆排序 堆与堆排序 堆(heap)分为二叉堆.二项式堆.斐波那契堆,堆是 ...

最新文章

  1. Linux终端运行fasterrcnn,对yolo与fasterrcnn anchors的理解
  2. ThreadLocal为什么要使用弱引用和内存泄露问题
  3. 如何重新划分swap分区
  4. 使用docker toolbox 在windows上搭建统一环境
  5. Hibernate two table same id
  6. 固定资产增值和减值操作
  7. lintcode-93-平衡二叉树
  8. elementui分页组件按钮样式修改_Vue使用Elementui修改默认最快方法!
  9. 12. OD-XOFTSPY监控系统软件,通过windows API进行破解(双重验证,聪明的加密师)
  10. 李飞飞李佳“逆风翻盘”:谷歌全新AutoML产品发布,AI客服规模化商用
  11. 分析java 嵌套类与内部类
  12. 保存/读取图片到数据库
  13. 大一c语言编程上机题库,C语言上机题库Word版
  14. 博客外链怎么做?利用博客做外链还有效嘛
  15. vs2015遇到找不到kernel32.lib,无法解析的外部符号 __imp__printf的问题
  16. 2021年全新UI界面1:1仿皮皮虾APP段子
  17. 房价下跌的“理由”越来越多
  18. oracle中distance用法,distance的用法和短语例句
  19. 【C语言】逗号表达式
  20. 安卓虚拟键盘_逍遥安卓模拟器对电脑配置有什么要求

热门文章

  1. 使用DOM操纵样式表
  2. IOS之Objective-C学习 ARC下的单例模式
  3. Mysql:SQL语句:DML语句
  4. Silverlight 2 控件 SDK 源代码
  5. Celery基本使用---django-celery实现异步HTTP请求
  6. 各种浏览器css不兼容的写法
  7. Java内存运行机制
  8. 2.24 js处理内嵌div滚动条
  9. ActiveX控件在项目中的应用
  10. Spring的起源和背景