堆(Heap)-c实现

这个堆的实现采用数组存储的完全二叉树实现。

最近有点烦躁,先是跳槽到了一个外包公司,感觉2016有点坑,另外一件事就是老婆怀孕了,但是在家里没人照顾,很担心。

这个堆的实现就暂时不优化了,基本的插入,删除实现。

还有,NJ,这个城市真热。

这周希望把剩下的几个数据结构给写了@2016-08-11 00:08:22

  1 #include <stdio.h>
  2 
  3 //#define PF  printf("____________in Function___________ %s\n", __FUNCTION__)
  4 #define PF
  5 
  6 #define HAVETWOSON  1  //表示其有2个子节点
  7 #define HAVEONLYLSON 2 //只有1个左孩子节点
  8 
  9 /*堆的实现采用数组,首先是构建完全二叉树的规则
 10 1.完全二叉树的性质
 11 2.子节点的value小于父节点
 12 3.为了保持数组下标和完全二叉树的对应关系,数组下标选择从1开始处理,第1个数组元素置为heap的大小
 13 4.最近比较事多,老婆怀孕,家里没人照顾,好烦躁。这个堆的实现push和pop有好的方法去实现,但是没心情去好好想,就偷懒了,具体的想法就是,push和pop操作的元素只是i,2i, 2i+1,希望有时间来改改吧。
 14 */
 15 void RevHeap(int *top, int *l, int *r);
 16 int GetMin(int *t, int *l, int *r, int *min);
 17 void PopEle(int *heap);
 18 void PrintBHeapWh(int *top);
 19 void PrintBHeapEl(int *top);
 20 int CheckHeap(int *heap, int pos);
 21 void ConsBHeap(int *heap);
 22 void PushEle(int *heap, int newEle);
 23 
 24 void PushEle(int *heap, int newEle)
 25 {
 26     *heap = *heap + 1;
 27     *(heap + *heap) = newEle;
 28 }
 29 int GetMin(int *t, int *l, int *r, int *min)
 30 {
 31     PF;
 32     //min = (int *)malloc(sizeof(int));
 33     *min = *t;
 34     if(*min > *l)
 35     {
 36         *min = *l;
 37     }
 38     if(*min > *r)
 39     {
 40         *min = *r;
 41     }
 42     if(*min == *l)
 43     {
 44         return 1;
 45     }
 46     else if(*min == *r)
 47     {
 48         return 2;
 49     }else
 50     {// min = *t
 51         return 0;
 52     }
 53 }
 54 void PopEle(int *heap)
 55 {
 56     //PF;
 57     *(heap+1) = *(heap + *heap);
 58     --(*heap);
 59 }
 60 
 61 void PrintBHeapWh(int *top)
 62 {
 63     //PF;
 64     int i ;
 65     for(i=0; i<*top+1; i++)
 66     {
 67         printf("%d ", *(top+i));
 68     }
 69     printf("\n");
 70 }
 71 
 72 
 73 void PrintBHeapEl(int *top)
 74 {
 75     //PF;
 76     int i ;
 77     for(i=1; i<*top+1; i++)
 78     {
 79         printf("%d ", *(top+i));
 80     }
 81     printf("\n");
 82 
 83 }
 84 
 85 void RevHeap(int *top, int *l, int *r)
 86 {
 87 
 88     int *min = NULL;
 89     min = (int *)malloc(sizeof(int));
 90     int ret = -1;
 91     int tmp;
 92     if(r != NULL)
 93     {
 94         ret = GetMin(top, l, r, min);
 95 
 96         if(ret ==1)
 97         {
 98             //l is the min
 99             tmp = *top;
100             *top = *l;
101             *l = tmp;
102         }
103         else if(ret ==2)
104         {
105             tmp = *top;
106             *top = *r;
107             *r = tmp;
108         }
109     }
110     else
111     {
112         *min = *top;
113         if(*min > *l)
114         {
115             tmp = *l;
116             *l =*min;
117             *min = tmp;
118         }
119         *top = *min;
120     }
121     return ;
122 }
123 int CheckHeap(int *heap, int pos)
124 {
125     //PF;
126     if(pos*2+1 <= *heap)
127     {
128         if((*(heap +pos) > *(heap + pos*2))
129          || (*(heap +pos) > *(heap + pos*2+1)))
130         {
131 
132             return HAVETWOSON;
133         }
134     }
135     //只有左子树
136     //if((pos*2 <= size)&&(pos*2+1 > size))
137     if((pos*2 == *heap))
138     {
139         if(*(heap +pos) > *(heap + pos*2))
140         // || (*heap +pos > *heap + pos*2+1))
141         {
142             return HAVEONLYLSON;
143         }
144     }
145     return 0;
146 }
147 
148 
149 void ConsBHeap(int *heap)
150 {
151     PF;
152     int i ;
153     int isNeedCon = 0;
154 
155     for(i = 1; i < *heap+1; i++)
156     {
157         if(CheckHeap(heap, i)==HAVETWOSON)
158         {
159             //printf("proc [%d]th elem [%d]\n", i, *(heap+i));
160             RevHeap(heap +i, (heap + 2*i), (heap+ 2*i+1));
161             //printf("i=[%d] %d  %d %d \n", i, *(heap+i), *(heap + 2*i), *(heap+ 2*i+1));
162             isNeedCon = 1;
163         }
164         else if(CheckHeap(heap, i)== HAVEONLYLSON)
165         {
166             RevHeap(heap+i, (heap + 2*i), NULL);
167             //printf("i=[%d] %d  %d \n", i, *(heap+i), *(heap + 2*i) );
168             isNeedCon = 1;
169         }
170         //printf("[%d]th change.", i);
171         //PrintBHeapEl(heap);
172     }
173     printf("\n");
174 
175     //用递归好写,偷点懒
176     if(isNeedCon)
177     {
178         ConsBHeap(heap);
179     }
180     return;
181 }
182 int main(void)
183 {
184     //int arr[12]={0,4, 5, 2, 1,3, 6, 8, 9, 7, 22, 11};
185     int arr[7]={6, 4, 5, 2, 1,3, 6};
186 
187     //PrintBHeapEl(arr);
188     printf("Berfore process the heap:\n");
189     PrintBHeapWh(arr);
190 
191     ConsBHeap(arr);
192     printf("Consturcct the heap:\n");
193     PrintBHeapWh(arr);
194 
195     PopEle(arr);
196     printf("Pop  the heap:\n");
197     PrintBHeapWh(arr);
198     ConsBHeap(arr);
199     printf("Pop Con the heap:\n");
200     PrintBHeapWh(arr);
201 
202     PushEle(arr, 1);
203     printf("Pop  the heap:\n");
204     PrintBHeapWh(arr);
205     ConsBHeap(arr);
206     printf("Push Con the heap:\n");
207     PrintBHeapWh(arr);
208 
209     return 0;
210 }

运行结果贴下:

Berfore process the heap:
6 4 5 2 1 3 6



Consturcct the heap:
6 1 2 4 5 3 6
Pop  the heap:
5 6 2 4 5 3


Pop Con the heap:
5 2 3 4 5 6
Pop  the heap:
6 2 3 4 5 6 1



Push Con the heap:
6 1 3 2 5 6 4

posted @ 2016-08-11 00:09  ashen~  阅读(633)  评论(0编辑  收藏  举报