zoukankan      html  css  js  c++  java
  • 数据结构-堆 Java实现

    数据结构-堆 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 }
  • 相关阅读:
    codevs 2632 非常好友
    codevs 1213 解的个数
    codevs 2751 军训分批
    codevs 1519 过路费
    codevs 1503 愚蠢的宠物
    codevs 2639 约会计划
    codevs 3369 膜拜
    codevs 3135 River Hopscotch
    数论模板
    JXOJ 9.7 NOIP 放松模拟赛 总结
  • 原文地址:https://www.cnblogs.com/caiyao/p/9362212.html
Copyright © 2011-2022 走看看