int BaseNode::insertNode( int n, BaseNode * pChild ) { if (! pChild ) return -1; if ( this == NULL || pChild == NULL ) return -1; if ( pChild->isChild( this ) ) // assert that this isn't a circular attachment return -1; // grab a reference to the child, otherwise when we detach from the old parent the child // may get deleted. Ref rChild( pChild ); if ( pChild->m_pParent != NULL ) pChild->m_pParent->detachNode( pChild ); updateVersion(); pChild->m_pParent = this; m_Children.insert( m_Children.begin() + n, pChild ); // notify ourselves onAttach( pChild ); // notify the child object pChild->onAttached(); return n; }
void max_heapify(int array[], int length, int root) { int max, l, r; while(root <= length) { l = lChild(root); r = rChild(root); if(l <= length && array[root-1] < array[l-1]) max = l; else max = root; if(r <= length && array[max-1] < array[r-1]) max = r; if(root != max) { exchange(&array[root-1], &array[max-1]); root = max; } else break; } }
/*父子(至多)三者中的大者*/ Rank ProperParent(Rank n, Rank i) { if (rChildValid(n, i)) { return bigger(bigger(i, lChild(i)), rChild(i)); } else if (lChildValid(n, i)) { return bigger(i, lChild(i)); } else { return i; } }
int BaseNode::attachNode( BaseNode * pChild ) { if ( this == NULL || pChild == NULL ) return -1; if ( pChild->isChild( this ) ) // assert that this isn't a circular attachment return -1; // this checks if this node is already attached to this node, if so just pretend we are attaching.. if ( this == pChild->m_pParent ) { for(size_t i=0;i<m_Children.size();++i) if ( m_Children[i] == pChild ) { // notify ourselves onAttach( pChild ); // notify the child object pChild->onAttached(); return (int)i; } return -1; } // grab a reference to the child, otherwise when we detach from the old parent the child // may get deleted. Ref rChild( pChild ); if ( pChild->m_pParent != NULL ) pChild->m_pParent->detachNode( pChild ); // update the internal version number of the parent object, this is important for // serialization reasons. updateVersion(); pChild->m_pParent = this; m_Children.push_back( pChild ); // notify ourselves onAttach( pChild ); // notify the child object pChild->onAttached(); return( m_Children.size() - 1 ); }
bool rChildValid(Rank n, Rank i) { return inHeap(n, rChild(i)); }