最小堆代码实现

  1 package sort;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 /**
  7  * 最小堆排序
  8  * Created by liuwei on 16/3/7.
  9  */
 10 public class MinHeap {
 11 
 12     private List<Integer> heap = null;
 13 
 14     public  MinHeap(){
 15         heap = new ArrayList<>();
 16     }
 17 
 18     public MinHeap(int[] nums){
 19         heap = new ArrayList<>(nums.length);
 20         for(int i=0; i<nums.length;i++){
 21             heap.add(nums[i]);
 22         }
 23         adjust();
 24     }
 25 
 26     public void addElement(int number){
 27         heap.add(number);
 28 //        adjust();
 29         adjustFromDownToUp();
 30     }
 31 
 32     public int size(){
 33         return heap.size();
 34     }
 35 
 36     public boolean isEmpty(){
 37         for(Integer i : heap){
 38             return false;
 39         }
 40         return true;
 41     }
 42 
 43     public Integer getTop(){
 44         return isEmpty()? null:heap.get(0);
 45     }
 46 
 47     public Integer delTop(){
 48         if(isEmpty()) return null;
 49         int top = heap.remove(0);
 50         if(!isEmpty()){
 51             heap.add(0,heap.get(heap.size()-1));
 52             heap.remove(heap.size()-1);
 53 //            adjust();
 54             adjustFromUpToDown();
 55         }
 56         return top;
 57     }
 58 
 59     private void swap(int i,int j){
 60         int tmp = heap.get(i);
 61         heap.set(i,heap.get(j));
 62         heap.set(j,tmp);
 63     }
 64 
 65     /**
 66      * 全局调整
 67      * */
 68     private void adjust(){
 69         if(size() <= 1) return;
 70         int i = size() - 1;
 71         int parent = 0;
 72         if(i % 2 == 1){
 73             parent = (i -1) / 2;
 74             if(heap.get(i) < heap.get(parent)){
 75                 swap(i,parent);
 76             }
 77             i--;
 78         }
 79         while(i > 0){
 80             parent = (i - 1) / 2;
 81             int value = Math.min(heap.get(i-1),heap.get(i));
 82             if(value < heap.get(parent)){
 83                 if(value == heap.get(i-1)){
 84                     swap(i-1,parent);
 85                 }
 86                 else{
 87                     swap(i,parent);
 88                 }
 89             }
 90             i= i -2;
 91         }
 92     }
 93 
 94     //当删除一个元素的时候,是将最后一个元素,插入到之前第一个元素的位置,该元素后面是有序的,这时候自上向下调整
 95     private void adjustFromUpToDown(){
 96         if(size() < 2) return;
 97         int index = 0;
 98         int left = 2 * index + 1;
 99         int right = 2 * index + 2;
100         while(index < size() ){
101             if(right < size()){
102                 int value = Math.min(heap.get(left),heap.get(right));
103                 if(value < heap.get(index)){
104                     if(value == heap.get(left)){
105                         swap(left,index);
106                         index = left;
107                     }
108                     else{
109                         swap(right,index);
110                         index = right;
111                     }
112                     left = 2 * index + 1;
113                     right = 2 * index + 2;
114                 }
115                 else{
116                     return;
117                 }
118             }
119             else if(left < size()){
120                 if(heap.get(left) < heap.get(index)){
121                     swap(left,index);
122                 }
123                 return;
124             }
125             else{
126                 return;
127             }
128         }
129     }
130 
131     //当增加一个元素的时候,是插入到最后一个位置,这时候,前面是有序的,这时候自下而上的调整
132     private void adjustFromDownToUp(){
133         if(size() < 2) return;
134         int index = heap.size() - 1;
135         int parent = (index - 1) / 2;
136         if(index % 2 == 1){
137             if(heap.get(index) < heap.get(parent)){
138                 swap(index,parent);
139                 index = parent;
140             }
141             else{
142                 return;
143             }
144         }
145         int left = 0, right = 0;
146         while(index > 0){
147             parent = (index - 1)/2;
148             if(index % 2 == 1){
149                 left = index;
150                 right = index + 1;
151             }
152             else{
153                 left = index - 1;
154                 right = index;
155             }
156             int value = Math.min(heap.get(left),heap.get(right));
157             if(value < heap.get(parent)){
158                 if(value == heap.get(left)){
159                     swap(left,parent);
160                 }
161                 else{
162                     swap(right,parent);
163                 }
164                 index = parent;
165             }
166             else{
167                 return;
168             }
169         }
170     }
171 
172 
173     public void show(){
174         for(Integer i : heap){
175             System.out.print(i+" ");
176         }
177         System.out.println();
178     }
179 
180     public static void main(String[] args) {
181         int[] nums = {9,12,17,30,50,20,60,65,4};
182         MinHeap heap = new MinHeap();
183 //        heap.show();
184 //        heap.addElement(49);
185 //        heap.show();
186 //        while(!heap.isEmpty()){
187 //            System.out.print(heap.delTop()+" ");
188 //        }
189 //        System.out.println();
190 
191         for(int i=0; i<nums.length;i++){
192             heap.addElement(nums[i]);
193 //            heap.show();
194         }
195 
196         while(!heap.isEmpty()){
197             System.out.print(heap.delTop()+" ");
198         }
199         System.out.println();
200     }
201 }

 

转载于:https://www.cnblogs.com/nashiyue/p/5251906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值