优先级队列(堆)


1、二叉树的顺序存储

1.1 存储方式

使用数组保存二叉树结构,方式即将二叉树用层序遍历方式放入数组中。一般只适合表示完全二叉树,因为非完全二叉树会有空间的浪费。这种方式的主要用法就是堆的表示。

1.2 下标关系

  1. 已知双亲(parent)的下标,则:
    左孩子(left)下标 = 2 * parent + 1;
    右孩子(right)下标 = 2 *parent + 2;

  2. 已知孩子(不区分左右)(child)下标,则:
    双亲(parent)下标 = (child - 1) / 2;

2、 堆(heap)

2.1 概念

  1. 堆逻辑上是一棵完全二叉树
  2. 堆物理上是保存在数组中
  3. 满足任意结点的值都大于其子树中结点的值,叫做大堆,或者大根堆,或者最大堆
  4. 反之,则是小堆,或者小根堆,或者最小堆
  5. 堆的基本作用是,快速找集合中的最值

2.2 操作

代码如下:

//向下调整
    //size:array中哪部分内容是堆
    //index:从哪个位置开始进行向下调整
    public static void shiftDown(int[] array, int size, int index) {
        int parent = index;
        int child = 2 * parent + 1;
        //这个条件的含义是看看parent有没有子节点
        while (child < size) {
            // 把左右子树中较大的节点找到
            if (child+1<size&&array[child+1]>child){
                child = child+1;
            }
            // 上述条件结束后,child肯定对应左右子树中比较大的元素。
            // 在拿当前的这个child和parent位置的元素比较一下
            if (array[child]>array[parent]){
                int tmp = array[child];
                array[child] = array[parent];
                array[parent] =tmp;
            }else{
                break;
            }
            parent = child;
            child = 2*parent+1;
        }
    }

    //建堆
    public static void createHeap(int[] array, int size) {
        //基于向下调整建堆
        //可以向下调整,也可以向上调整
        //向下调整,就需要从后往前遍历数组
        for (int i = (size - 1 - 1) / 2; i >= 0; i--) {
            shiftDown(array, size, i);
        }
    }

    //入队列
    public static void shiftUp(int[] array, int index) {
        while (index > 0) {
            int parent = (index - 1) / 2;
            if (array[parent] >= array[index]) {
                break;
            }

            int t = array[parent];
            array[parent] = array[index];
            array[index] = t;

            index = parent;
        }
    }

    //出队列
    public class MyPriorityQueue {
        // 演示作用,不再考虑扩容部分的代码
        private int[] array = new int[100];
        private int size = 0;

        public void offer(int e) {
            array[size++] = e;
            shiftUp(array, size - 1);
        }

        public int poll() {
            int oldValue = array[0];
            array[0] = array[--size];
            shiftDown(array, size, 0);
            return oldValue;
        }

        public int peek() {
            return array[0];
        }
    }

3、 优先级队列


3.1 概念

  • 在很多应用中,我们通常需要按照优先级情况对待处理对象进行处理,比如首先处理优先级最高的对象,然后处理次高的对象。最简单的一个例子就是,在手机上玩游戏的时候,如果有来电,那么系统应该优先处理打进来的电话。
  • 在这种情况下,我们的数据结构应该提供两个最基本的操作,一个是返回最高优先级对象,一个是添加新的对象。这种数据结构就是优先级队列(Priority Queue)

3.2 操作

public static void shiftUp(int[] array, int index) {
    while (index > 0) {
        int parent = (index - 1) / 2;
        if (array[parent] >= array[index]) {
            break;
       }
        
        int t = array[parent];
        array[parent] = array[index];
        array[index] = t;
        
        index = parent;
   }
}

public class MyPriorityQueue {
    // 演示作用,不再考虑扩容部分的代码
    private int[] array = new int[100];
    private int size = 0;
    
    public void offer(int e) {
        array[size++] = e;
        shiftUp(array, size - 1);
   }
    
    public int poll() {
        int oldValue = array[0];
        array[0] = array[--size];
        shiftDown(array, size, 0);
        return oldValue;
   }
    
    public int peek() {
        return array[0];
   }
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值