Exemple #1
0
node* balanceNode(node* nod) {
	node* newroot = NULL;
	
	int bf = 0;

	if(nod->left)
		nod->left = balanceNode(nod->left);
	if(nod->right)
		nod->right= balanceNode(nod->right);

	bf = balanceFactor(nod);

	if(bf >= 2) {
		if(balanceFactor(nod->left) <= -1 )
			newroot = rotateLR(nod);
		else
			newroot = rotateLL(nod);
	}else if(bf <= -2) {
		if(balanceFactor(nod->right)<= 1)
			newroot = rotateRL(nod);
		else
			newroot = rotateRR(nod);
	}else
		newroot = nod;

	return(newroot);
}
Exemple #2
0
Tree * balance (Tree * tree) {
    if (tree -> nil)
        return tree;

    Balance balanced = balanceCase(tree);

    switch (balanced) {
        case Balanced:
            tree -> left = balance(tree -> left);
            tree -> right = balance(tree -> right);
            return tree;
        case LL:
            ll++;
            tree -> left = balance(tree -> left);
            tree -> right = balance(tree -> right);
            return rotateLL(tree);
        case LR:
            lr++;
            tree -> left = balance(tree -> left);
            tree -> right = balance(tree -> right);
            return rotateLR(tree);
        case RL:
            rl++;
            tree -> left = balance(tree -> left);
            tree -> right = balance(tree -> right);
            return rotateRL(tree);
        case RR:
            rr++;
            tree -> left = balance(tree -> left);
            tree -> right = balance(tree -> right);
            return rotateRR(tree);
    }
}
Exemple #3
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
Exemple #4
0
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
void balanceCheck(struct node *root){
     if ( getHeight(root->left) > (getHeight(root->right) + 1) ){
        printf("Imbalance! The Right sub-tree of node %d is taller!\n",root->value);
        if ( getHeight(root->left->right) > (getHeight(root->left->left)) ){
           printf("Rotating using LR Rotation...\n");
           rotateLR(root);
        } else {
          printf("Rotating using LL Rotation...\n");
          rotateLL(root);
        }

     } else if ( getHeight(root->right) > (getHeight(root->left) + 1) ){
        printf("Imbalance! The Right sub-tree of node %d is taller!\n",root->value);

        if ( getHeight(root->right->left) > (getHeight(root->right->right)) ){
           printf("Rotating using RL Rotation...\n");
           rotateRL(root);
        } else {
           printf("Rotating using RR Rotation...\n");
           rotateRR(root);
        }
     }
}