Esempio n. 1
0
Tree balanceLeft(Tree t) {

  if(t -> left -> bf == LH) {
    // simple (rotate right)
    t = rotateRight(t);
    t -> bf = EH;
    t -> right -> bf = EH;
  }
  else {
    // double (rotate left and then right)
    t -> left = rotateLeft(t -> left);
    t = rotateRight(t);
    switch(t->bf) {
      case EH:
        t -> left -> bf = EH;
        t -> right -> bf = EH;
        break;
      case RH:
        t -> left -> bf = LH;
        t -> right -> bf = EH;
        break;
      case LH:
        t -> left -> bf = EH;
        t -> right -> bf = RH;
    }
    t -> bf = EH;
  }

  return t;
}
Esempio n. 2
0
void Align::rotatePolar(double angle){

    rotateLeft(3*M_PI/2);    
    double seconds =  ((angle) * ROBOT_DIAMETER * M_PI) / (2 * M_PI * 3.0 * WHEEL_RAD);
	
	seconds = seconds - (9.15*(seconds / 100));

	//stop
	if(seconds < 0.0){
		diffDrive.call(msg_turn_right);
		seconds = seconds * -1;
	}else{
	diffDrive.call(msg_turn_left);
	}



        //sleep until at correct angle
        ros::Duration(seconds).sleep();

        //stop rotating
        diffDrive.call(msg_stop);
        //ros::Duration(3).sleep();
        ready = 1;
	//ROS_INFO("Rotate Position: %d", position);
}
void MyGallery::setupActions()
{
  p->toolbar->addAction(QIcon::fromTheme("object-rotate-left"),
                  tr("Rotate Left"), p->viewer->obj(), SLOT(rotateLeft()));
  p->toolbar->addAction(QIcon::fromTheme("object-rotate-right"),
                  tr("Rotete Right"), p->viewer->obj(), SLOT(rotateRight()));
}
Esempio n. 4
0
void AVLTree<K, V>::rotateLeftRight(Node * & t)
{
        *_out << __func__ << endl; // Outputs the rotation name (don't remove this)
        // Implemented for you:
        rotateLeft(t->left);
        rotateRight(t);
}
Esempio n. 5
0
void AVLTree<K, V>::insert(Node* & subtree, const K & key, const V & value)
{
        if(subtree == NULL) {
                subtree = new Node(key, value);
                return;
        }      
        if(subtree->key == key) return;
 
        if(key < subtree->key) {
                insert(subtree->left, key, value);
        } else {
                insert(subtree->right, key, value);
        }
        if(heightOrNeg1(subtree->right) - heightOrNeg1(subtree->left) == 2) {
                if(subtree->right != NULL) {
                        if(heightOrNeg1(subtree->right->left) > heightOrNeg1(subtree->right->right)) {
                                rotateRightLeft(subtree);
                        } else if (heightOrNeg1(subtree->right->left) < heightOrNeg1(subtree->right->right)) {
                                rotateLeft(subtree);
                        }
                }
        } else if(heightOrNeg1(subtree->right) - heightOrNeg1(subtree->left) == -2) {
                if(subtree->left != NULL) {
                        if(heightOrNeg1(subtree->left->right) > heightOrNeg1(subtree->left->left)) {
                                rotateLeftRight(subtree);
                        } else if(heightOrNeg1(subtree->left->right) < heightOrNeg1(subtree->left->left)) {
                                rotateRight(subtree);
                        }
                }      
        }
        subtree->height = calculateSubtreeHeight(subtree);
}
Esempio n. 6
0
void AVLTree<K, V>::rotateRightLeft(Node * & t)
{
	*_out << __func__ << endl; // Outputs the rotation name (don't remove this)
    // your code here
	rotateRight(t->right);
	rotateLeft(t);
}
Esempio n. 7
0
/*Balanceamento da AVL a esquerda*/
AVL balanceLeft(AVL p) {
	if(p->left->bf==LH) {
		p=rotateRight(p);
		p->bf=EH;
		p->right->bf=EH;
	}
	else {
		p->left=rotateLeft(p->left);
		p=rotateRight(p);
		switch(p->bf) {
		case EH:
			p->left->bf=EH;
			p->right->bf=EH;
			break;
		case RH:
			p->right->bf=EH;
			p->left->bf=LH;
			break;
		case LH:
			p->right->bf=RH;
			p->left->bf=EH;
		}
		p->bf=EH;
	}
	return p;
}
Esempio n. 8
0
void MainWindow::initButtomBar()
{
    bottomFrame = new FloatFrame(this);
    ///set all the button's focous policy to Qt::NoFocous in 'ui' file.

    contralBar = new ContralBar(bottomFrame);
    bottomFrame->addWidget(contralBar);

    QHBoxLayout *hlayout = new QHBoxLayout(bottomFrame);
    hlayout->setContentsMargins(0,0,0,0);   ///qframe's layout margis default is not 0.
    hlayout->setAlignment(Qt::AlignCenter);
    hlayout->addWidget(contralBar);
    bottomFrame->setLayout(hlayout);

    settingButton = contralBar->settingButton;
    openButton = contralBar->openButton;
    preButton = contralBar->preButton;
    playButton = contralBar->playButton;
    nextButton = contralBar->nextButton;
    rotateLeftButton = contralBar->rotateLeftButton;
    rotateRightButton = contralBar->rotateRightButton;
    deleteButton = contralBar->deleteButton;

    connect(settingButton, SIGNAL(clicked()), SLOT(setting()));
    connect(openButton, SIGNAL(clicked()), SLOT(openFile()));
    connect(preButton, SIGNAL(clicked()), viewer, SLOT(prePic()));
    connect(playButton, SIGNAL(clicked()), SLOT(switchSlideShow()));
    connect(nextButton, SIGNAL(clicked()), viewer, SLOT(nextPic()));
    connect(rotateLeftButton, SIGNAL(clicked()), viewer, SLOT(rotateLeft()));
    connect(rotateRightButton, SIGNAL(clicked()), viewer, SLOT(rotateRight()));
    connect(deleteButton, SIGNAL(clicked()), viewer, SLOT(deleteFileAsk()));

    bottomFrame->installEventFilter(this);
}
Esempio n. 9
0
FlightPanel::FlightPanel(QWidget *parent):
    QWidget(parent),
    wi(this),
    timer(this)
{
    up.reset(new QPushButton(QIcon(), QString("up"), this));
    down.reset(new QPushButton(QIcon(), QString("down"), this));
    left.reset(new QPushButton(QIcon(), QString("left"), this));
    right.reset(new QPushButton(QIcon(), QString("right"), this));

    up->resize(   controlHeight, controlHeight/2.5);
    down->resize( controlHeight, controlHeight/2.5);
    left->resize( controlHeight, controlHeight/2.5);
    right->resize(controlHeight, controlHeight/2.5);

    QRect rect = QRect(0, 0, this->width(), this->height() - 50);
    wi.setGeometry(rect);
    wi.setVisible(true);

    QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));

    QObject::connect(up.get(), SIGNAL(clicked()),    &wi, SLOT(rotateUp()));
    QObject::connect(down.get(), SIGNAL(clicked()),  &wi, SLOT(rotateDown()));
    QObject::connect(left.get(), SIGNAL(clicked()),  &wi, SLOT(rotateLeft()));
    QObject::connect(right.get(), SIGNAL(clicked()), &wi, SLOT(rotateRight()));
    timer.setInterval(200);
    timer.start();
}
void rotate()
{	
	if(speed > 10)
		rotateLeft(MAXSPEED/(speed-10));
	else if(speed < 10)
		rotateRight(MAXSPEED/(10-speed));
}
Esempio n. 11
0
int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QMainWindow::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: openFile(); break;
        case 1: applyChanges(); break;
        case 2: saveFile(); break;
        case 3: exit(); break;
        case 4: effects(); break;
        case 5: zoom(); break;
        case 6: resize(); break;
        case 7: crop(); break;
        case 8: rotateRight(); break;
        case 9: rotateLeft(); break;
        case 10: mirror(); break;
        case 11: reflection(); break;
        case 12: grayScale(); break;
        case 13: xray(); break;
        case 14: sepia(); break;
        default: ;
        }
        _id -= 15;
    }
    return _id;
}
Esempio n. 12
0
void print_permutation(char *wordRa){
	static count;
	char singleWord[2];
	char aheadWord[10],currentwordRa[10];
	int index,wordRalength,i;
	wordRalength=strlen(wordRa);
	if(wordRalength>2){
		
		strcpy(currentwordRa,wordRa);
		for(i=0;i<wordRalength;i++){
			singleWord[0]=currentwordRa[0];
			singleWord[1]='\0';
			strcat(toBePrinted,singleWord);
			strcpy(aheadWord,currentwordRa+1);
			print_permutation(aheadWord);
			index=strlen(toBePrinted)-1;
			toBePrinted[index]='\0';
			rotateLeft(currentwordRa);
		}
		
	}else
	{
		//printf("\n%d)TOBEpTelse=%s",++count,toBePrinted);
		printf("\n%s",toBePrinted);
		printf("%s",wordRa);
		//printf("\n%d)TOBEpTelse=%s",++count,toBePrinted);
		printf("\n%s",toBePrinted);
		printf("%c%c",wordRa[1],wordRa[0]);
	}
}
/*
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;
}
Esempio n. 14
0
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 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__) ;
    }
  }
}
Esempio n. 17
0
	// -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;
	}
Esempio n. 18
0
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;
}
Esempio n. 19
0
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)
	{

	}	
}
Esempio n. 20
0
bnode *addNode(bnode *root, char *input)
{
		if (root == NULL)
		{
				bnode *newNode = malloc(sizeof(bnode));
				newNode -> word = malloc(sizeof(char) * strlen(input) + 1);
				strcpy(newNode -> word, input);
				newNode -> freq = 1;
				newNode -> left = NULL;
				newNode -> right = NULL;

				return(newNode);
		}

		match = 0;
		match = strCheck(root, input);

		if (match == 1)
				return(root);

		bnode *parent = root;
		bnode *ptr = root -> left;
		while (ptr != NULL && ptr -> left != NULL)
		{
				if (ptr -> right == NULL)
						break;
				parent = parent -> left;
				ptr = ptr -> left;
		}
		if (ptr == NULL)
				ptr = parent;

		bnode *newNode = malloc(sizeof(bnode));
		newNode -> word = malloc(sizeof(char) * (strlen(input) + 1));
		strcpy(newNode -> word, input);
		newNode -> freq = 1;
		newNode -> left = NULL;
		newNode -> right = NULL;

		if (ptr -> left == NULL)
				ptr -> left = newNode;
		else if (ptr -> right == NULL)
		{
				ptr = rotateLeft(ptr);
				ptr -> right = ptr -> left;
				ptr -> left = newNode;
				if (parent -> left != NULL)
						parent -> left = ptr;
				if (ptr -> right == root)
						root = ptr;
		}

		root = rotateTree(root);
		return(root);
}
Esempio n. 21
0
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);
        }
    }
}
Esempio n. 22
0
Node* rotate(Node* gr, Node* par, Node* ch)
{
    if (par->left == ch) 
    {
        return rotateRight(gr, par, ch);
    } 
    else 
    {
        return rotateLeft(gr, par, ch);
    }
}
Esempio n. 23
0
bool Block::rotate(int direction) {
	switch (direction) {
	case DIRECTION_LEFT:
		return rotateLeft();
		break;
	case DIRECTION_RIGHT:
		return rotateRight();
		break;
	}
	return false;
}
Esempio n. 24
0
static spltreeNode_t* delete_max_rc(spltreeNode_t* splnode, spltreeNode_t** max) {
	if(!splnode->right){
		*max = splnode;
		return splnode->left;
	}
	splnode->right = delete_max_rc(splnode->right,max);
	if(!splnode->right) {
		return splnode;
	}
	return rotateLeft(splnode);
}
Esempio n. 25
0
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);
}
Esempio n. 26
0
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
Esempio n. 27
0
// testing the functions
int main(){
    
    unsigned int swapMe = 0x12345678;
    swapMe = swap(swapMe);
    
    unsigned int rotateMe = 0x88888888;
    rotateMe = rotateLeft(rotateMe);
    
    printf("swapped: %x\nrotated: %x\n", swapMe, rotateMe);
    
    return 0;
}
void printPermutations(char* s, int pos){
	int len;
	if(pos >= (len=strlen(s))-1){
		puts(s);
		return;
	}
	int i;
	for(i=0; i< len-pos; i++){
		rotateLeft(s, pos);
		printPermutations(s, pos+1);
	}
}
Esempio n. 29
0
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;
}
Esempio n. 30
0
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 ;
}