// Heap 堆的顺序表存储结构 typedef int WType; // 权值类型定义为整型 typedef struct { WType *key; // 权值 int total; // 当前堆的总数 }Heap;//索引从1开始 void shiftUp(Heap &H, int i) { bool done = false; WType temp; if (i == 1)exit(0); // 结点i为根 do { if (H.key[i] > H.key[i/2]) { temp = H.key[i]; H.key[i] = H.key[i/2]; H.key[i/2] = temp; }// 互换H[i]和H[i/2] else done = true; i = i/2; } while (i != 1 && !done); } void shiftDown(Heap &H, int i) { bool done = false; WType temp; if (2*i > H.total)exit(0);//结点i是叶子 do { i = 2*i; if(i+1 <= H.total && H.key[i+1] > H.key[i]) i=i+1; if(H.key[i/2] < H.key[i]) { temp = H.key[i/2]; H.key[i/2] = H.key[i]; H.key[i] = temp; } else done = true; } while (2*i <= H.total && !done); } void insert(Heap &H, WType x) { H.total++; int n = H.total; H.key[n] = x; shiftUp(H,n); } void deleteH(Heap &H, int i) { int n = H.total; WType x = H.key[i]; WType y = H.key[n]; n = --H.total; if(i == n+1)exit(0);//完成 H.key[i] = y; if(y >= x) shiftUp(H,i); else shiftDown(H,i); } WType deleteMax(Heap &H) { WType x = H.key[1]; deleteH(H,1); return x; } void makeHeap(WType *a, int length, Heap &H) { H.total = length; H.key = (int *)malloc((H.total+1)*sizeof(int));//第一位不用 for(int i = 1; i <= length; i++) { H.key[i] = a[i-1]; } for(i = H.total/2; i >= 1; i--) shiftDown(H,i); } #include <stdio.h> #include <stdlib.h> // Heap 堆的顺序表存储结构 typedef int WType; // 权值类型定义为整型 typedef struct { WType *key; // 权值 int total; // 当前堆的总数 }Heap;//索引从1开始 void shiftUp(Heap &H, int i) { bool done = false; WType temp; if (i == 1)exit(0); // 结点i为根 do { if (H.key[i] > H.key[i/2]) { temp = H.key[i]; H.key[i] = H.key[i/2]; H.key[i/2] = temp; }// 互换H[i]和H[i/2] else done = true; i = i/2; } while (i != 1 && !done); } void shiftDown(Heap &H, int i) { bool done = false; WType temp; if (2*i > H.total)exit(0);//结点i是叶子 do { i = 2*i; if(i+1 <= H.total && H.key[i+1] > H.key[i]) i=i+1; if(H.key[i/2] < H.key[i]) { temp = H.key[i/2]; H.key[i/2] = H.key[i]; H.key[i] = temp; } else done = true; } while (2*i <= H.total && !done); } void insert(Heap &H, WType x) { H.total++; int n = H.total; H.key[n] = x; shiftUp(H,n); } void deleteH(Heap &H, int i) { int n = H.total; WType x = H.key[i]; WType y = H.key[n]; n = --H.total; if(i == n+1)exit(0);//完成 H.key[i] = y; if(y >= x) shiftUp(H,i); else shiftDown(H,i); } WType deleteMax(Heap &H) { WType x = H.key[1]; deleteH(H,1); return x; } void makeHeap(WType *a, int length, Heap &H) { H.total = length; H.key = (int *)malloc((H.total+1)*sizeof(int));//第一位不用 for(int i = 1; i <= length; i++) { H.key[i] = a[i-1]; } for(i = H.total/2; i >= 1; i--) shiftDown(H,i); } void main() { int *array, n;//动态数组和它的长度 int k = 1; printf("Input the length of the array :/n"); scanf("%d",&n); array = (int *)malloc(n*sizeof(int)); printf("Please input the elem of the array:/n"); for(int i = 0; i < n; i++) scanf("%d",array+i); printf("Now the fellow is your array that you input:/n"); for(i = 0; i < n; i++) { printf("array[%d]=%d/t",i,array[i]); if((i+1)%5==0||i==n-1)putchar('/n'); } Heap myH; makeHeap(array,n,myH);//将array转换为堆 printf("The fellow is the heap of your array:/n"); for(i = 1; i <= myH.total; i++) { printf("%d/t",myH.key[i]); if(i%5==0||i==n)printf("/n"); } deleteMax(myH); printf("Delete the max num of the heap:/n"); for(i = 1; i <= myH.total; i++) { printf("%d/t",myH.key[i]); if(i%5==0||i==myH.total)printf("/n"); } printf("Input the index of the heap that you want to delete:/n"); scanf("%d",k); if(k<1||k>myH.total)printf("Your input is wrong!/n"); else deleteH(myH,k); printf("Delete the %d of the heap:/n",myH.key[k]); for(i = 1; i <= myH.total; i++) { printf("%d/t",myH.key[i]); if(i%5==0||i==n)printf("/n"); } }