link deleteMax(link h) { if (hl->red) h = rotR(h); if (hr == z) { free(h); return z; } if (!hr->red && !hrl->red) h = mvRedR(h); hr = deleteMax(hr); return balance(h); }
link LLRBinsert(link h, Item *item) { Key v = key(item); /* Insert a new node at the bottom*/ conflict = NULL; if (h == z) { return NEW(item, z, z, 1, 1); } if (less(v, key(h->item))) hl = LLRBinsert(hl, item); else if (eq(v, key(h->item))) { /* If the object we are trying to insert is already there, we opt to return a pointer to the existing item, so that the user may choose what to do (i.e. create a list of items) */ conflict = h->item; } else hr = LLRBinsert(hr, item); /* Enforce left-leaning condition */ if (hr->red && !hl->red) h = rotL(h); if (hl->red && hll->red) h = rotR(h); if (hl->red && hr->red) colorFlip(h); return fixNr(h); }
Node* OPfunction(Node *N){ switch(verifBal(N)){ case(NO_ACTION):{ break; } case(SIMPLE_LEFT):{ printf("\nSIMPLE LEFT"); N = rotL(N); break; } case(SIMPLE_RIGHT):{ printf("\nSIMPLE RIGHT"); N = rotR(N); break; } case(DOUBLE_LR):{ printf("\nDOUBLE LR"); N = rotLR(N); break; } case(DOUBLE_RL):{ printf("\nDOUBLE RL"); N = rotRL(N); break; } } updateBal(N); return(N); }
BTNode* insert_node(BTNode* t ,int item, int sw){//sw是分左右的,key 0是黑,1是红 if(t==null) return NODE(item,null,null,1); if(t->l->key && t->r->key){ t->key=1;t->l->key=0;t->r->key=0; } if(item<t->item){ t->l=insert_node(t->l,item,0); if(t->key && t->l->key && sw) t=rotR(t); if(t->l->key && t->l->l->key) { t=rotR(t); t->key=0; t->r->key=1; } }else{ t->r=insert_node(t->r,item,1); if(t->key && t->r->key && !sw) t=rotL(t); if(t->r->key && t->r->r->key) { t=rotL(t); t->key=0; t->l->key=1; } } return t; }
link rsRBinsert(link h, Item item, int sw) // Sedgewick's code, with details included - NOT TESTED { Key v = key(item); if (h == z) return NEW(item, z, z, 1); if ((h->l->red) && (h->r->red)) { h->red = 1; h->l->red = 0; h->r->red = 0; } if (less(v, key(h->item))) { h->l = rsRBinsert(h->l, item, 0); if (h->red && h->l->red && sw) h = rotR(h); if (h->l->red && h->l->l->red) { h = rotR(h); h->red = 0; h->r->red = 1; } } else { h->r = rsRBinsert(h->r, item, 1); if (h->red && h->r->red && !sw) h = rotL(h); if (h->r->red && h->r->r->red) { h = rotL(h); h->red = 0; h->l->red = 1; } } fixRank(h); return h; }
// BST根插入 link insertT(link h, Item item){ Key v = key(item); if(h == z) return NEW(item,z,z,1); if(less(v,key(h->item))) { h->l = insertT(h->l, item); h = rotR(h); } else { h->r = insertT(h->r, item); h = rotL(h); } return h; }
//查找 link searchR(link h, Key v){ //Key t = key(h->item); Key t = h->item; link tmp = NULL; nr++; if (h == z) return NULL; if eq(v, t) return h; if less(v, t){ if ((tmp = searchR(h->l, v)) != NULL){ h->l = tmp; h = rotR(h); } else return NULL; } else{ if ((tmp = searchR(h->r, v)) != NULL){
link deleteR(link h, Key v) { Key t = key(h->item); if (less(v,t)) { if (!hl->red && !hll->red) h = mvRedL(h); hl = deleteR(hl, v); } else { if (hl->red) h = rotR(h); if (eq(v,key(h->item)) && hr == z) free(h); return z; if (!hr->red && !hrl->red) h = mvRedR(h); if (eq(v,key(h->item))) { h->item = getMin(hr); hr = deleteMin(hr); } else hr = deleteR(hr, v); } return balance(h); }
static link LLRBinsert(link h, EnglishWord newWord){ Key v = makeKey(newWord); int val = 0; if (compareNodes(h,z)) return NEW(newWord, z, z, 1); val = compareKeys(v,makeKey(h->englishWord)); if ( val < 0) hl = LLRBinsert(hl, newWord); else hr = LLRBinsert(hr, newWord); if (hr->red && !hl->red) h = rotL(h); if (hl->red && hll->red) h = rotR(h); if (hl->red && hr->red) colorFlip(h); return h; }
/****************************************************************************************** * AVLbalance() * * Arguments: h: ponteiro para um no da arvore * * Returns: link retorna um no da arvore * Description: balanceia a arvore AVL *****************************************************************************************/ link AVLbalance(link h){ int balanceFactor; if (h==NULL) return h; balanceFactor = Balance(h); if(balanceFactor > 1){ if (Balance(h->l) >= 0) h=rotR(h); else h=rotLR(h); } else if(balanceFactor < -1){ if (Balance(h->r) <= 0) h = rotL(h); else h = rotRL(h); } else{ int peso_left = peso(h->l); int peso_right = peso(h->r); h->peso = peso_left > peso_right ? peso_left + 1 : peso_right + 1; } return h; }
link RBinsert(link h, Item item, int sw) // Program 13.6 coded to be a bit clearer and make mutually exclusive // cases obvious. Also includes tracing. See 2320 notes. BPW // h is present node in search down tree. // Returns root of modified subtree. // item is the Item to be inserted. // sw == 1 <=> h is to the right of its parent. { Key v = key(item); link before; // Used to trigger printing of an intermediate tree tracePrint("Down",h); if (h == z) return NEW(item, z, z, 1); // Attach red leaf if ((h->l->red) && (h->r->red)) // Flip colors before searching down { tracePrint("Color flip",h); h->red = 1; h->l->red = 0; h->r->red = 0; if (trace==2) STprintTree(); } if (less(v, key(h->item))) { tracePrint("Insert left",h); before=h->l; h->l = RBinsert(h->l, item, 0); // Insert in left subtree if (trace==2 && before!=h->l) // Has a rotation occurred? STprintTree(); if (h->l->red) if (h->red) if (sw) { tracePrint("Case ~1",h); h = rotR(h); // Set up case ~2 after return } else ; else if (h->l->l->red) { tracePrint("Case 2",h); h = rotR(h); h->red = 0; h->r->red = 1; } } else { tracePrint("Insert right",h); before=h->r; h->r = RBinsert(h->r, item, 1); // Insert in right subtree if (trace==2 && before!=h->r) // Has a rotation occurred? STprintTree(); if (h->r->red) if (h->red) if (!sw) { tracePrint("Case 1",h); h = rotL(h); // Set up case 2 after return } else ; else if (h->r->r->red) { tracePrint("Case ~2",h); h = rotL(h); h->red = 0; h->l->red = 1; } } fixRank(h); //fix the rank after rotating tracePrint("Up",h); return h; }
link mvRedL(link h) { colorFlip(h); if (hrl->red) { hr = rotR(hr); h = rotL(h); } return h; }
/****************************************************************************************** * rotRL() * * Arguments: h: ponteiro para um no da arvore * * Returns: link * Description: faz uma rotacao dupla direita esquerda *****************************************************************************************/ link rotRL(link h){ if (h==NULL) return h; h->r = rotR(h->r); return rotL(h); }
/****************************************************************************************** * rotLR() * * Arguments: h: ponteiro para um no da arvore * * Returns: link * Description: faz uma rotacao dupla esquerda direita *****************************************************************************************/ link rotLR(link h){ if (h == NULL) return h; h->l = rotL(h->l); return rotR(h); }
void DMPDSExporter::fillBones( DMPSkeletonData::SubSkeletonStruct* subSkel, string parent, DMPParameters* param, MDagPath& jointDag ) { MStatus status; if (jointDag.apiType() != MFn::kJoint) { return; // early out. } DMPSkeletonData::BoneStruct newBone; newBone.boneHandle = (unsigned int)subSkel->bones.size(); newBone.name = jointDag.partialPathName().asUTF8(); newBone.parentName = parent; MFnIkJoint fnJoint(jointDag, &status); // matrix = [S] * [RO] * [R] * [JO] * [IS] * [T] /* These matrices are defined as follows: •[S] : scale •[RO] : rotateOrient (attribute name is rotateAxis) •[R] : rotate •[JO] : jointOrient •[IS] : parentScaleInverse •[T] : translate The methods to get the value of these matrices are: •[S] : getScale •[RO] : getScaleOrientation •[R] : getRotation •[JO] : getOrientation •[IS] : (the inverse of the getScale on the parent transformation matrix) •[T] : translation */ MVector trans = fnJoint.getTranslation(MSpace::kTransform); double scale[3]; fnJoint.getScale(scale); MQuaternion R, RO, JO; fnJoint.getScaleOrientation(RO); fnJoint.getRotation(R); fnJoint.getOrientation(JO); MQuaternion rot = RO * R * JO; newBone.translate[0] = trans.x * param->lum; newBone.translate[1] = trans.y * param->lum; newBone.translate[2] = trans.z * param->lum; newBone.orientation[0] = rot.w; newBone.orientation[1] = rot.x; newBone.orientation[2] = rot.y; newBone.orientation[3] = rot.z; newBone.scale[0] = scale[0]; newBone.scale[1] = scale[1]; newBone.scale[2] = scale[2]; subSkel->bones.push_back(newBone); // Load child joints for (unsigned int i=0; i<jointDag.childCount();i++) { MObject child; child = jointDag.child(i); MDagPath childDag = jointDag; childDag.push(child); fillBones(subSkel, newBone.name, param, childDag); } // now go for animations if (param->bExportSkelAnimation) { for (unsigned int i = 0; i < subSkel->animations.size(); ++i) { DMPSkeletonData::TransformAnimation& anim = subSkel->animations[i]; DMPSkeletonData::TransformTrack subTrack; subTrack.targetBone = newBone.name; MPlug plugT; // translate MPlug plugR; // R MPlug plugRO; // RO MPlug plugJO; // JO MPlug plugS; // scale double dataT[3]; double dataR[3]; double dataRO[3]; double dataJO[3]; double dataS[3]; MFnDependencyNode fnDependNode( jointDag.node(), &status ); plugT = fnDependNode.findPlug("translate", false, &status); plugR = fnDependNode.findPlug("rotate", false, &status); plugRO = fnDependNode.findPlug("rotateAxis", false, &status); plugJO = fnDependNode.findPlug("jointOrient", false, &status); plugS = fnDependNode.findPlug("scale", false, &status); float timeStep = param->samplerRate; if (param->animSampleType == DMPParameters::AST_Frame) { timeStep /= param->fps; } for (float curTime = anim.startTime; curTime <= anim.endTime; curTime += timeStep) { MTime mayaTime; DMPSkeletonData::TransformKeyFrame keyframe; keyframe.time = curTime - anim.startTime; mayaTime.setUnit(MTime::kSeconds); mayaTime.setValue(curTime); // Get its value at the specified Time. plugT.child(0).getValue(dataT[0], MDGContext(mayaTime)); plugT.child(1).getValue(dataT[1], MDGContext(mayaTime)); plugT.child(2).getValue(dataT[2], MDGContext(mayaTime)); plugR.child(0).getValue(dataR[0], MDGContext(mayaTime)); plugR.child(1).getValue(dataR[1], MDGContext(mayaTime)); plugR.child(2).getValue(dataR[2], MDGContext(mayaTime)); plugRO.child(0).getValue(dataRO[0], MDGContext(mayaTime)); plugRO.child(1).getValue(dataRO[1], MDGContext(mayaTime)); plugRO.child(2).getValue(dataRO[2], MDGContext(mayaTime)); plugJO.child(0).getValue(dataJO[0], MDGContext(mayaTime)); plugJO.child(1).getValue(dataJO[1], MDGContext(mayaTime)); plugJO.child(2).getValue(dataJO[2], MDGContext(mayaTime)); plugS.child(0).getValue(dataS[0], MDGContext(mayaTime)); plugS.child(1).getValue(dataS[1], MDGContext(mayaTime)); plugS.child(2).getValue(dataS[2], MDGContext(mayaTime)); // fill the frame. keyframe.translate[0] = dataT[0] * param->lum; keyframe.translate[1] = dataT[1] * param->lum; keyframe.translate[2] = dataT[2] * param->lum; // calculate quaternion. MEulerRotation rotR(dataR[0], dataR[1], dataR[2]); MEulerRotation rotRO(dataRO[0], dataRO[1], dataRO[2]); MEulerRotation rotJO(dataJO[0], dataJO[1], dataJO[2]); MQuaternion finalRot = rotRO.asQuaternion()*rotR.asQuaternion()*rotJO.asQuaternion(); keyframe.orientation[0] = finalRot.w; keyframe.orientation[1] = finalRot.x; keyframe.orientation[2] = finalRot.y; keyframe.orientation[3] = finalRot.z; keyframe.scale[0] = dataS[0]; keyframe.scale[1] = dataS[1]; keyframe.scale[2] = dataS[2]; subTrack.frames.push_back(keyframe); } anim.tracks.push_back(subTrack); } } }
Node* rotRL(Node *N){ N->r = rotR(N->r); N = rotL(N); return(N); }
Node* rotLR(Node *N){ N->l = rotL(N->l); N = rotR(N); return(N); }
link mvRedR(link h) { colorFlip(h); if (hll->red) { h = rotR(h); } return h; }
link balance(link h) { if (hr->red) h = rotL(h); if (hl->red && hll->red) h = rotR(h); if (hl->red && hr->red) colorFlip(h); return fixNr(h); }