/* void MovieTree::fixUpAdd(Movie*) Description: Private method used to balance the tree after adding a movie. Example: MovieTree tree; tree.addMoviebyTitle("Back to the Future", 1985, 96, 5); Precondition: The ends of the tree are nil. The tree is arranged correctly. Postcondition: The tree will be balanced. */ void MovieTree::fixUpAdd(Movie *x) { x->isRed = true; while (x != root && x->parent->isRed) { if (x->parent == x->parent->parent->left) { Movie *y = x->parent->parent->right; if (y->isRed) { x->parent->isRed = false; y->isRed = false; x->parent->parent->isRed = true; x = x->parent->parent; } else { if (x == x->parent->right) { x = x->parent; rotateLeft(x); } x->parent->isRed = false; x->parent->parent->isRed = true; rotateRight(x->parent->parent); } } else { Movie *y = x->parent->parent->left; if (y->isRed) { x->parent->isRed = false; y->isRed = false; x->parent->parent->isRed = true; x = x->parent->parent; } else { if (x == x->parent->left) { x = x->parent; rotateRight(x); } x->parent->isRed = false; x->parent->parent->isRed = true; rotateLeft(x->parent->parent); } } } root->isRed = false; }
Orientation rotate( int8_t amount ) const{ switch( normalizedRotation( amount ) ){ case 1: return rotateRight(); case 2: return rotate180(); case 3: return rotate180().rotateRight(); default: return *this; } }
void C_galgas_type_descriptor::recursiveInsert (C_galgas_type_descriptor * & ioRootPtr, C_galgas_type_descriptor * inDescriptor, bool & ioExtension) { if (ioRootPtr == NULL) { ioExtension = true ; ioRootPtr = inDescriptor ; }else{ const int32_t comparaison = strcmp (ioRootPtr->mGalgasTypeName, inDescriptor->mGalgasTypeName) ; if (comparaison > 0) { recursiveInsert (ioRootPtr->mPreviousType, inDescriptor, ioExtension) ; if (ioExtension) { ioRootPtr->mBalance++; if (ioRootPtr->mBalance == 0) { ioExtension = false; }else if (ioRootPtr->mBalance == 2) { if (ioRootPtr->mPreviousType->mBalance == -1) { rotateLeft (ioRootPtr->mPreviousType) ; } rotateRight (ioRootPtr) ; ioExtension = false; } } }else if (comparaison < 0) { recursiveInsert (ioRootPtr->mNextType, inDescriptor, ioExtension) ; if (ioExtension) { ioRootPtr->mBalance-- ; if (ioRootPtr->mBalance == 0) { ioExtension = false ; }else if (ioRootPtr->mBalance == -2) { if (ioRootPtr->mNextType->mBalance == 1) { rotateRight (ioRootPtr->mNextType) ; } rotateLeft (ioRootPtr) ; ioExtension = false; } } }else{ ioExtension = false; C_String errorMessage ; errorMessage << "FATAL ERROR (type '@" << inDescriptor->mGalgasTypeName << "' already defined)" ; fatalError (errorMessage, __FILE__, __LINE__) ; } } }
int AVLTree::ins(AVLNode* &p, FFSObject *e) { int deltaH = 0; if (p == NULL) { p = new AVLNode; #ifdef _DEBUG_AVL AVLTree::tree_cnt++; #endif p->elem = e; p->bal = 0; p->left = p->right = NULL; deltaH = 1; /* tree hight increased by 1 */ #ifdef _DEBUG_AVL fprintf(stderr, "added %s to AVLTree (count %d)\n", e->getName(), count); #endif } else if (compareType == AVL_CMP_NAME ? e->compareName(p->elem) > 0 : e->compareAll(p->elem) > 0) { if (ins(p->right, e)) { p->bal++; /* height of right subtree increased */ if (p->bal == 1) { deltaH = 1; } else if (p->bal == 2) { if (p->right->bal == -1) { rotateRight(p->right); } rotateLeft(p); } } } else /* if (e->compareAll(p->elem) <= 0)*/ { if (ins(p->left, e)) { p->bal--; if (p->bal == -1) { deltaH = 1; } else if (p->bal == -2) { if (p->left->bal == 1) { rotateLeft(p->left); } rotateRight(p); } } } return deltaH; }
/* Notes: * tree insertion using splay method * produces different results to the visualiser */ Link insertSplay(Link n, int value) { if (n == NULL) { return newNode(value); } if (value < n->value){ // right rotations if (n->left == NULL){ // left branch is NULL n->left = newNode(value); } else if (value < n->left->value){ // zig zig left n->left->left = insertSplay(n->left->left, value); // n->left = rotateRight(n->left); n = rotateRight(n); } else{ // zig zag left n->left->right = insertSplay(n->left->right, value); n->left = rotateLeft(n->left); } n = rotateRight(n); } if (value > n->value){ // left rotations if (n->right == NULL){ n->right = newNode(value); } else if (value > n->right->value){ // zig zig right n->right->right = insertSplay(n->right->right, value); // n->right = rotateLeft(n->right); n = rotateLeft(n); } else{ // zig zag right n->right->left = insertSplay(n->right->left, value); n->right = rotateRight(n->right); } n = rotateLeft(n); } return n; }
void armTranslateDPI_MoveImm(ARM_PseudoInstruction& pi, DPI_Opcode op, bool s, bool a, ARM_Condition cond, uint8_t Rd, uint8_t immed, uint8_t rotate_amount) { pi.f = arm_dpi_move_table[a][s][op-FIRST_MOVE][0][Rd==ARM_CPU::PC]; pi.args.dpi_imm.Rd = Rd; pi.args.dpi_imm.set_carry = rotate_amount; pi.args.dpi_imm.immed = rotateRight(immed,rotate_amount); pi.args.any.cond = cond; }
// -1 --- неуспешно включване, елементът вече го има // 0 --- успешно включване, но няма промяна във височината // 1 --- успешно включване и има увеличение на височината с 1 int insertAt(P p, T const& x) { if (!p) { // дъно BinaryTree<AVL>::assignFrom(p, AVL(x)); return 1; } // p --- валидна позиция if ((*p).data() == x) // Грешка! x вече го има return -1; // p && *p != x int result; if (x < (*p).data()) { // вмъкваме наляво result = insertAt(-p, x); if (result == 1) { (*p).balance()--; if ((*p).balance() == -2) { if ((*-p).balance() == 1) rotateLeft(-p); rotateRight(p); result = 0; } } } else { // вмъкваме надясно result = insertAt(+p, x); if (result == 1) { (*p).balance()++; if ((*p).balance() == 2) { if ((*+p).balance() == -1) rotateRight(+p); rotateLeft(p); result = 0; } } } // ако сме вмъкнали успешно и балансът се е получил 0 // значи нямаме промяна във височината if (result >= 0 && (*p).balance() == 0) result = 0; return result; }
bool ExpressionTreeUtils::fixExprPrecedence(Expression*& top, Expression* e) { if ( dynamic_cast<Value*> (e)) return false; if ( dynamic_cast<Empty*> (e)) return false; Operator* op = dynamic_cast<Operator*> (e); bool more_iterations_needed = true; while (more_iterations_needed) { more_iterations_needed = false; // Fix all children for (int operand = 0; operand < op->size(); ++operand) more_iterations_needed = fixExprPrecedence(top, op->at(operand)) || more_iterations_needed; } //Look left if (op->descriptor()->prefix().isEmpty()) { Operator* left = dynamic_cast<Operator*> (op->first()); if (left && left->descriptor()->postfix().isEmpty()) { if (op->descriptor()->precedence() < left->descriptor()->precedence() // Must rotate because of precedence // Must rotate because of associativity. This assumes that the associativity of different operators at the same precedence level is the same. || ( (op->descriptor()->precedence() == left->descriptor()->precedence()) && op->descriptor()->associativity() == OperatorDescriptor::RightAssociative) ) { rotateRight(top, left, op); return true; } } } //Look right if (op->descriptor()->postfix().isEmpty()) { Operator* right = dynamic_cast<Operator*> (op->last()); if (right && right->descriptor()->prefix().isEmpty()) { if (op->descriptor()->precedence() < right->descriptor()->precedence() // Must rotate because of precedence // Must rotate because of associativity. This assumes that the associativity of different operators at the same precedence level is the same. || ( (op->descriptor()->precedence() == right->descriptor()->precedence()) && op->descriptor()->associativity() == OperatorDescriptor::LeftAssociative) ) { rotateLeft(top, right, op); return true; } } } return false; }
int main(void) { setupADC(); setupStepperMotor(); startTimer(); USART_init(); mouse.velocity = 0; mouse.maxVelocity = 5000; mouse.acceleration = 2000; mouse.deceleration = 10000; enableDrive(1); turnOnTimers(1,1); for(int i = 0; i < 10; i++) { int right = isWallRight(); int front = isWallFront(); int left = isWallLeft(); if(!right) { rotateRight(); } else if(front && !left) { rotateLeft(); } else if(front) { moveBackwardsAndCorrect(); } if(left && right) mouse.IR_CORRECT = 20; moveForwardAndStop(); mouse.IR_CORRECT = 0; } turnOnTimers(0, 0); enableDrive(0); while(1==1) { } }
void AVLTreeIndex::restoreBalance(AVLTreeNode *parent, AVLTreeNode *newNode) { // case 1: parent DNE, newNode unbalances if (parent == NULL) { if (newNode->key < root->key) root->balance = biasleft; // newNode inserted left of root else root->balance = biasright; // newNode inserted right of root adjustBalance(root, newNode); } // case 2: insertion of newNode in opposite subtree of parent, balances parent else if (((parent->balance == biasleft) && (newNode->key.compare(parent->key) > 0)) || ((parent->balance == biasright) && (newNode->key.compare(parent->key) < 0))) { parent->balance = balanced; adjustBalance(parent, newNode); } // case 3: insertion of newNode in right child of parent else if (parent->balance == biasright) { if (newNode->key > parent->right->key) { // insertion into right subtree of right child, single rotation left parent->balance = balanced; rotateLeft(parent); adjustBalance(parent->parent, newNode); } else if (newNode->key < parent->right->key) { // insertion into left subtree of right child, double rotation left rotateRight(parent->right); rotateLeft(parent); adjustRL(parent, newNode); } } // case 4: insertion of newNode in left child of parent else if (parent->balance == biasleft) { if (newNode->key < parent->left->key) { // insertion into left subtree of left child, single rotation right parent->balance = balanced; rotateRight(parent); adjustBalance(parent->parent, newNode); } else if (newNode->key < parent->right->key) { // insertion into right subtree of left child, double rotation right rotateLeft(parent->left); rotateRight(parent); adjustLR(parent, newNode); } } }
Node* rotate(Node* gr, Node* par, Node* ch) { if (par->left == ch) { return rotateRight(gr, par, ch); } else { return rotateLeft(gr, par, ch); } }
static spltreeNode_t* delete_min_rc(spltreeNode_t* splnode, spltreeNode_t** min) { if(!splnode->left) { *min = splnode; return splnode->right; } splnode->left = delete_min_rc(splnode->left,min); if(!splnode->left) { return splnode; } return rotateRight(splnode); }
void backToZone(){ rotateRight(25,50); motor[back]=-25; drive(-50); while(nMotorEncoder[back]>-30); motor[back]=0; while(nMotorEncoder[back]<-20); drive(50); wait1Msec(200); drive(0); }
bool Block::rotate(int direction) { switch (direction) { case DIRECTION_LEFT: return rotateLeft(); break; case DIRECTION_RIGHT: return rotateRight(); break; } return false; }
void QMapDataBase::rebalance(QMapNodeBase *x) { QMapNodeBase *&root = header.left; x->setColor(QMapNodeBase::Red); while (x != root && x->parent()->color() == QMapNodeBase::Red) { if (x->parent() == x->parent()->parent()->left) { QMapNodeBase *y = x->parent()->parent()->right; if (y && y->color() == QMapNodeBase::Red) { x->parent()->setColor(QMapNodeBase::Black); y->setColor(QMapNodeBase::Black); x->parent()->parent()->setColor(QMapNodeBase::Red); x = x->parent()->parent(); } else { if (x == x->parent()->right) { x = x->parent(); rotateLeft(x); } x->parent()->setColor(QMapNodeBase::Black); x->parent()->parent()->setColor(QMapNodeBase::Red); rotateRight (x->parent()->parent()); } } else { QMapNodeBase *y = x->parent()->parent()->left; if (y && y->color() == QMapNodeBase::Red) { x->parent()->setColor(QMapNodeBase::Black); y->setColor(QMapNodeBase::Black); x->parent()->parent()->setColor(QMapNodeBase::Red); x = x->parent()->parent(); } else { if (x == x->parent()->left) { x = x->parent(); rotateRight(x); } x->parent()->setColor(QMapNodeBase::Black); x->parent()->parent()->setColor(QMapNodeBase::Red); rotateLeft(x->parent()->parent()); } } } root->setColor(QMapNodeBase::Black); }
void AVL_Tree::balance(Node* city, int counter) { Node* check; while(city){ counter++; if(city->getBalance() == 0) break; else if(city->getBalance() == 2){ check = city->getRight(); counter++; if(city != m_root){ if(city -> getParent() -> getRight() == city) city -> getParent() -> adjustBalance(-1); else city -> getParent() -> adjustBalance(1); } counter++; if(check->getBalance()==-1) rotateRL(city); else rotateLeft(city); city -> setBalance(0); check -> setBalance(0); } else if(city -> getBalance() == -2){ counter++; if(city != m_root){ counter++; if(city -> getParent() -> getRight() == city) city -> getParent() -> adjustBalance(-1); else city -> getParent() -> adjustBalance(1); } check = city -> getLeft(); counter++; if(check -> getBalance() == 1) rotateLR(city); else rotateRight(city); city -> setBalance(0); check -> setBalance(0); } city = city -> getParent(); } std::cout << "Comparison count for this function is " << counter << "\n"; }//end balance
void square() { int i = 0; for(i; i < 4; i++) { driveStraight(); Wait(1400); rotateRight(); Wait(560); } stop(); }
node* balancedTreeInsert(node* root , node* newnode) { if(root == NULL) { return newnode ; } if(less(newnode->key,root->key)) { root->lc = balancedTreeInsert(root->lc , newnode) ; } else { root->rc = balancedTreeInsert(root->rc , newnode) ; } root->ht = max(getHeight(root->lc) , getHeight(root->rc)) + 1 ; if(isBalanced(root)) return root ; z_cnt++ ; int balance = getBalance(root) ; node* ret = NULL ; if(balance > 1) { if(!less(newnode->key , root->lc->key)) { root->lc = rotateLeft(root->lc) ; } ret = rotateRight(root) ; } if(balance < -1) { if(less(newnode->key , root->rc->key)) { root->rc = rotateRight(root->rc) ; } ret = rotateLeft(root) ; } return ret ; }
void QMapPrivateBase::rebalance( NodePtr x, NodePtr& root) { x->color = Node::Red; while ( x != root && x->parent->color == Node::Red ) { if ( x->parent == x->parent->parent->left ) { NodePtr y = x->parent->parent->right; if (y && y->color == Node::Red) { x->parent->color = Node::Black; y->color = Node::Black; x->parent->parent->color = Node::Red; x = x->parent->parent; } else { if (x == x->parent->right) { x = x->parent; rotateLeft( x, root ); } x->parent->color = Node::Black; x->parent->parent->color = Node::Red; rotateRight (x->parent->parent, root ); } } else { NodePtr y = x->parent->parent->left; if ( y && y->color == Node::Red ) { x->parent->color = Node::Black; y->color = Node::Black; x->parent->parent->color = Node::Red; x = x->parent->parent; } else { if (x == x->parent->left) { x = x->parent; rotateRight( x, root ); } x->parent->color = Node::Black; x->parent->parent->color = Node::Red; rotateLeft( x->parent->parent, root ); } } } root->color = Node::Black; }
void AVL_Tree::balance(Node* city) { Node* check; while(city){ if(city->getBalance() == 0) break; else if(city->getBalance() == 2){ check = city->getRight(); if(city != m_root){ if(city -> getParent() -> getRight() == city) city -> getParent() -> adjustBalance(-1); else city -> getParent() -> adjustBalance(1); } if(check->getBalance()==-1) rotateRL(city); else rotateLeft(city); city -> setBalance(0); check -> setBalance(0); } else if(city -> getBalance() == -2){ if(city != m_root){ if(city -> getParent() -> getRight() == city) city -> getParent() -> adjustBalance(-1); else city -> getParent() -> adjustBalance(1); } check = city -> getLeft(); if(check -> getBalance() == 1) rotateLR(city); else rotateRight(city); city -> setBalance(0); check -> setBalance(0); } city = city -> getParent(); } }//end balance
/** * Private helner function to heln bubble un elements with higher nriority */ void percolateUp(BSTNode<Data>* x) { // loop while x is not a root and its priority is higher than // the parent's priority while(x->parent != nullptr && (x->parent)->info < x->info) { if((x->parent)->left == x) rotateRight(x->parent); else rotateLeft(x->parent); } // reset the root pointer if(x == BST<Data>::root) BST<Data>::root = x; }
void DinamicObject::turnLeft() { if(velocity != 0.0f && (SDL_GetTicks() - lastDirModificationTime > minTimeBetweenDirModifs * (maxVelocityAbsoluteValue / abs(int(velocity.absoluteValue))))) { if(velocity > 0) rotateLeft(); else rotateRight(); lastDirModificationTime = SDL_GetTicks(); } }
void LeftBalance(pAVLTree *tree) //要判定插入的 位置,做单旋转,还是双转 { AVLTreeNode* nodeLeft, *nodeRight; nodeLeft = (*tree)->lchild; switch (nodeLeft->balance) { case L_HIGH: //就是插入到了 tree左孩子的,左结点上 (*tree)->balance = nodeLeft->balance = EQUAL_HIGH; rotateRight(tree); //需要右转 break; case EQUAL_HIGH: (*tree)->balance = L_HIGH; break; case R_HIGH: //插入的结点在,tree左孩子的右节点,,双转 nodeRight = (*tree)->rchild; switch (nodeRight->balance) { case L_HIGH: (*tree)->balance = R_HIGH; nodeRight->balance = EQUAL_HIGH; break; case EQUAL_HIGH: (*tree)->balance = nodeRight->balance = EQUAL_HIGH; break; case R_HIGH: (*tree)->balance = EQUAL_HIGH; nodeRight->balance = L_HIGH; break; } nodeRight->balance = EQUAL_HIGH; rotateLeft(&(*tree)->lchild); //左旋 rotateRight(tree); break; default: break; } }
BinaryTree* rotate(BinaryTree *tree) { int nodeDiff = nodeDifference(tree); if(nodeDiff == -2) { BinaryTree *left = leftChild(tree); if(height(leftChild(left)) < height(rightChild(left))) tree->Left = rotateLeft(tree->Left); return rotateRight(tree); } if(nodeDiff == 2) { BinaryTree *right = rightChild(tree); if(height(leftChild(right)) > height(rightChild(right))) tree->Right = rotateRight(tree->Right); return rotateLeft(tree); } return tree; }
int main() { vector<int> v = { 1, 2, 3, 4, 5, 6 }; ListNode *head = new ListNode(v[0]); ListNode *p = head; for (auto i = next(v.begin()); i != v.end(); i++, p = p->next) p->next = new ListNode(*i); head = rotateRight(head, 3); printListNode(head); system("pause"); return 0; }
int main() { int arr[] = {1, 2, 3, 4, 5, 6}; int n = sizeof(arr)/sizeof(arr[0]); int d = 2; //scanf("%d", &d); //rotateLeft(arr, n, d); rotateRight(arr, n, d); printArray(arr, n); return 0; }
Node *insertNode(Node *root, int val) { if(root==NULL) return newNode(val); if(val<root->val) root->left = insertNode(root->left, val);/*insert in left subtree*/ else root->right = insertNode(root->right, val);/*insert in roght subtree*/ root->height = max(height(root->left), height(root->right))+1; int diff = getBalance(root);/*get height difference after node is inserted*/ /*Left Left case. key was inserted in left subtree and Left subtree height > right subtree*/ if(diff>1 && val<root->left->val) { return rotateRight(root); } /*Right Right case. key was inserted in right subtree and Left subtree height < right subtree*/ if(diff<-1 && val>root->right->val) { return rotateLeft(root); } /*Left Right Case. Inserted key in right subtree of left subtree*/ if(diff>1 && val>root->left->val) { root->left = rotateLeft(root->left); return rotateRight(root); } /*Right Left Case. Inserted key in left subtree of right subtree*/ if(diff<-1 && val<root->right->val) { root->right = rotateRight(root->right); return rotateLeft(root); } return root; }
void followReverse(int powerLevel){ reverse(powerLevel); if(SensorValue[LEGOLS1]>50){ rotateLeft(powerLevel); } else if(SensorValue[LEGOLS3]>50){ rotateRight(powerLevel); } if(SensorValue[LEGOLS1]>50&&SensorValue[LEGOLS3]>50){ left(powerLevel); } else if(SensorValue[LEGOLS2]>50){ right(powerLevel); } }
////////////////////////////Line Follow Functions/////////////////////////////// void followForward(int powerLevel){ forward(powerLevel); if(SensorValue[LEGOLS1]>50){ rotateLeft(powerLevel); } else if(SensorValue[LEGOLS3]>50){ rotateRight(powerLevel); } if(SensorValue[LEGOLS1]>50&&SensorValue[LEGOLS3]>50){ left(powerLevel); } else if(SensorValue[LEGOLS2]>50){ right(powerLevel); } }
void DBVH::balanceNode( DBVHNode* node ) { if( node->leaf || (node->left->leaf && node->right->leaf) ) return; int lHeight, rHeight; lHeight = heightOfSubTree(node->left); rHeight = heightOfSubTree(node->right); if(lHeight > rHeight) rotateRight(node); else rotateLeft(node); }