Java数据结构--二叉堆

堆排序的过程:

895536-20160318132404490-2043248502.png

895536-20160318132410224-97336863.png

895536-20160318132416178-1365874358.png

895536-20160318132421006-1948155848.png

895536-20160318132426490-1842857322.png

895536-20160318132431662-1517739033.png

895536-20160318132437490-2143652067.png

895536-20160318132442428-6449493.png

895536-20160318132447459-172621453.png

895536-20160318132451928-540224526.png

895536-20160318132457240-374349078.png

/**
 * 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;
    }
}

转载于:https://www.cnblogs.com/Salaku/p/5268632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值