Java 与数据结构(8):堆

一、堆

堆是一种特殊的树形数据结构,它满足以下两个条件:

  1. 堆是一棵完全二叉树,即除了最后一层外,其他层都是满的,并且最后一层的节点都靠左排列。

  2. 堆中每个节点的值都必须大于等于(或小于等于)其子节点的值,这种关系被称为堆序性。

根据堆序性,堆可以分为两种类型:

  1. 最大堆:每个节点的值都大于等于其子节点的值。

  2. 最小堆:每个节点的值都小于等于其子节点的值。

堆的常见操作包括插入元素、删除元素和查找最大/最小元素等。其中,插入元素和删除元素的时间复杂度为O(log n),查找最大/最小元素的时间复杂度为O(1)。堆常用于实现优先队列、排序算法等。

二、Java 示例

下面是一个Java实现最大堆的示例:

import java.util.Arrays;

public class MaxHeap {
    private int[] heap;
    private int size;

    public MaxHeap(int capacity) {
        heap = new int[capacity];
        size = 0;
    }

    public void insert(int value) {
        if (size == heap.length) {
            throw new IllegalStateException("Heap is full");
        }

        heap[size] = value;
        size++;
        bubbleUp();
    }

    public int deleteMax() {
        if (size == 0) {
            throw new IllegalStateException("Heap is empty");
        }

        int max = heap[0];
        heap[0] = heap[size - 1];
        size--;
        bubbleDown();
        return max;
    }

    private void bubbleUp() {
        int index = size - 1;
        while (hasParent(index) && parent(index) < heap[index]) {
            swap(index, parentIndex(index));
            index = parentIndex(index);
        }
    }

    private void bubbleDown() {
        int index = 0;
        while (hasLeftChild(index)) {
            int largerChildIndex = leftChildIndex(index);
            if (hasRightChild(index) && rightChild(index) > leftChild(index)) {
                largerChildIndex = rightChildIndex(index);
            }

            if (heap[index] < heap[largerChildIndex]) {
                swap(index, largerChildIndex);
            } else {
                break;
            }

            index = largerChildIndex;
        }
    }

    private boolean hasParent(int index) {
        return parentIndex(index) >= 0;
    }

    private boolean hasLeftChild(int index) {
        return leftChildIndex(index) < size;
    }

    private boolean hasRightChild(int index) {
        return rightChildIndex(index) < size;
    }

    private int parent(int index) {
        return heap[parentIndex(index)];
    }

    private int leftChild(int index) {
        return heap[leftChildIndex(index)];
    }

    private int rightChild(int index) {
        return heap[rightChildIndex(index)];
    }

    private int parentIndex(int index) {
        return (index - 1) / 2;
    }

    private int leftChildIndex(int index) {
        return 2 * index + 1;
    }

    private int rightChildIndex(int index) {
        return 2 * index + 2;
    }

    private void swap(int index1, int index2) {
        int temp = heap[index1];
        heap[index1] = heap[index2];
        heap[index2] = temp;
    }

    @Override
    public String toString() {
        return Arrays.toString(heap);
    }
}

使用示例:

MaxHeap heap = new MaxHeap(5);
heap.insert(3);
heap.insert(10);
heap.insert(5);
heap.insert(2);
heap.insert(7);
System.out.println(heap); // [10, 7, 5, 3, 2]
System.out.println(heap.deleteMax()); // 10
System.out.println(heap); // [7, 3, 5, 2]

三、堆在spring 中的作用

在Spring框架中,堆的作用主要体现在两个方面:

  1. Spring IoC容器

Spring IoC容器使用堆来管理应用程序中的所有bean。在应用程序启动时,Spring IoC容器将所有bean实例化并存储在堆中。当应用程序需要使用某个bean时,Spring IoC容器从堆中获取该bean并返回给应用程序。通过使用堆,Spring IoC容器能够轻松地管理大量的bean实例,并提供高效的bean查找和访问。

  1. Spring AOP框架

Spring AOP框架使用堆来管理切面(Aspect)和通知(Advice)。在应用程序启动时,Spring AOP框架将所有切面和通知实例化并存储在堆中。当应用程序需要使用某个切面或通知时,Spring AOP框架从堆中获取该实例并将其应用于目标对象。通过使用堆,Spring AOP框架能够轻松地管理大量的切面和通知实例,并提供高效的切面和通知查找和访问。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暗星涌动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值