自己实现优先级队列priorityQuene

package com.ww.class_9_23;

import java.util.Iterator;


//小根堆
public class DIYHeap {
    Integer [] heap;//存储数据
    int count;//存储个数

    public DIYHeap() {
        heap=new Integer[13];
        count=0;
    }
    public void add(Integer a){
        /**
         * 1.判断是否存在元素,不存在,直接插入到根位置
         * 2.若存在元素,插到count位置,进行调整,从下往上调整
         * 3.count加1
         */
        if(count==0){
            heap[0]=a;
        }else{
            sifeUp(a,count);
        }
        count++;

    }
    public Integer remove(){
        /**
         * 1.判断是否存在元素,不存在,返回0
         * 2.存在元素,删除堆顶元素,将最后位置元素放到堆顶,从上向下遍历
         * 3.count减1
         */
        if(count==0){
            return 0;
        }
        Integer oldValue=heap[0];
        //将最后元素调整到0号位置
        Integer v=heap[count-1];
        heap[count-1]=null;
        count--;
        siftDown(v,0);
        return oldValue;
    }
    public Integer peek(){
        if(count==0){
            return 0;
        }else{
            return heap[0];
        }
    }

    /**
     * 从下往上调整
     * @param v 插入的元素
     * @param index  要调整的位置
     */
    public void sifeUp(Integer v,Integer index){
        int parentIndex=0;
        do{
            parentIndex=(index-1)/2;
            if(heap[parentIndex]>v){
                heap[index]=heap[parentIndex];
                index=parentIndex;
            }else{
                break;
            }
        }while(parentIndex>0);
        heap[index]=v;
    }

    private void siftDown(Integer v,Integer index){
        //利用完全二叉树特征,非叶子节点到size/2大小
        int end=(count-1)/2;
        while(index<end){
            //找到左右孩子最小的
            int childIndex=index*2+1;
            Integer child=heap[childIndex];
            if(((childIndex+1)<count)&&child>heap[childIndex+1]){
                child=heap[childIndex+1];
                childIndex=childIndex+1;
            }
            //拿最小孩子节点和父节点比较
            if(v>child){
                //父节点大于最小孩子节点,调整
                heap[index]=child;
                index=childIndex;
            }else{
                break;
            }
        }
        heap[index]=v;
    }



    public static void main(String[] args) {
        DIYHeap diyHeap=new DIYHeap();
        diyHeap.add(11);
        diyHeap.add(22);
        diyHeap.add(12);
        System.out.println(diyHeap.remove());
        System.out.println(diyHeap.remove());
    }
}
/**
 * 大根堆
 */
public class DIYHeapBig {
    Integer[] heap;
    int count;

    public DIYHeapBig() {
        heap=new Integer[13];
        count=0;
    }

    private void add(Integer v){
        if(count==0){
            heap[0]=v;
        }else{
            sifeUp(v,count);
        }
        count++;
    }
    private Integer remove(){
        if(count==0){
            return null;
        }
        Integer oldValue=heap[0];
        Integer v=heap[count-1];
        heap[count-1]=null;
        count--;
        sifeDown(v,0);
        return oldValue;
    }
    private Integer peek(){
        if(count==0){
            return null;
        }else{
            return heap[0];
        }
    }
    private void sifeUp(Integer v,int index){
        int parentIndex=0;
        do{
            //找父节点
            parentIndex=(index-1)/2;
            if(heap[parentIndex]<v){
                heap[index]=heap[parentIndex];
                index=parentIndex;
            }else{
                break;
            }
        }while(parentIndex>0);
        heap[index]=v;
    }
    private void sifeDown(Integer v,int index){
        int end=(count)/2;
        while(index<end){
            int childIndex=index*2+1;
            Integer child=heap[childIndex];
            //找到最大左右孩子结点
            if(((childIndex+1)<count)&&(child<heap[childIndex+1])){
                child=heap[childIndex+1];
                childIndex=childIndex+1;
            }
            //拿最大孩子节点和父节点比较
            if(child>v){
                heap[index]=child;
                index=childIndex;
            }else{
                break;
            }
        }
        heap[index]=v;
    }

    public static void main(String[] args) {
        DIYHeapBig diyHeapBig=new DIYHeapBig();
        System.out.println(diyHeapBig.remove());
        diyHeapBig.add(66);
        diyHeapBig.add(11);
        diyHeapBig.add(22);
        diyHeapBig.add(10);
        System.out.println(diyHeapBig.remove());
        System.out.println(diyHeapBig.remove());
        System.out.println(diyHeapBig.remove());
    }


}

猜你喜欢

转载自blog.csdn.net/iiiiiiiiiooooo/article/details/120909883