动态堆
来源:互联网 发布:手机淘宝联盟怎么提现 编辑:程序博客网 时间:2024/04/30 14:47
在一个最小堆中减少其中元素的值,这个堆能够动态地维护结构。这样就不需要多次对程序进行排序。
[0]= 0 1 ||[1]= 1 2 ||[2]= 4 3 ||[3]= 9 9 ||[4]= 16 10 ||[5]= 5 6 ||[6]= 16 7 ||[7]= 9 8 ||[8]= 4 4 ||[9]= 1 5 ||
[0]= 0 2 ||[1]= 1 5 ||[2]= 4 3 ||[3]= 9 9 ||[4]= -3 1 ||[5]= 5 6 ||[6]= 16 7 ||[7]= 9 8 ||[8]= 4 4 ||[9]= 1 10 ||
[0]= 0 2 ||[1]= 1 5 ||[2]= 4 7 ||[3]= 9 9 ||[4]= -3 3 ||[5]= 5 6 ||[6]= -174 1 ||[7]= 9 8 ||[8]= 4 4 ||[9]= 1 10 ||
-174 1
-3 3
0 2
1 10
1 5
4 7
4 4
5 6
9 8
9 9
¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
#include <stdio.h>
#include <stdlib.h>
#define N 150
typedef struct wrap_data
{
int data;
int pos;
}wrap_data;
wrap_data * min_heap[N]={0};
int count=1;
int left(i)
{
return 2*i;
}
int right(i)
{
return 2*i+1;
}
void insert_help(wrap_data *point_a)
//void insert_help(int a)
{
wrap_data * temp;
int exchange;
int label=1,sign=0;
if(point_a->data>min_heap[label]->data )
{
min_heap[label]=point_a;
}
else
{
return ;
}
while(left(label)<=count-1)
{
if(right(label)<=count-1)
{
if(min_heap [left (label)]->data < min_heap [right (label)]->data )
exchange=left (label) ;
else
exchange= right (label);
}
else
exchange=left (label) ;
if(min_heap[exchange]->data < min_heap[label]->data )
{
temp=min_heap[exchange];
min_heap[exchange]=min_heap[label];
min_heap[label]=temp;
}
else
break;
label=exchange;
}
}
void insert(wrap_data *point_a)
//void insert(int a)
{
wrap_data * temp;
int pos_temp;
int p, num;
if(count==N)
{
insert_help(point_a);
return ;
}
min_heap[count]=point_a;
point_a->pos=count;
num=count;
while(num/2)
{
p=num/2;
if(min_heap[num]->data<min_heap[p]->data)
{
temp=min_heap[num];min_heap[num]=min_heap[p];min_heap[p]=temp;
pos_temp=min_heap[num]->pos;min_heap[num]->pos=min_heap[p]->pos;min_heap[p]->pos=pos_temp;
}
else
{
break;
}
num=p;
}
count++;
if(count>N)count=N;
}
//update similar to insert ,just a little different
void update(int pos, int minus )
{
wrap_data * temp;
int num,p;
int pos_temp;
num=pos;
min_heap[num]->data-=minus; //add by chenbing 2011.11.7
while(num/2)
{
p=num/2;
if(min_heap[num]->data<min_heap[p]->data)
{
temp=min_heap[num];min_heap[num]=min_heap[p];min_heap[p]=temp;
pos_temp=min_heap[num]->pos;min_heap[num]->pos=min_heap[p]->pos;min_heap[p]->pos=pos_temp;
}
else
{
break;
}
num=p;
}
}
wrap_data * pop()
{
wrap_data * temp,*result=min_heap[1];
int exchange;
int label=1;
min_heap[label]=min_heap[count-1];
while(left(label)<=count-1)
{
if(right(label)<=count-1)
{
if(min_heap [left (label)]->data < min_heap [right (label)]->data )
exchange=left (label) ;
else
exchange= right (label);
}
else
exchange=left (label) ;
if(min_heap[exchange]->data<min_heap[label]->data)
{
temp=min_heap[exchange];min_heap[exchange]=min_heap[label];min_heap[label]=temp;
}
else
break;
label=exchange;
}
count--;
return result;
}
int empty()
{
return !(count-1);
}
int main()
{
wrap_data *result;
int i=0;
wrap_data *a[100]={0};
for(i=0;i<10;i++)
{
a[i]=(wrap_data*) calloc (1, sizeof (wrap_data ));
a[i]->data=i*i%20;
insert(a[i]);
}
for (i=0;i<10;i++)
{
printf ("[%d]= %d %d ||",i,a[i]->data, a[i]->pos );
}
printf("\n");
update ( a[4]->pos , 19);
for (i=0;i<10;i++)
{
printf ("[%d]= %d %d ||",i,a[i]->data, a[i]->pos );
}
printf("\n");
update ( a[6]->pos , 190);
for (i=0;i<10;i++)
{
printf ("[%d]= %d %d ||",i,a[i]->data, a[i]->pos );
}
printf("\n");
while(!empty())
{
result=pop();
printf("%d %d\n",result->data,result->pos);
// printf("%d \n",result->data);
}
}
- 动态堆
- DHAT:动态堆分析工具
- 动态存储--堆分配--数组
- 支持动态维护的堆排序
- 动态链表实现堆排序
- C++动态数组简单模拟二叉堆
- C++动态内存分配(堆)
- 浅析动态内存分配栈与堆
- hdu3282 动态中位数(用堆实现)
- 堆排序:动态数组求中位数
- USACO|Barn Repair|堆|贪心|动态规划
- 指针·堆内存分配(动态内存)
- 堆/栈/动态存储方式/静态存储方式
- 动态规划解循环石子堆合并问题
- 堆-栈-动态存储方式-静态存储方式
- C语言中动态内存的堆和栈
- 堆/栈/动态存储方式/静态存储方式
- 静态存储区、动态存储区、堆、栈理解
- 大小端存储的问题
- 一天一小步(排序——3)
- OpenStack Nova Architecture
- 好的测试(Q\L\G\F)
- Common Code Smell
- 动态堆
- 虚拟蜜网Honeywall的安装
- mfc dll原理!
- Linux系统利用Crontab命令实现定时重启
- 如何制作Win XP操作系统映像文件
- Green Plum测试报告
- oracle 10g 随linux系统自动启动的设置脚本
- Tomcat发布程序
- FormClosing事件添加对话框重复执行的问题