pat04-树9. Path in a Heap (25)
04-树9. Path in a Heap (25)
Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index i, you are supposed to print the path from H[i] to the root.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N and M (<=1000) which are the size of the input sequence, and the number of indices to be checked, respectively. Given in the next line are the N integers in [-10000, 10000] which are supposed to be inserted into an initially empty min-heap. Finally in the last line, M indices are given.
Output Specification:
For each index i in the input, print in one line the numbers visited along the path from H[i] to the root of the heap. The numbers are separated by a space, and there must be no extra space at the end of the line.
Sample Input:5 3 46 23 26 24 10 5 4 3Sample Output:
24 23 10 46 23 10 26 10
关于堆的操作(以大顶堆为例):
1.建堆。
1 MaxHeap Create( int MaxSize ) 2 { /* 创建容量为MaxSize的空的最大堆 */ 3 MaxHeap H = malloc( sizeof( struct HeapStruct ) ); 4 H->Elements = malloc( (MaxSize+1) * sizeof(ElementType)); 5 H->Size = 0; 6 H->Capacity = MaxSize; 7 H->Elements[0] = MaxData; 8 /* 定义“哨兵”为大于堆中所有可能元素的值,便于以后更快操作 */ 9 return H; 10 }
2.插入。
1 void Insert( MaxHeap H, ElementType item ) 2 { /* 将元素item 插入最大堆H, 其中H->Elements[0]已经定义为哨兵 */ 3 int i; 4 if ( IsFull(H) ) { 5 printf("最大堆已满"); 6 return; 7 } 8 i = ++H->Size; /* i指向插入后堆中的最后一个元素的位置 */ 9 for ( ; H->Elements[i/2] < item; i/=2 ) 10 H->Elements[i] = H->Elements[i/2]; /* 向下过滤结点 */ 11 H->Elements[i] = item; /* 将item 插入 */ 12 }
3.删除最大值。
1 ElementType DeleteMax( MaxHeap H ) 2 { /* 从最大堆H中取出键值为最大的元素, 并删除一个结点 */ 3 int Parent, Child; 4 ElementType MaxItem, temp; 5 if ( IsEmpty(H) ) { 6 printf("最大堆已为空"); 7 return; 8 } 9 MaxItem = H->Elements[1]; /* 取出根结点最大值 */ 10 /* 用最大堆中最后一个元素从根结点开始向上过滤下层结点 */ 11 temp = H->Elements[H->Size--]; 12 for( Parent=1; Parent*2<=H->Size; Parent=Child ) { 13 Child = Parent * 2; 14 if( (Child!= H->Size) &&(H->Elements[Child] < H->Elements[Child+1]) ) 15 Child++; /* Child指向左右子结点的较大者 */ 16 if( temp >= H->Elements[Child] ) break; 17 else /* 移动temp元素到下一层 */ 18 H->Elements[Parent] = H->Elements[Child]; 19 } 20 H->Elements[Parent] = temp; 21 return MaxItem; 22 }
代码如下:
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 #include<map> 8 #include<string> 9 using namespace std; 10 int main(){ 11 //freopen("D:\\INPUT.txt","r",stdin); 12 int n,m; 13 scanf("%d %d",&n,&m); 14 int i,j,temp,size=0; 15 int *minheap=new int[n+1]; 16 for(i=1;i<=n;i++){ 17 scanf("%d",&temp); 18 minheap[++size]=temp; 19 for(j=size;j>=2;j/=2){ 20 if(temp<minheap[j/2]){ 21 minheap[j]=minheap[j/2]; 22 } 23 else{ 24 break; 25 } 26 } 27 minheap[j]=temp; 28 } 29 for(i=0;i<m;i++){ 30 scanf("%d",&temp); 31 while(temp){ 32 printf("%d",minheap[temp]); 33 temp==1?printf("\n"):printf(" "); 34 temp/=2; 35 } 36 } 37 return 0; 38 }