数据结构与算法 — 二叉堆及优先队列的代码实现(Golang)

1 二叉堆

二叉堆有两种类型,最大堆和最小堆。

1.1 最大堆

最大堆的任何一个父节点的值,都大于等于它左、右孩子节点的值。

1.2 最小堆

最小堆的任何一个父节点的值,都小于等于它左、右孩子节点的值。

1.3 二叉堆的自我调整

  1. 插入节点【节点上浮】, 时间复杂度O(logn)
  2. 删除节点【节点下沉】, 时间复杂度O(logn)
  3. 构建二叉树, 时间复杂度O(n)

1.4 代码实现

二叉堆的存储方式是顺序存储,即节点数据都存储在数组中。

Java语言


import java.util.Arrays;

/**
 * 最小堆的上浮和下沉
 */
public class BinaryHeap {

    /**
     * 上浮调整
     *
     * @param array
     */
    public static void upAdjust(int[] array) {
        int childIndex = array.length - 1; //最后一个位置得索引
        int parentIndex = (childIndex - 1) / 2; //思考一下为什么不论最后一个元素是左孩子还是右孩子,其父节点得索引计算都是这个公式

        while (childIndex > 0 && array[childIndex] < array[parentIndex]) { //上浮的元素不能小于等于0,因为等于0时就已经是根节点了,不能再上浮了,小于0时,就数组下标越界了,所以循环条件是(childIndex >0 && array[childIndex] < array[(childIndex-1)/2]
            //交换位置
            int temp = array[childIndex];
            array[childIndex] = array[parentIndex];
            array[parentIndex] = temp;

            childIndex = parentIndex;
            parentIndex = (childIndex - 1) / 2;
        }
    }

    /**
     * 下沉调整
     *
     * @param array       待调整的堆
     * @param parentIndex 要下沉的父节点
     */
    public static void downAdjust(int[] array, int parentIndex) {
        int length = array.length;
        int childIndex = 2 * parentIndex + 1; //左孩子的索引
        while (childIndex < length) { //边界判断
            if (childIndex + 1 < length && array[childIndex + 1] < array[childIndex]) {//当有右孩子时,判断左右孩子的大小
                childIndex = childIndex + 1;//右孩子对应的索引
//                childIndex++; //等同于childIndex + 1;
            }
            if (array[parentIndex] < array[childIndex]) { //当父节点比子节点小的时候,退出循环
                break;
            }
            int temp = array[parentIndex];
            array[parentIndex] = array[childIndex];
            array[childIndex] = temp;
            //交换完毕

            parentIndex = childIndex;//将交换过的子节点索引重新给父节点
            childIndex = 2 * parentIndex + 1;//重新计算新的左孩子索引

        }
    }

    public static void buildHeap(int[] array) {
        //从最后一个非叶子节点开始,依次做下沉操作
        for (int i = (array.length - 2) / 2; i >= 0; i--) {
            downAdjust(array, i);
        }
    }

    public static void main(String[] args) {

        int[] array = new int[]{1, 3, 2, 6, 5, 7, 8, 9, 10, 0};
        upAdjust(array);
        System.out.println(Arrays.toString(array)); //结果:[0, 1, 2, 6, 3, 7, 8, 9, 10, 5]

        array = new int[]{7, 1, 3, 10, 5, 2, 8, 9, 6};
//        downAdjust(array,0);
        buildHeap(array);
        System.out.println(Arrays.toString(array)); //结果:[1, 5, 2, 6, 7, 3, 8, 9, 10]

    }

}

Golang语言:

package main

import "fmt"

/**
 * 最小堆的上浮
 */
func UpAdjust(array []int) {
	childIndex := len(array) - 1
	parentIndex := (childIndex - 1) / 2
	for childIndex > 0 && array[childIndex] < array[parentIndex] {
		temp := array[childIndex]
		array[childIndex] = array[parentIndex]
		array[parentIndex] = temp

		childIndex = parentIndex
		parentIndex = (childIndex - 1) / 2
	}
}

/**
 * 最小堆的下沉
 */
func DownAdjust(array []int, parentIndex int) {
	length := len(array)
	childIndex := 2*parentIndex + 1

	for childIndex < length {
		if childIndex+1 < length && array[childIndex+1] < array[childIndex] {
			childIndex++
		}
		if array[parentIndex] < array[childIndex] {
			break
		}
		temp := array[parentIndex]
		array[parentIndex] = array[childIndex]
		array[childIndex] = temp

		parentIndex = childIndex
		childIndex = 2*parentIndex + 1
	}
}

func BuildHeap(array []int) {
	for i := (len(array) - 2) / 2; i >= 0; i-- {
		DownAdjust(array, i)
	}
}

func main() {
	array := []int{1, 3, 2, 6, 5, 7, 8, 9, 10, 0}
	UpAdjust(array)
	fmt.Println(array) //结果:[0, 1, 2, 6, 3, 7, 8, 9, 10, 5]

	array = []int{7, 1, 3, 10, 5, 2, 8, 9, 6}
	//DownAdjust(array, 0)
	BuildHeap(array)
	fmt.Println(array) //结果:[1, 5, 2, 6, 7, 3, 8, 9, 10]

}

1.5 二叉堆时间复杂度

参考1:二叉堆实现及时间复杂度分析

堆排序:nlog(n)
堆插入元素:log(n)
堆调整:log(n)
堆重建:O(n)

2 优先队列

2.1 优先队列特点

优先队列不在遵循队列先入先出的原则,而是分为两种情况:

  • 最大优先队列,无论入队顺序如何,都是当前最大的元素优先出队。
  • 最小优先队列,无论入队顺序如何,都是当前最小的元素优先出队。

优先队列的实现基础是二叉堆,因为二叉堆有最大堆和最小堆。
通过最大堆可实现最大优先队列,每一次入队就是堆的插入操作,每一次出队就是删除堆顶节点的操作。
通过最小堆可实现最小优先队列,每一次入队就是堆的插入操作,每一次出队就是删除堆顶节点的操作。

2.2 代码实现【略】

看漫画算法 P101

2.3 优先队列的时间复杂度

参考1:优先队列时间复杂度

出队和入队的时间复杂度是 O(logn)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值