正在加载图片...
第8章图 template <class Type> MinHeap<Type>: MinHeap( int Maxsize)( pHeap=new Type [HMax Size]; template <class Type> MinHeap<Type>:: MinHeap( Type Array, int n)& pHeap=new Type [HMaxSize]: for( int i=0; i <n; i++)pHeap[]=Array [] int iPos=( CurrentSize-1)/2 while(iPos>=0)i FilterDown( iPos, Currentsize ) template <class Type> void MinHeap<Type>: Filter Down( int start, int end)i int i=star,j=2 start +1; Type Temp= pHeap[] if(j <end & pHeap[+1]<= pHeap nl)j++ if( Te pHeap[ =pHeapl: template <class Type> void MinHeap<Type>: FilterUp( int end )i 1)/2; if( pHeap[l <=Temp )break; i=j;j=(j-1)/2; pHeapi= Temp第 8 章 图 103 } template <class Type> MinHeap<Type> :: MinHeap ( int Maxsize ) { HMaxSize = Maxsize; pHeap = new Type[HMaxSize]; CurrentSize = -1; } template <class Type> MinHeap<Type> :: MinHeap ( Type Array[], int n ) { HMaxSize = ( n < MaxHeapSize ) ? MaxHeapSize : n; pHeap = new Type[HMaxSize]; for ( int i = 0; i < n; i++ ) pHeap[i] = Array[i]; CurrentSize = n-1; int iPos = ( CurrentSize - 1 ) / 2; while ( iPos >= 0 ) { FilterDown ( iPos, CurrentSize ); iPos--; } } template <class Type> void MinHeap<Type> :: FilterDown ( int start, int end ) { int i = start, j = 2 * start + 1; Type Temp = pHeap[i]; while ( j <= end ) { if ( j < end && pHeap[j+1] <= pHeap[j] ) j++; if ( Temp <= pHeap[j] ) break; pHeap[i] = pHeap[j]; i = j; j = 2 * j + 1; } pHeap[i] = Temp; } template <class Type> void MinHeap<Type> :: FilterUp ( int end ) { int i = end, j = ( end - 1 ) / 2; Type Temp = pHeap[i]; while ( i > 0 ) { if ( pHeap[j] <= Temp ) break; pHeap[i] = pHeap[j]; i = j; j = ( j - 1 ) / 2; } pHeap[i] = Temp; }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有