数据结构 -- 最大堆

最大堆

最大堆是堆的两种形式之一。
根结点(亦称为堆顶)的关键字是堆里所有结点关键字中最大者,称为大根堆,又称最大堆(大顶堆)。
大根堆要求根节点的关键字既大于或等于左子树的关键字值,又大于或等于右子树的关键字值。

最大堆的添加元素和删除元素过程

在这里插入图片描述

java代码实现

import java.util.Random;

public class MaxHeap<E extends Comparable<E>> {
    private Array<E> arr;

    public MaxHeap(int capactiy) {
        this.arr = new Array<>(capactiy);
    }

    public static void main(String[] args) {

        int n = 100;

        MaxHeap<Integer> maxHeap = new MaxHeap<>(31);
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            int a = random.nextInt(100);
            maxHeap.add(a);
            // System.out.println("add : "+a);
            // maxHeap.printStructure();
        }

        int[] arr = new int[31];
        for (int i = 0; i < 31; i++) {
            System.out.println("=======================================================");
            maxHeap.printStructure();
            arr[i] = maxHeap.extractMax();
            System.out.println("---------删除---" + arr[i] + "-----------------");
            maxHeap.printStructure();
        }


        for (int i = 1; i < 31; i++)
            if (arr[i - 1] < arr[i])
                throw new IllegalArgumentException("Error");

