#include <stdio.h>
#include <stdlib.h> #define HEAP_SIZE 100
#define HEAP_FULL_VALUE -100 #if 0
/*小顶堆存储结构*/
typedef struct small_heap
{
int data[HEAP_SIZE];
int num;
}SMALL_HEAP;
#endif /*
* name: heap_Swap
*
* purpose:
* swap two value of heap
*/
static void heap_Swap(int heap[],int index_src,int index_dst)
{
int tmp = heap[index_src]; heap[index_src] = heap[index_dst];
heap[index_dst] = tmp;
} /*
* name: heap_Up
*
* purpose:
* move up value of the index position to adjust heap struct
*/
static void heap_Up(int heap[],int index)
{
int parent = index / ; while(parent >= )
{
if(heap[index] < heap[parent])
{
heap_Swap(heap,index,parent);
index = parent;
}
else
{
break;
}
}
} /*
* name: heap_Down
*
* purpose:
* move down value of the index position to adjust heap struct
*/
static void heap_Down(int heap[],int index,int heap_data_num)
{
if(index * > heap_data_num)
{//leaf node can not move down
return;
} while(index * <= heap_data_num)
{
int child = index * ; // left child if(child > heap_data_num)
{
return;
} if(child * < heap_data_num)
{//the node have two child
//use multiply 2 to judge not use divide 2 to judge to pretend error
if(heap[child + ] < heap[child])
{
child += ; //right child is smaller update
} } if(heap[child] < heap[index])
{//the child samller than index swap value
heap_Swap(heap,index,child);
index = child;
}
else
{
break;
}
}
} /*
* name: heap_Insert
*
* purpose:
* insert a value into heap and ajust heap struct
*/
void heap_Insert(int heap[],int *heap_data_num,int value)
{
if(*heap_data_num == )
{
heap[] = HEAP_FULL_VALUE; //data 0 do not save in the heap
} (*heap_data_num)++; //update heap size
heap[*heap_data_num] = value; //add value to heap heap_Up(heap,*heap_data_num); //adjust heap struct
} /*
* name: heap_Delete
*
* purpost:
* delete a value from heap
*/
void heap_Delete(int heap[],int *heap_data_num,int value)
{
int index; for(index = ; index <= *heap_data_num; index++)
{
if(heap[index] == value)
{
break;
}
} if(index > *heap_data_num)
{//the value is not exist
return;
} heap[index] = heap[*heap_data_num]; //set the index value as final value (*heap_data_num)--;//the final value is not as the heap heap_Down(heap,index,*heap_data_num); //move down int parent = index / ;
if(parent > && heap[index] < heap[parent])
{//ajust to the special situation
heap_Up(heap,index);
} heap[*heap_data_num + ] = HEAP_FULL_VALUE; //delete final data
} void heap_Print(int heap[],int heap_data_num)
{
int i;
for(i = ; i <= heap_data_num; i++)
{
printf("%d ",heap[i]);
} printf("\n");
} int main()
{
int heap[HEAP_SIZE];
int i,heap_data_num = ; for(i = ; i < heap_data_num; i++)
{
heap[i] = HEAP_FULL_VALUE;
} #if 0
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,); heap_Print(heap,heap_data_num); heap_Delete(heap,&heap_data_num,);
heap_Print(heap,heap_data_num);
heap_Delete(heap,&heap_data_num,);
heap_Print(heap,heap_data_num); #endif #if 1
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,); heap_Print(heap,heap_data_num); heap_Delete(heap,&heap_data_num,); heap_Print(heap,heap_data_num);
#endif } 注:需要注意一点就是在进行节点是否有两个孩子的判断时,要用*2去判断,不能用除2判断,因为除2自动取整会导致少1的错误。

