Пример #1
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);
}
Пример #2
0
treeNode *reBalance(treeNode *nodeN) {
	if (findHeight(nodeN->left) - findHeight(nodeN->right) > 1) {
		if (findHeight(nodeN->left->left) > findHeight(nodeN->left->right)) {
			nodeN = rotateRight(nodeN);
		} else {
			nodeN = rotateLeftRight(nodeN);
		}
	} else {
		if (findHeight(nodeN->right) - findHeight(nodeN->left) > 1) {
			if (findHeight(nodeN->right->right)
					> findHeight(nodeN->right->left)) {
				nodeN = rotateLeft(nodeN);
			} else {
				nodeN = rotateRightLeft(nodeN);
			} /* else node is balanced  */
		}
	}
	return nodeN;
}
Пример #3
0
void AVLTree<K, V>::insert(Node* & subtree, const K & key, const V & value)
{
    // your code here
	
	// position found
	if(subtree == NULL)
	{
		subtree = new Node(key, value);
		return;
	}

	// duplicate key
	if(key == subtree->key)
	{
		return;
	}

	// insert left
	if(key < subtree->key)
	{
		// insert to the left recursively
		insert(subtree->left, key, value);
		// check balance
		int balance = heightOrNeg1(subtree->right) - heightOrNeg1(subtree->left);
		int left_balance = heightOrNeg1(subtree->left->right) - heightOrNeg1(subtree->left->left);
		if(balance == -2)
		{
			if(left_balance == -1)
			{
				rotateRight(subtree);
			}
  			else
			{
				rotateLeftRight(subtree);
			}
		}
		subtree->height = 1+max( heightOrNeg1(subtree->left), heightOrNeg1(subtree->right) );
		return;
	}
	
	// insert right
	if(key > subtree->key)
	{
		// insert to the right recursively
		insert(subtree->right, key, value);
		// check balance
		int balance = heightOrNeg1(subtree->right) - heightOrNeg1(subtree->left);
		int right_balance = heightOrNeg1(subtree->right->right) - heightOrNeg1(subtree->right->left);
		if(balance == 2)
		{
			if(right_balance == 1)
			{
				rotateLeft(subtree);
			}
			else
			{
				rotateRightLeft(subtree);
			}
		}
		subtree->height = 1+max( heightOrNeg1(subtree->left), heightOrNeg1(subtree->right) );
	}
}