栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据结构与算法 — 二叉堆及优先队列的代码实现【基于Golang语言实现】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构与算法 — 二叉堆及优先队列的代码实现【基于Golang语言实现】

二叉堆及优先队列

二叉堆

1、最大堆2、最小堆3、二叉堆的自我调整代码实现 优先队列

特点

代码实现【略】

二叉堆

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

1、最大堆

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

2、最小堆

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

3、二叉堆的自我调整
    插入节点【节点上浮】, 时间复杂度O(logn)删除节点【节点下沉】, 时间复杂度O(logn)构建二叉树, 时间复杂度O(n)
代码实现

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

Java语言:

import java.util.Arrays;


public class BinaryHeap {

    
    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;
        }
    }

    
    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]

}

优先队列 特点

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

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

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

代码实现【略】

看漫画算法 P101

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770880.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号