implement min heap

 1 class MinHeap{
 2     private ArrayList<Integer> arr;
 3     private int DEFAULT_LEN = 10;
 4     public MinHeap(){
 5         arr = new ArrayList<Integer>(DEFAULT_LEN);
 6     }
 7 
 8 //Use an existing array to build min heap
 9     public MinHeap(ArrayList<Integer> input){
10         this.arr = input;
11         buildMinHeap();
12     }
13     
14     public MinHeap(int[] input){
15         arr = new ArrayList<Integer>();
16         for(int i = 0; i < input.length; i ++){
17             arr.add(input[i]);
18         }
19         buildMinHeap();
20     }
21 
22 //因爲須要維持徹底二叉樹的形態,須要先將要插入的結點x放在最底層的最右邊,插入後滿 足徹底二叉樹的特色; 
23 //而後把x依次向上調整到合適位置知足堆的性質.
24 //時間:O(logn)。  「結點上浮」 
25     public void insert(int val){//bot - top
26         arr.add(val);
27         botupModifyHeap(arr.size() - 1);
28     }
29 
30 //當刪除頂點的數值時,原來的位置就會出現一個孔,填充這個孔的方法就是, 
31 //把最後的葉子的值賦給該孔並下調到合適位置,而後該葉子刪除。 
32     public int deleteTop(){//top - bot
33         int result = arr.get(0);
34         arr.set(0, arr.get(arr.size() - 1));
35         arr.remove(arr.size() - 1);
36         upbotModifyHeap(0);
37         return result;
38     }
39 
40     public int swapTop(int val){
41         int result = arr.get(0);
42         arr.set(0, val);
43         upbotModifyHeap(0);
44         return result;
45     }
46 
47     @Override
48     public String toString(){
49         StringBuilder sb = new StringBuilder();
50         for(int i = 0; i < arr.size(); i ++){
51             sb.append(arr.get(i) + " ");
52         }
53         return sb.toString();
54     }
55 
56 
57     private void buildMinHeap(){
58         for(int i = arr.size() / 2 - 1; i > -1; i --){//bottom-up build min heap
59             upbotModifyHeap(i);
60         }
61     }
62 
63     private void upbotModifyHeap(int curIndex){//top-bot modify min heap
64         int left = curIndex * 2 + 1;
65         int right = curIndex * 2 + 2;
66         int smallest = curIndex;
67         if(left < arr.size() && arr.get(left) < arr.get(smallest))
68             smallest = left;
69         if(right < arr.size() && arr.get(right) < arr.get(smallest))
70             smallest = right;
71         if(smallest != curIndex){
72             swap( smallest, curIndex);
73             upbotModifyHeap(smallest);
74         }
75     }
76 
77     private void botupModifyHeap(int curIndex){//bottom-up modify min heap
78         if(curIndex == 0) return;
79         int parentIndex = curIndex / 2;
80         if(arr.get(parentIndex) > arr.get(curIndex)){
81             swap(parentIndex,curIndex);
82             botupModifyHeap(parentIndex);
83         }
84     }
85 
86     private void swap(int aa, int bb){
87         int tmp = arr.get(aa);
88         arr.set(aa, arr.get(bb));
89         arr.set(bb, tmp);
90     }
91 }

Test case:app

int[] input = new int[]{7,8,9,10,11,12};ide

MinHeap minHeap = new MinHeap(input);
//        System.out.println(minHeap);
        for(int i = 0; i < input.length; i ++){
            minHeap.insert(i + 1);
            System.out.print(minHeap.deleteTop() + " ");
        }
        for(int i = 0; i < input.length; i ++){
            System.out.print(minHeap.deleteTop() + " ");
        }



Output:
1 2 3 4 5 6 7 8 9 10 11 12 

 

        MinHeap m2 = new MinHeap();
        int[] i2 = new int[]{4,7,9,3,6,5,1,2,8};
        for(int i = 0; i < i2.length; i ++)
            m2.insert(i2[i]);
        for(int i = 0; i < i2.length; i ++)
            System.out.print(m2.deleteTop() + " ");


Output:
1 2 3 4 5 6 7 8 9 
相關文章
相關標籤/搜索