Java数据结构--二叉堆

堆排序的过程:

/**
 * Created by root on 16-3-12.
 */
public class BinaryHeap<E extends Comparable> {
    //internal class
    public static class ArrayList<T> {
        public static int DEFAULT_SIZE = 8;
        T[] data;
        int size;

        //constructor
        public ArrayList() {
            data = (T[]) new Object[DEFAULT_SIZE];
            size = 0;
        }

        public ArrayList(int size) {
            data = (T[]) new Object[size];
            size = 0;
        }

        //isEmpty:
        public boolean isEmpty() {
            return size == 0;
        }
        //add:
        public void add(T x){
            this.set(size,x);
            size++;
        }
        //get
        public T get(int index) {
            if (index > size || index < 0) {
                throw new IndexOutOfBoundsException();
            }
            return data[index];
        }

        //set
        public void set(int index, T target) {
            if (index > data.length || index < 0) {
                throw new IndexOutOfBoundsException();
            }
            data[index] = target;
        }
    }

    //fields
    ArrayList array;

    public boolean isEmpty() {
        return array.size == 0;
    }

    public boolean isFull() {
        return array.size == array.data.length;
    }

    //constructor
    public BinaryHeap() {
        array = new ArrayList();
    }

    public void insert(E target) {
      if(isFull()){
          enlargeArray();
      }
        int hole=array.size;
        while(hole>0){
            int comparison=target.compareTo(array.get(getParent(hole)));
            if(comparison<0){
                array.set(hole,array.get(getParent(hole)));
                hole=getParent(hole);
            }
            else {
                break;
            }
        }
        array.set(hole,target);
        array.size++;
    }

    public Object findMin(){
        return array.get(0);
    }

    //remove min
    public E removeMin(){
        E minItem=(E)findMin();
        E lastItem=(E)array.get(array.size-1);
        array.size--;
        int hole=0;
        for(int child=getChild(hole);child<array.size;child=getChild(hole)){
            array.set(hole,array.get(child));
            hole=child;
        }
        array.set(hole,lastItem);
        return minItem;
    }
    public int getChild(int parent){
        int leftChildIndex=parent*2+1;
        int rightChildIndex=parent*2+2;
        if(rightChildIndex>array.size-1){
            return leftChildIndex;
        }
        E leftChildItem=(E)array.get(leftChildIndex);
        E rightChildItem=(E)array.get(rightChildIndex);
        int comparison=leftChildItem.compareTo(rightChildItem);
        if(comparison<0){
            return  leftChildIndex;
        }
        return rightChildIndex;

    }
    public int getParent(int child){
        return (child-1)/2;
    }

    //enlarge
    public void enlargeArray(){
        ArrayList oldArray=array;
        ArrayList newArray=new ArrayList(array.data.length*2+1);
        for(int i=0;i<oldArray.size;i++){
            newArray.set(i,oldArray.get(i));
        }
        this.array=newArray;
        array.size=oldArray.size;
    }
}
posted @ 2016-03-12 14:21  Salaku  阅读(296)  评论(0编辑  收藏  举报