数据结构-堆 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 }

 

posted @ 2018-07-24 19:56  Birding  阅读(322)  评论(0编辑  收藏  举报