堆的基本操作

首先堆是一棵完全二叉树,其次堆分大顶堆和小顶堆。
大顶堆的堆顶,也就是根节点,其值大于左孩子的值且大于右孩子的值,左右孩子的大小不做区分,其子树满足这一性质。
小顶堆的堆顶,也就是根节点,其值小于左孩子的值且小于右孩子的值,左右孩子的大小不做区分,其子树满足这一性质。

堆基本操作的完整代码

然后我们来看看如果创建一个堆:在创建堆的时候,我们要给堆所需的元素,否则创建的堆为空堆,空堆的实际意义不大,所以此处讨论的是创建有元素的堆,此堆的底层空间为一个数组,满足parent = parent * 2 + 1为子节点的性质。

堆的核心思想在于调整,正因为向上(向下)调整,使得堆在排序上的时间复杂度可以达到O(log2N),是一种相当高效的排序算法。

void Swap(int* parent, int* child)
{
    int tmp = *parent;
    *parent = *child;
    *child = tmp;
}

void _Adjustup(Heap* hp, int child)
{
    int parent = (child - 1) >> 1;
    while (child)
    {
        if ((child + 1) < hp->_size && hp->comp(hp->_data[child + 1] , hp->_data[child]))
            ++child;
        if (hp->comp(hp->_data[child], hp->_data[parent]))
        {
            Swap(&hp->_data[parent], &hp->_data[child]);
            child = parent;
            parent = (child - 1) >> 1;
        }
        else
            break;
    }
}

void _Adjustdown(Heap* hp, int parent)
{
    assert(hp);
    int child = (parent << 1) + 1;
    while (child < hp->_size)
    {
        if (child + 1 < hp->_size && hp->comp(hp->_data[child + 1], hp->_data[child]))
        {
            ++child;
        }
        if (hp->comp(hp->_data[child], hp->_data[parent]))
        {
            Swap(&hp->_data[parent], &hp->_data[child]);
            parent = child;
            child = (parent << 1) + 1;
        }
        else
        {
            break;
        }
    }
}

//创建堆
void CreteHeap(Heap* hp, int arr[], int size)
{
    assert(hp);
    int i = 0;
    int child = 0;
    int parent = 0;
    hp->_data = (DataType*)malloc(sizeof(DataType)* size);
    if (NULL == hp->_data)
    {
        assert(0);
        return;
    }
    hp->_capacity = size;
    hp->_size = size;
    for (; i < size; ++i)
    {   
        hp->_data[i] = arr[i];
    }
    parent = size - 1;
    for (; parent >= 0; --parent)
    {
        _Adjustdown(hp, parent);
    }   
}

下面给出了堆的基本操作以及堆的几个简单的应用案例:

void InsertHeap(Heap* hp, DataType data)
{
    int parent = 0;
    assert(hp);
    if (hp->_size == hp->_capacity)
    {
        DataType* NewBase = (DataType*)realloc(hp->_data, hp->_capacity * 2 * sizeof(DataType));
        if (NULL == NewBase)
        {
            assert(0);
            return;
        }
        hp->_data = NewBase;
        hp->_capacity = hp->_capacity * 2;
    }
    hp->_data[hp->_size] = data;
    ++hp->_size;
    for (; parent < hp->_size; ++parent)
    {
        _Adjustup(hp, parent);
    }
}

//返回堆顶元素
DataType HeapTop(Heap* hp)
{
    assert(hp);
    return hp->_data[0];
}

// 检测一个堆是否为空堆
int EmptyHeap(Heap* hp)
{
    assert(hp);
    if (0 == hp->_size)
        return 1;
    return 0;
}

// 获取堆中元素的个数
int SizeHeap(Heap* hp)
{
    assert(hp);
    return hp->_size;
}

// 删除堆顶元素
void DeleteHeap(Heap* hp)
{
    assert(hp);
    int parent;
    Swap(&hp->_data[0], &hp->_data[hp->_size - 1]);
    --hp->_size;
    parent = hp->_size - 1;
    for (; parent >= 0; --parent)
    {
        _Adjustdown(hp, parent);
    }
}

// 销毁堆
void DestroyHeap(Heap* hp)
{
    assert(hp);
    free(hp->_data);
    hp->_data = NULL;
    hp->_size = 0;
    hp->_capacity = 0;
}

//堆排序
void HeapSort(Heap* hp)
{
    assert(hp);
    while (!EmptyHeap(hp))
    {
        printf("%d ", HeapTop(hp));
        DeleteHeap(hp);
    }
}

void TopK(Heap* hp,int k)
{
    assert(hp);
    if (SizeHeap(hp) > k)
    {
        while (k--)
        {
            printf("%d ", HeapTop(hp));
            DeleteHeap(hp);
        }
    }
    else
    {
        k = SizeHeap(hp);
        while (k--)
        {
            printf("%d ", HeapTop(hp));
            DeleteHeap(hp);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
最大堆排序基本操作包括以下几个步骤: 1. 构建最大:将待排序的数组构造成一个最大。最大是一种满足父节点大于等于子节点的完全二叉树。构建最大的方法是从最后一个非叶子节点开始,依次向上调整每个节点,使其满足最大的性质。 2. 交换顶元素和最后一个元素:将最大顶元素与最后一个元素交换位置,即将最大的元素放到数组的末尾。 3. 调整:将剩下的元素重新调整为最大。从顶开始,比较左右子节点的值,将较大的子节点与父节点交换位置,然后再对交换后的子节点进行调整,直到整个重新满足最大的性质。 4. 重复步骤2和步骤3,直到所有元素都被排序。 下面是一个用C++实现基于最大堆排序的示例: ```cpp #include <iostream> using namespace std; // 调整 void adjustHeap(int arr[], int n, int i) { int largest = i; // 初始化最大值为根节点 int left = 2 * i + 1; // 左子节点 int right = 2 * i + 2; // 右子节点 // 如果左子节点大于根节点,更新最大值 if (left < n && arr[left] > arr[largest]) { largest = left; } // 如果右子节点大于根节点,更新最大值 if (right < n && arr[right] > arr[largest]) { largest = right; } // 如果最大值不是根节点,交换根节点和最大值 if (largest != i) { swap(arr[i], arr[largest]); // 递归调整交换后的子节点 adjustHeap(arr, n, largest); } } // 堆排序 void heapSort(int arr[], int n) { // 构建最大 for (int i = n / 2 - 1; i >= 0; i--) { adjustHeap(arr, n, i); } // 交换顶元素和最后一个元素,并调整 for (int i = n - 1; i > 0; i--) { swap(arr[0], arr[i]); adjustHeap(arr, i, 0); } } int main() { int arr[] = {91, 84, 72, 63, 55, 46, 37, 29, 20, 11}; int n = sizeof(arr) / sizeof(arr[0]); heapSort(arr, n); cout << "Sorted array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值