static PriorityQueue Merge1(PriorityQueue H1, PriorityQueue H2) { if (H1->Left == NULL) // 单节点 H1->Left = H2; else { H1->Right = Merge(H1->Right, H2); SwapChildren(H1); } return H1; }
static PriorityQueue Merge1(PriorityQueue H1, PriorityQueue H2) { if(H1->Left == NULL){ //single node(leftist's left child npl >= right child npl) H1->Left = H2; }else{ H1->Right = Merge(H1->Right, H2); if(H1->Left->Npl < H1->Right->Npl){ SwapChildren(H1); } H1->Npl = H1->Right->Npl + 1; } return H1; }
static PriorityQueue Merge1(PriorityQueue H1,PriorityQueue H2) { if(H1->Left == NULL) H1->Left = H2; else { H1->Right = Merge(H1->Right,H2); if(H1->Left->Np1 < H1->Right->Np1) SwapChildren(H1); H1->Np1 = H1->Right->Np1 + 1; } return H1; }
/* START: fig6_27.txt */ static PriorityQueue Merge1( PriorityQueue H1, PriorityQueue H2 ) { /* 1*/ if( H1->Left == NULL ) /* Single node */ /* 2*/ H1->Left = H2; /* H1->Right is already NULL, H1->Npl is already 0 */ else { /* 3*/ H1->Right = Merge( H1->Right, H2 ); /* 4*/ if( H1->Left->Npl < H1->Right->Npl ) /* 5*/ SwapChildren( H1 ); /* 6*/ H1->Npl = H1->Right->Npl + 1; } /* 7*/ return H1; }
//----------------------------------------------------------------// void MOAIProfilerEntryBase::SortChildren () { if ( mNumChildren > 1 ) { if ( mNumChildren > 2 ) { // Bubble sort is acceptable in this case, because there will be almost no swaps after // a few frames, so the algorithm basically just runs over the child nodes once. bool swapped = true; while ( swapped ) { swapped = false; MOAIProfilerEntryBase* child = mFirstChild; while ( child->mNext ) { MOAIProfilerEntryBase* left = child; MOAIProfilerEntryBase* right = child->mNext; child = child->mNext; if ( left->IsLessThan( *right ) ) { left->SwapWith ( *right ); SwapChildren ( *left, *right ); swapped = true; } } } } else { if ( mFirstChild->IsLessThan ( *mLastChild ) ) { MOAIProfilerEntryBase* temp = mLastChild; mLastChild = mFirstChild; mFirstChild = temp; mFirstChild->mNext = mLastChild; mLastChild->mNext = 0; } } } }