小顶堆---非递归C语言来一发的更多相关文章

  1. 小顶堆第二弹-----堆降序排序(C语言非递归)

    现在po一下C语言版本的,留作以后接口使用. 1 #include <stdio.h> #include <stdlib.h> #define HEAP_SIZE 100 #d ...

  2. 堆排序(大顶堆、小顶堆)----C语言

    堆排序 之前的随笔写了栈(顺序栈.链式栈).队列(循环队列.链式队列).链表.二叉树,这次随笔来写堆 1.什么是堆? 堆是一种非线性结构,(本篇随笔主要分析堆的数组实现)可以把堆看作一个数组,也可以被 ...

  3. 《排序算法》——堆排序(大顶堆,小顶堆,Java)

    十大算法之堆排序: 堆的定义例如以下: n个元素的序列{k0,k1,...,ki,-,k(n-1)}当且仅当满足下关系时,称之为堆. " ki<=k2i,ki<=k2i+1;或k ...

  4. 大顶堆与小顶堆应用---寻找前k小数

    vector<int> getLeastNumber(vector<int>& arr,int k){ vector<int> vec(k,); if(== ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. heap c++ 操作 大顶堆、小顶堆

    在C++中,虽然堆不像 vector, set 之类的有已经实现的数据结构,但是在 algorithm.h 中实现了一些相关的模板函数.下面是一些示例应用 http://www.cplusplus.c ...

  7. python 基于小顶堆实现随机抽样

    起因:之前用蓄水池抽样,算法精简,但直观性很差. 所以这次采用了简单的,为没一个行,赋值一个随机值,然后取 最大的K个作为,随机样本. 基本思路:为每一个行(record,记录,实体) 赋一个rand ...

  8. Python使用heapq实现小顶堆(TopK大)、大顶堆(BtmK小)

    Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) | 四号程序员 Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) 4 Replies 需1求:给出N长 ...

  9. CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)

    https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...

随机推荐

  1. [LeetCode] 496. Next Greater Element I 下一个较大的元素 I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  2. 【VS开发】【视频开发】利用ffmpeg+opencv实现画中画

    需求:把两路视频合成一路,即一个画面同时显示两路视频,其中一路缩小成小视频叠在大视频上面,和电视机的画中画效果类似. 思路:用h264编码的视频举例,文件中存储的es流是h264,经过解码成yuv,y ...

  3. os.environ模块

    os.environ是用来获取当前操作系统的一些基本信息的模块 import osos.environ.setdefault("DJANGO_SETTINGS_MODULE", & ...

  4. python zip函数(11)

    一.zip函数描述和使用 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,返回的结果可以直接强转为list列表,这样做的好处是节约了不少的 ...

  5. Django组件之auth

    一.什么是Auth模块 Auth模块是Django自带的用户认证模块,默认使用 auth_user 表来存储用户数据. 二.使用方法 1.创建超级用户 python3 manage.py create ...

  6. Windows下Charles抓包https协议配置

    最近设置https协议对手机app抓包遇到一些问题,现在在这里记录下,以防以后遇到问题没有记录 1.从官网下载Charles的安装包 https://www.charlesproxy.com/down ...

  7. Spring Cloud--Hystrix服务熔断(线程隔离/服务降级)代码实现

    一旦服务阻塞就进行服务降级或线程隔离.要不然就会导致大面积服务的瘫痪,Hystrix就是干这个的,一出现不健康的服务就进行熔断,不阻塞后面线程的执行. 引入依赖: 加注解: 这三个注解可以用一个注解搞 ...

  8. Jenkins + GitLab + SpringBoot 实现持续集成脚本

    Linux脚本 #!/bin/bash jar_name=hq-api.jar cd /usr/local/app/hq-api echo "Stopping SpringBoot Appl ...

  9. 3.NioEventLoop的启动和执行

    NioEventLoop启动和执行 NioEventLoop启动 在服务端启动的代码中,我们看到netty在注册和绑定时,判断了当前线程是否是NioEventLoop线程.如果不是, 则将这些操作包装 ...

  10. 1.1 文档PUT内部原理

    文档更新原理:       PUT 一条数据的时候,如果是全量替换,ES并不会覆盖原来的文档,而是新创建一个文档,并将version+1,原文档标记为deleted,不会立刻物理删除.ES会在集群的d ...