C语言实现常用数据结构——堆
- #include<stdio.h>
- #include<stdlib.h>
- #define CAPACITY 20
- /*堆有两个性质:
- * 1.结构性:堆必须是一颗完全二叉树
- * 2.堆序性:堆的父节点要么都大于子节点,要么小于子节点,前者叫大顶堆,后者叫小顶堆;
- * 由此,堆可以用一个数组来表示,并有如下性质:
- * 1.对于任意i位置的元素,他的左子节点在2i位置,右子节点在2i+1位置;
- * 2.他的父节点(假如有)在i/2位置*/
- /*创建一个小顶堆,size代表的是实际元素的个数*/
- typedef struct MinHeap {
- int size;
- int data[CAPACITY];
- } heap;
- void init( heap *h );
- void insert(heap *h,int x);
- void travel(heap *h);
- /*数组0位置要空着*/
- void init( heap *h ) {
- h->size=;
- }
- void insert(heap *h,int x) {
- if(h->size == CAPACITY) {
- printf("heap is full!");
- return;
- }
- int i;
- h->size++;
- for(i=h->size; i>=; i/=) {
- if(x < h->data[i/]) {
- h->data[i]=h->data[i/];
- } else {
- break;
- }
- }
- h->data[i]=x;
- }
- /*删除最小元素,在小顶堆即意味着删除根节点
- * 1.首先将根元素保存,等待最后return;
- * 2.将最后一个元素赋值给根元素,并将这个值赋给缓冲区,这样保证了堆的结构性;
- * 3.从根节点开始遍历,比较父节点和两个子节点的大小,如果缓冲区值大于较小的子节点,则将小节点的值赋给父节点
- * 4.直到缓冲区值小于游标的两个子节点,此时将缓冲区值赋给游标所在位置*/
- int deleteMin(heap *h) {
- int child;
- int result=h->data[];
- h->data[]=h->data[h->size];
- h->size--;
- int i=;
- int temp=h->data[];
- for(i=; *i <= h->size; i=child) {
- child=*i;
- if(child !=h->size && h->data[child] > h->data[child+] ) {/*如果左子节点非最后元素且>右子节点,则右子节点最小*/
- child++;
- }
- if(temp > h->data[child]) {/*如果temp大于当前元素的最小子节点,则将最小子节点赋值给父节点,否则跳出*/
- h->data[i]=h->data[child];
- } else {
- break;
- }
- }
- h->data[i]=temp;/*将缓冲区值赋给当前游标*/
- return result;
- }
- /*遍历堆数组:越过空白位置0,从1开始*/
- void travel(heap *h) {
- int i;
- for(i=; i<=h->size; i++) {
- printf("%d ",h->data[i]);
- }
- printf("\n");
- }
- /*堆排序*/
- void heap_sort(int a[],int n) {
- int i;
- heap *h=(heap*)malloc(sizeof(heap));/*给堆指针分配空间*/
- init(h);/*初始化堆*/
- for(i=; i<n; i++) {/*将数组的元素依次插入堆*/
- insert(h,a[i]);
- }
- for(i=; i<n; i++) {
- a[i]=deleteMin(h);
- }
- }
- /*遍历数组*/
- void travel_array(int a[],int n) {
- int i;
- for(i=; i<n; i++) {
- printf("%d ",a[i]);
- }
- printf("\n");
- }
- main() {
- int a[]= {,,,,,,,,,,,,,,,};
- int n=sizeof(a)/sizeof(int);
- travel_array(a,n);
- heap_sort(a,n);
- travel_array(a,n);
- }
C语言实现常用数据结构——堆的更多相关文章
- C语言实现常用数据结构——链表
#include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; ...
- C语言实现常用数据结构——图
#include<stdio.h> #include<stdlib.h> #define SIZE 20 #define LENGTH(a) (sizeof(a)/sizeof ...
- C语言实现常用数据结构——二叉树
#include<stdio.h> #include<stdlib.h> #define SIZE 10 typedef struct Tree { int data; str ...
- C语言实现常用数据结构——队列
#include<stdio.h> #include<stdlib.h> #define MAX_SIZE 10 /* 用一个动态数组来实现队列 */ typedef stru ...
- C语言实现常用数据结构——栈
#include<stdio.h> #include<stdlib.h> //用链表实现栈 typedef struct Node { int data; struct Nod ...
- C++常用数据结构的实现
常用数据结构与算法的实现.整理与总结 我将我所有数据结构的实现放在了github中:Data-Structures-Implemented-By-Me 常用数据结构与算法的实现.整理与总结 KMP字符 ...
- 1. C语言中的数据结构.md
C语言内建数据结构类型 整型 整型数据是最基本的数据类型,不过从整形出发衍生出好几种integer-like数据结构,譬如字符型,短整型,整型,长整型.他们都是最基本的方式来组织的数据结构,一般是几位 ...
- 常用数据结构及复杂度 array、LinkedList、List、Stack、Queue、Dictionary、SortedDictionary、HashSet、SortedSet
原文地址:http://www.cnblogs.com/gaochundong/p/data_structures_and_asymptotic_analysis.html 常用数据结构的时间复杂度 ...
- php常用数据结构
# 常用数据结构--------------------------------------------------------------------------------## 树(Tree)- ...
随机推荐
- CAP和最终一致性
查阅资料整理了最终一致性.CAP 相关的内容.由于图省事儿,没有做文字的整理记载,只有 slides 和一些查阅过的链接,大家将就着看.欢迎指正. slides: slides 链接:请戳这里 背景 ...
- automapper如何全局配置map条件过滤null值空值对所有映射起效
原文 automapper如何全局配置map条件过滤null值空值对所有映射起效 我们在使用automapper的时候经常会遇到这样的问题:假设展示给用户的数据我们用UserDto类,User类就是我 ...
- linux下一个C语言要求CPU采用
部分 从灾难中 本来我想写一个小程序来测试CPU其他工具利用它可以检验类数据的性能.以后参考IPbench中间cpu_target_lukem插件实现我们的功能.原理非常简单:就是我们给程序设置了 ...
- win7在USB该解决方案不健全音箱
Win7安装后,原XP在正常工作USB小喇叭不工作,重新安装声卡驱动程序仍然是相同的.后来,通过以下的得心应手最后一次尝试. 1.右键右下角喇叭button. 2.点击"播放设备" ...
- linux之tail -F命令异常file truncated
使用tail -F收集日志时,经常报出file truncated, 导致日志又重新读取.tail: `test.out' has appeared; following end of new fi ...
- WPF - 本质:数据和行为
原文:WPF - 本质:数据和行为 如果自己来做一个UI框架,我们会首先关注哪些方面?我想UI框架主要处理的一定包括两个主要层次的内容,一个是数据展现,另一个就是数据操作,所以UI框架必须能够接收各种 ...
- js 指向表格行变色,离开恢复
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- 数据绑定(七)使用ObjectDataProvider对象作为Binding的Source
原文:数据绑定(七)使用ObjectDataProvider对象作为Binding的Source ObjectDataProvider就是把对象作为数据源提供给Binding,类似的还有XmlData ...
- Git 将子文件夹分离为一个新的库
前面的需求 公司Android的项目上,想要将一些module抽取出来,作为一个可以被其它项目上使用的. 所以使用了git submodule的方案. 为了将代码库中的一个文件夹分离后,作为一个单独的 ...
- ADB 基础命令使用
1.adb shell(>=2个设备显示:error: more than one device/emulator,仅连接一个设备可用) adb -d shell 只运行在真实设备中 adb - ...