zoukankan      html  css  js  c++  java
  • 最大堆

          根据《算法导论》中介绍的算法实现。

          

    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    typedef struct priority_queue_tag {
        int heap_size;
        int *array;
    } priority_queue;
    
    int parent(int i)
    {
        return (i - 1) / 2;
    }
    
    int left_child(int i)
    {
        return i * 2 + 1;
    }
    
    int right_child(int i)
    {
        return i * 2 + 2;
    }
    
    void swap(int *a, int *b) 
    {
        if(a == NULL|| b == NULL || a==b)
            return;
        int t = *a; 
        *a = *b; 
        *b = t;
    }
    
    void heapify(priority_queue *pq, int i)
    {
        int left = left_child(i);
        int right = right_child(i);
        int largest = i;
        if (left < pq->heap_size
                && pq->array[largest] < pq->array[left]) {
            largest = left;
        }
        if (right < pq->heap_size
                && pq->array[largest] < pq->array[right]) {
            largest = right;
        }
        if (largest != i) {
            swap(&pq->array[i], &pq->array[largest]);
            heapify(pq, largest);
        }
    }
    
    void fix_up(priority_queue *pq, int i)
    {
        while (i > 0 && pq->array[parent(i)] < pq->array[i]) {
            swap(&pq->array[parent(i)], &pq->array[i]);
            i = parent(i);
        }
    }
    
    priority_queue *priority_queue_create(int n)
    {
        priority_queue *pq = (priority_queue *)malloc(sizeof(priority_queue));
        pq->array = (int *)malloc(sizeof(void *) * n);
        pq->heap_size = 0;
        return pq;
    }
    
    int priority_queue_top(priority_queue* pq)
    {
        return pq->array[0];
    }
    /*去掉并返回堆的第一个元素 */
    int priority_queue_extract_top(priority_queue* pq)
    {
        int i = pq->heap_size - 1;
        swap(&pq->array[0], &pq->array[i]);
        --pq->heap_size;
        heapify(pq, 0);
        return pq->array[i];
    }
    
    /*把元素key插入队列 */
    void priority_queue_insert(priority_queue *pq, int key)
    {
        int i = pq->heap_size;
        pq->array[i] = key;
        ++pq->heap_size;
        fix_up(pq, i);
    }
    
    bool priority_queue_is_empty(priority_queue *pq)
    {
        return pq->heap_size == 0;
    }
    
    void priority_queue_destroy(priority_queue *pq)
    {
        free(pq->array);
        free(pq);
    }
    
    int main()
    {
        priority_queue *pq = priority_queue_create(10);
        for (int i = 0; i < 10; i++) {
            priority_queue_insert(pq, i);
            }
        printf("最大堆结果:
    ");
        while (!priority_queue_is_empty(pq)) {
            int p = priority_queue_extract_top(pq);
            printf("%d ", p);
        }
        printf("
    ");
        priority_queue_destroy(pq);
        return 0;
    }
    

      

  • 相关阅读:
    微信小程序非域名测试环境,手机预览不正常,模拟器和真机调试正常解决办法
    vue 建项目及初步开发遇到的问题
    开发&测试
    架构&项目
    运维
    读书&源码
    ArrayList
    jdk集合知识点整理
    jdk集合关系图
    jvm知识点整理
  • 原文地址:https://www.cnblogs.com/moxiaopeng/p/4857612.html
Copyright © 2011-2022 走看看