大顶堆插入删除(C)

#include<stdio.h>
#include<stdlib.h>

typedef int element_type;

typedef struct max_heap_struct {
    element_type *ele_arr;
    int size;
    int capacity;
} max_heap_t;

void heap_item_swap(max_heap_t *heap, int idx1, int idx2)
{
    element_type tmp;
    tmp = heap->ele_arr[idx1];
    heap->ele_arr[idx1] = heap->ele_arr[idx2];
    heap->ele_arr[idx2] = tmp;
}

void heap_print(max_heap_t *heap) {
    for (int i = 0; i < heap->size; i++) {
        printf("%d ", heap->ele_arr[i]);
    }
    printf("\n");
}

max_heap_t* create_heap(int max_size)
{
    max_heap_t *heap = (max_heap_t *)malloc(sizeof(max_heap_t));
    heap->ele_arr = (int *)malloc(sizeof(element_type) * (max_size));
    heap->capacity = max_size;
    heap->size = 0;

    return heap;
}

int is_heap_full(max_heap_t *heap)
{
    return heap->size == heap->capacity;
}

int is_empty(max_heap_t *heap)
{
    return heap->size == 0;
}

int heap_insert(max_heap_t *heap, element_type item)
{
    if (is_heap_full(heap)) {
        printf("max heap is full.\n");
        return 0;
    }
    printf("heap insert: %d.\n", item);

    int target_idx = heap->size;
    heap->ele_arr[target_idx] = item;
    while (target_idx > 0) {
        int parent_idx = (target_idx - 1) / 2;

        if (heap->ele_arr[parent_idx] > heap->ele_arr[target_idx]) {
            break;
        } else {
            heap_item_swap(heap, parent_idx, target_idx);
            target_idx = parent_idx;
        }
    }

    heap->size++;
}

void heap_delete_max(max_heap_t *heap)
{
    if (is_empty(heap)) {
        printf("max heap is empty.\n");
    }

    heap->ele_arr[0] = heap->ele_arr[heap->size - 1];
    heap->size--;

    int parent_id = 0;
    int left_child_id =  parent_id * 2 + 1;
    int right_child_id = left_child_id + 1;

    while (left_child_id < heap->size) {
        int max_child = left_child_id;
        if (right_child_id < heap->size) {
            if (heap->ele_arr[right_child_id] > heap->ele_arr[left_child_id]) {
                max_child = right_child_id;
            } 
        }

        if (heap->ele_arr[parent_id] > heap->ele_arr[max_child]) {
            break;
        } else {
            heap_item_swap(heap, parent_id, max_child);
            parent_id = max_child;
            left_child_id = parent_id * 2 + 1;
            right_child_id = left_child_id + 1;
        }
    }
}

void heap_destroy(max_heap_t *heap) {
    free(heap->ele_arr);
    free(heap);
}

int main()
{
    max_heap_t *heap = create_heap(10);

    heap_insert(heap, 10);
    heap_insert(heap, 50);
    heap_insert(heap, 30);
    heap_insert(heap, 20);
    heap_insert(heap, 40);
    heap_print(heap);

    heap_delete_max(heap);
    heap_print(heap);

    heap_destroy(heap);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值