        System.out.println("Test MaxHeap completed.");

    }

    /**
     * 获得堆中元素的个数
     *
     * @return
     */
    public int getSize() {
        return this.arr.getData_size();
    }

    /**
     * 获得堆是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return this.arr.isEmpty();
    }

    /**
     * 获得父节点的索引
     *
     * @param index
     * @return
     */
    private int parent(int index) {
        if (index == 0) {
            throw new IllegalArgumentException("Index=0 doesn't have parent ");
        }
        return (index - 1) / 2;
    }

    /**
     * 获得左孩子的索引
     *
     * @param index
     * @return
     */
    private int leftChild(int index) {
        return index * 2 + 1;
    }

    /**
     * 获得右孩子的索引
     *
     * @param index
     * @return
     */
    private int rightChild(int index) {
        return index * 2 + 2;
    }

    /**
     * 在堆底添加元素
     *
     * @param e
     */
    public void add(E e) {
        this.arr.addLast(e);
        shiftUp(this.getSize() - 1);
    }

    private void shiftUp(int index) {
        while (index > 0 && this.arr.get(this.parent(index)).compareTo(this.arr.get(index)) < 0) {
            this.arr.swap(this.parent(index), index);
            index = this.parent(index);
        }
    }

    /**
     * 获得堆中的最大的元素
     *
     * @return
     */
    public E findMax() {
        if (this.arr.getData_size() == 0) {
            throw new IllegalArgumentException("FindMax failed. the heap is empty");
        }
        return arr.get(0);
    }

    /**
     * 取出堆中最大的元素
     */
    public E extractMax() {
        E max = this.findMax();
        arr.swap(0, this.arr.getData_size() - 1);
        arr.removeLast();
        this.siftDown(0);
        return max;
    }

    private void siftDown(int index) {
        while (this.leftChild(index) < this.getSize()) {
            int leftIndex = this.leftChild(index);
            int rightIndex = leftIndex + 1;
            if (rightIndex < this.getSize() && this.arr.get(rightIndex).compareTo(this.arr.get(leftIndex)) > 0) {
                leftIndex = rightIndex;
            }
            if (this.arr.get(index).compareTo(this.arr.get(leftIndex)) >= 0) {
                return;
            }
            this.arr.swap(index, leftIndex);
            index = leftIndex;
        }
    }


    public void printStructure() {
        System.out.println();
        System.out.println(this.arr.get(0));
        int i = 1;
        while (true) {
            int len = power(i);
            int left = power(i) - 1;
            for (int j = 0; j < len; j++) {
                if (j + left >= this.getSize()) {
                    System.out.println();
                    return;
                }
                System.out.print(this.arr.get(j + left) + "\t");
            }
            i++;
            System.out.println();
        }

    }

    private int power(int p) {
        int ss = 1;
        for (int i = 0; i < p; i++) {
            ss *= 2;
        }
        return ss;
    }

    /**
     * 底层的数组
     *
     * @param <E>
     */
    private class Array<E> {
        private E[] data;
        private int data_size;
        private int capacity;

        public Array(int capacity) {
            this.capacity = capacity;
            this.data = (E[]) new Object[capacity];
            this.data_size = 0;
        }

        /**
         * 获得数组的长度
         *
         * @return
         */
        public int getData_size() {
            return this.data_size;
        }

        public boolean isEmpty() {
            return this.data_size == 0;
        }

        /**
         * 在数组尾部添加元素
         *
         * @param e
         */
        public void addLast(E e) {
            if (this.data_size == this.capacity) {
                this.data_size--;
            }
            this.data[data_size] = e;

            this.data_size++;
        }


        /**
         * 获得 索引为index 的元素
         *
         * @param index
         * @return
         */
        public E get(int index) {
            if (index < 0 || index > this.data_size) {
                throw new IllegalArgumentException("Get failed. illegal index. ");
            }
            return this.data[index];
        }

        public E removeLast() {
            this.data_size--;
            return this.data[this.data_size];
        }

        public void swap(int i, int j) {
            E ss = this.data[i];
            this.data[i] = this.data[j];
            this.data[j] = ss;
        }

    }

}


	99
	95	98	
	78	85	76	98	
	16	75	80	57	43	72	53	98	
	15	2	56	70	14	62	16	31	8	24	16	27	18	51	9	79	
	
	---------删除---99-----------------
	
	98
	95	98	
	78	85	76	98	
	16	75	80	57	43	72	53	79	
	15	2	56	70	14	62	16	31	8	24	16	27	18	51	9	
	=======================================================
	
	98
	95	98	
	78	85	76	98	
	16	75	80	57	43	72	53	79	
	15	2	56	70	14	62	16	31	8	24	16	27	18	51	9	
	---------删除---98-----------------
	
	98
	95	98	
	78	85	76	79	
	16	75	80	57	43	72	53	9	
	15	2	56	70	14	62	16	31	8	24	16	27	18	51	
	=======================================================
	
	98
	95	98	
	78	85	76	79	
	16	75	80	57	43	72	53	9	
	15	2	56	70	14	62	16	31	8	24	16	27	18	51	
	---------删除---98-----------------
	
	98
	95	79	
	78	85	76	53	
	16	75	80	57	43	72	51	9	
	15	2	56	70	14	62	16	31	8	24	16	27	18	
	=======================================================
	
	98
	95	79	
	78	85	76	53	
	16	75	80	57	43	72	51	9	
	15	2	56	70	14	62	16	31	8	24	16	27	18	
	---------删除---98-----------------
	
	95
	85	79	
	78	80	76	53	
	16	75	62	57	43	72	51	9	
	15	2	56	70	14	18	16	31	8	24	16	27	
	=======================================================
	
	95
	85	79	
	78	80	76	53	
	16	75	62	57	43	72	51	9	
	15	2	56	70	14	18	16	31	8	24	16	27	
	---------删除---95-----------------
	
	85
	80	79	
	78	62	76	53	
	16	75	27	57	43	72	51	9	
	15	2	56	70	14	18	16	31	8	24	16	
	=======================================================
	
	85
	80	79	
	78	62	76	53	
	16	75	27	57	43	72	51	9	
	15	2	56	70	14	18	16	31	8	24	16	
	---------删除---85-----------------
	
	80
	78	79	
	75	62	76	53	
	16	70	27	57	43	72	51	9	
	15	2	56	16	14	18	16	31	8	24	
	=======================================================
	
	80
	78	79	
	75	62	76	53	
	16	70	27	57	43	72	51	9	
	15	2	56	16	14	18	16	31	8	24	
	---------删除---80-----------------
	
	79
	78	76	
	75	62	72	53	
	16	70	27	57	43	24	51	9	
	15	2	56	16	14	18	16	31	8	
	=======================================================
	
	79
	78	76	
	75	62	72	53	
	16	70	27	57	43	24	51	9	
	15	2	56	16	14	18	16	31	8	
	---------删除---79-----------------
	
	78
	75	76	
	70	62	72	53	
	16	56	27	57	43	24	51	9	
	15	2	8	16	14	18	16	31	
	=======================================================
	
	78
	75	76	
	70	62	72	53	
	16	56	27	57	43	24	51	9	
	15	2	8	16	14	18	16	31	
	---------删除---78-----------------
	
	76
	75	72	
	70	62	43	53	
	16	56	27	57	31	24	51	9	
	15	2	8	16	14	18	16	
	=======================================================
	
	76
	75	72	
	70	62	43	53	
	16	56	27	57	31	24	51	9	
	15	2	8	16	14	18	16	
	---------删除---76-----------------
	
	75
	70	72	
	56	62	43	53	
	16	16	27	57	31	24	51	9	
	15	2	8	16	14	18	
	=======================================================
	
	75
	70	72	
	56	62	43	53	
	16	16	27	57	31	24	51	9	
	15	2	8	16	14	18	
	---------删除---75-----------------
	
	72
	70	53	
	56	62	43	51	
	16	16	27	57	31	24	18	9	
	15	2	8	16	14	
	=======================================================
	
	72
	70	53	
	56	62	43	51	
	16	16	27	57	31	24	18	9	
	15	2	8	16	14	
	---------删除---72-----------------
	
	70
	62	53	
	56	57	43	51	
	16	16	27	14	31	24	18	9	
	15	2	8	16	
	=======================================================
	
	70
	62	53	
	56	57	43	51	
	16	16	27	14	31	24	18	9	
	15	2	8	16	
	---------删除---70-----------------
	
	62
	57	53	
	56	27	43	51	
	16	16	16	14	31	24	18	9	
	15	2	8	
	=======================================================
	
	62
	57	53	
	56	27	43	51	
	16	16	16	14	31	24	18	9	
	15	2	8	
	---------删除---62-----------------
	
	57
	56	53	
	16	27	43	51	
	15	16	16	14	31	24	18	9	
	8	2	
	=======================================================
	
	57
	56	53	
	16	27	43	51	
	15	16	16	14	31	24	18	9	
	8	2	
	---------删除---57-----------------
	
	56
	27	53	
	16	16	43	51	
	15	16	2	14	31	24	18	9	
	8	
	=======================================================
	
	56
	27	53	
	16	16	43	51	
	15	16	2	14	31	24	18	9	
	8	
	---------删除---56-----------------
	
	53
	27	51	
	16	16	43	18	
	15	16	2	14	31	24	8	9	
	
	=======================================================
	
	53
	27	51	
	16	16	43	18	
	15	16	2	14	31	24	8	9	
	
	---------删除---53-----------------
	
	51
	27	43	
	16	16	31	18	
	15	16	2	14	9	24	8	
	=======================================================
	
	51
	27	43	
	16	16	31	18	
	15	16	2	14	9	24	8	
	---------删除---51-----------------
	
	43
	27	31	
	16	16	24	18	
	15	16	2	14	9	8	
	=======================================================
	
	43
	27	31	
	16	16	24	18	
	15	16	2	14	9	8	
	---------删除---43-----------------
	
	31
	27	24	
	16	16	9	18	
	15	16	2	14	8	
	=======================================================
	
	31
	27	24	
	16	16	9	18	
	15	16	2	14	8	
	---------删除---31-----------------
	
	27
	16	24	
	16	16	9	18	
	15	8	2	14	
	=======================================================
	
	27
	16	24	
	16	16	9	18	
	15	8	2	14	
	---------删除---27-----------------
	
	24
	16	18	
	16	16	9	14	
	15	8	2	
	=======================================================
	
	24
	16	18	
	16	16	9	14	
	15	8	2	
	---------删除---24-----------------
	
	18
	16	14	
	16	16	9	2	
	15	8	
	=======================================================
	
	18
	16	14	
	16	16	9	2	
	15	8	
	---------删除---18-----------------
	
	16
	16	14	
	15	16	9	2	
	8	
	=======================================================
	
	16
	16	14	
	15	16	9	2	
	8	
	---------删除---16-----------------
	
	16
	16	14	
	15	8	9	2	
	
	=======================================================
	
	16
	16	14	
	15	8	9	2	
	
	---------删除---16-----------------
	
	16
	15	14	
	2	8	9	
	=======================================================
	
	16
	15	14	
	2	8	9	
	---------删除---16-----------------
	
	15
	9	14	
	2	8	
	=======================================================
	
	15
	9	14	
	2	8	
	---------删除---15-----------------
	
	14
	9	8	
	2	
	=======================================================
	
	14
	9	8	
	2	
	---------删除---14-----------------
	
	9
	2	8	
	
	=======================================================
	
	9
	2	8	
	
	---------删除---9-----------------
	
	8
	2	
	=======================================================
	
	8
	2	
	---------删除---8-----------------
	
	2
	
	=======================================================
	
	2
	
	---------删除---2-----------------
	
	2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值