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:

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

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 

 

posted on 2015-03-27 11:01  Step-BY-Step  阅读(251)  评论(0编辑  收藏  举报

导航