小顶堆---非递归C语言来一发
#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语言来一发的更多相关文章
- 小顶堆第二弹-----堆降序排序(C语言非递归)
现在po一下C语言版本的,留作以后接口使用. 1 #include <stdio.h> #include <stdlib.h> #define HEAP_SIZE 100 #d ...
- 堆排序(大顶堆、小顶堆)----C语言
堆排序 之前的随笔写了栈(顺序栈.链式栈).队列(循环队列.链式队列).链表.二叉树,这次随笔来写堆 1.什么是堆? 堆是一种非线性结构,(本篇随笔主要分析堆的数组实现)可以把堆看作一个数组,也可以被 ...
- 《排序算法》——堆排序(大顶堆,小顶堆,Java)
十大算法之堆排序: 堆的定义例如以下: n个元素的序列{k0,k1,...,ki,-,k(n-1)}当且仅当满足下关系时,称之为堆. " ki<=k2i,ki<=k2i+1;或k ...
- 大顶堆与小顶堆应用---寻找前k小数
vector<int> getLeastNumber(vector<int>& arr,int k){ vector<int> vec(k,); if(== ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- heap c++ 操作 大顶堆、小顶堆
在C++中,虽然堆不像 vector, set 之类的有已经实现的数据结构,但是在 algorithm.h 中实现了一些相关的模板函数.下面是一些示例应用 http://www.cplusplus.c ...
- python 基于小顶堆实现随机抽样
起因:之前用蓄水池抽样,算法精简,但直观性很差. 所以这次采用了简单的,为没一个行,赋值一个随机值,然后取 最大的K个作为,随机样本. 基本思路:为每一个行(record,记录,实体) 赋一个rand ...
- Python使用heapq实现小顶堆(TopK大)、大顶堆(BtmK小)
Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) | 四号程序员 Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) 4 Replies 需1求:给出N长 ...
- CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)
https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...
随机推荐
- [ Docker ] 基础概念
目录- 什么是容器- 虚拟化和容器技术- docker 的基本概念 1. 什么是容器 容器英文:Container,容器是一种基础工具:泛指任何可以用于容纳其他物品的工具,可以部分或者完全封闭,被用于 ...
- Oracle通过命令导入数据存储文件
imp ztdev/ztdev FROMUSER=zt_base TOUSER=ztdev file=/home/oracle/zt_base_1023_sc_kk_new.dmp log=zt_ba ...
- iframe跨端口报错 Blocked a frame with origin from accessing a cross-origin frame
前言 在不同的端口号,甚至是不同的ip进行iframe嵌套的时候,在父页面调用子页面的方法的时候,报错 SecurityError: Blocked a frame with origin fr ...
- [转帖]Socat 入门教程
https://www.hi-linux.com/posts/61543.html 现在安装k8s 必须带 socat 今天看一下socat 到底是啥东西呢. Socat 是 Linux 下的一个多功 ...
- ubuntu sh脚本激活conda 虚拟环境
第一步:初始化coda 命令:sudo gedit ~/.bashrc 第二部:复制其中这样一段代码 # >>> conda initialize >>> # !! ...
- 遇到了NameError: name ‘name’ is not defined 这样的错误。
改正:__name__ == "__main__" name的左右两边各有两条下划线,不是左右两边各有一条
- 计数器的Verilog写法
计数器是非常基本的使用,没有计数器就无法处理时序.我在学习时发现市面上有几种不同的计数器写法,非常有趣,在此记录下来: 一.时序逻辑和组合逻辑彻底分开(by锆石科技FPGA教程) 1.代码 //=== ...
- SAS学习笔记60 统计SAS实例之T检验
单样本 H0:服从正态分布 P=0.0988>0.05不拒绝H0,服从正态分布 H0:等于140t=-2.14,P=0.0397 P<0.05,拒绝H0,差异有统计学意义 均值x=130. ...
- quartz2.3.0(七)调度器中断任务执行,手动处理任务中断事件
job任务类 package org.quartz.examples.example7; import java.util.Date; import org.slf4j.Logger; import ...
- vim的多文件编辑和多窗口功能
有的时候我们可能会需要打开多个文件同时进行编辑,例如把一个文件的内容复制到另一个文件中时: 多文件编辑 :n :编辑下一个文件 :N : 编辑上一个文件 :files :列出目前这个vim打开的所有文 ...