JAVA实现一个大根堆

public class TestHeap {


        public int[] elem;
        public int usedSize;

        public TestHeap() {
            this.elem = new int[10];
        }

        /**
         *
         * @param root 每棵子树的开始位置
         * @param len  结束位置
         */
        public void adjustDown(int root,int len) {
            int parent = root;
            int child = 2*parent+1;
            while (child < len) {
                //0、判断是否有左右孩子  有的话 找到最大值 C下标表示最大值下标
                if(child+1<len) {
                    if (this.elem[child]>this.elem[child+1]){
                        child+=1;
                    }
                }
                //代码指向到这里,c表示最大值下标
                if(this.elem[child] > this.elem[parent]) {
                    //交换
                    int tmp=this.elem[child];
                    this.elem[child]=this.elem[parent];
                    this.elem[parent]=tmp;
                    parent=child;
                    child=parent*2+1;
                }else {
                    break;
                }
            }


        }

        public void createHeap(int[] array) {
            for (int i = 0; i < array.length; i++) {
                this.elem[i] = array[i];
                this.usedSize++;
            }
            //i:每棵子树的根节点下标
            for (int i = (this.usedSize-1-1)/2; i >= 0 ; i--) {
                adjustDown(i,this.usedSize);
            }


        }
        public void show(){
            for (int i = 0; i <this.usedSize ; i++) {
                System.out.println(this.elem[i]+" ");
            }
        }
        public void push(int val) {
            //0、堆是否是满的--》扩容
             if(isFull()) {
                 this.elem= Arrays.copyOf(this.elem,this.elem.length*2);
            }
            //1、放到数组的最后一个位置
              val=this.elem[this.usedSize];
            //2、进行调整
            adjustUp(this.usedSize);
            this.usedSize++;
        }

        public void adjustUp(int child) {
            int parent = (child-1)/2;
            while (child > 0) {
                if (this.elem[child]>this.elem[parent]){
                    int tmp=this.elem[child];
                    this.elem[child]=this.elem[parent];
                    this.elem[parent]=tmp;
                    child=parent;
                    parent=(child-1)/2;
                }else {
                    break;
                }
            }
        }
        public boolean isFull() {
            return this.usedSize==this.elem.length;
        }
        public void  pop(){
            //判断为空
            if (isEmpty()){
                return;
            }
            int tmp=this.elem[0];
            this.elem[0]=this.elem[this.usedSize-1];
            this.elem[this.usedSize-1]=tmp;
            this.usedSize--;
            adjustDown(0,this.usedSize);

        }
        public  boolean isEmpty(){
            if (this.usedSize==0){
                return   true ;
            }
            return false;
        }
        public int peek(){
            if (isEmpty()){
                return -1;
            }
            return this.elem[0];

        }
        public void heapSort() {//堆排序
            int end = this.usedSize-1;
            while (end > 0) {
                int tmp=this.elem[0];
                this.elem[0]=this.elem[end];
                this.elem[end]=tmp;
                adjustDown(0,end--);
            }
        }


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值