Esempio n. 1
0
static TLDNode *balance(TLDNode *node)
{
    int diff;
    diff = getDiff(node);
    if (diff > 1)
    {
        if (getDiff(node->left) > 0)
        {
            node = leftLeftRotation(node);
        }
        else
        {
            node = leftRightRotation(node);
        }
    }
    else if (diff < -1)
    {
        if (getDiff(node->right) > 0)
        {
            node = rightLeftRotation(node);
        }
        else
        {
            node = rightRightRotation(node);
        }
    }
    return node;
}
Esempio n. 2
0
nodeT* insertNew(nodeT*root, int value)
{
    ///make the actual insertion
    if (root==NULL) return createNode(value);
    if (value<root->data)
        root->left=insertNew(root->left,value);
    else
        if (value>root->data)
        root->right=insertNew(root->right,value);
    ///update height
    root->height=max(height(root->left),height(root->right))+1;
    ///calculating balance factor
    int bF=balanceFactor(root);
    ///make the rotation, if necessary
    if (bF==-2)
    {
        if (root->right->data<value) singleLeftRotation(&root);
        if (root->right->data>value) rightLeftRotation(&root);
    }
    else
    {
        if (bF==2)
        {
            if (root->left->data>value) singleRightRotation(&root);
            if (root->left->data<value) leftRightRotation(&root);
        }
    }
    return root;
}
bool AVLTree<T>::addNode(T k, T val)
{
	bool isSuccess = BSTree<T>::addNode(k, val);
	if (isSuccess)
	{
		NodePointer ptr = searchNode(k);
		if (ptr != root)
		{
			NodePointer preParent = ptr->parent;
			if (preParent != root)
			{
				NodePointer prePreParent = preParent->parent;
				if (prePreParent == root)
				{
					if (getHeight(prePreParent->left) - getHeight(prePreParent->right) == 2)
					{
						if (ptr == preParent->left)
						{
							root = leftLeftRotation(prePreParent);
						}
						else
						{
							root = leftRightRotation(prePreParent);
						}
					}
					else if (getHeight(prePreParent->right) - getHeight(prePreParent->left) == 2)
					{
						if (ptr == preParent->left)
						{
							root = rightLeftRotation(prePreParent);
						}
						else
						{
							root = rightRightRotation(prePreParent);
						}
					}
				}
				else
				{
					NodePointer prePrePreParent = prePreParent->parent;
					if (getHeight(prePreParent->left) - getHeight(prePreParent->right) == 2)
					{
						if (prePreParent == prePrePreParent->left)
						{
							if (ptr == preParent->left)
							{
								prePrePreParent->left = leftLeftRotation(prePreParent);
							}
							else
							{
								prePrePreParent->left = leftRightRotation(prePreParent);
							}
						}
						else
						{
							if (ptr == preParent->left)
							{
								prePrePreParent->right = leftLeftRotation(prePreParent);
							}
							else
							{
								prePrePreParent->right = leftRightRotation(prePreParent);
							}
						}
					}
					else if (getHeight(prePreParent->right) - getHeight(prePreParent->left) == 2)
					{
						if (prePreParent == prePrePreParent->left)
						{
							if (ptr == preParent->left)
							{
								prePrePreParent->left = rightLeftRotation(prePreParent);
							}
							else
							{
								prePrePreParent->left = rightRightRotation(prePreParent);
							}
						}
						else
						{
							if (ptr == preParent->left)
							{
								prePrePreParent->right = rightLeftRotation(prePreParent);
							}
							else
							{
								prePrePreParent->right = rightRightRotation(prePreParent);
							}
						}
					}
				}
			}
		}
	}

	return isSuccess;
}
bool AVLTree<T>::delNode(T k)
{
	bool isSuccess = true;
	NodePointer ptr = searchNode(k);
	NodePointer preParent = NULL;
	if (ptr != NULL)
	{
		if (ptr == root)
		{
			BSTree<T>::delNode(k);
			// only left left exist
			if (getHeight(root->left) - getHeight(root->right) == 2)
			{
				root = leftLeftRotation(root);
			}
		}
		else
		{
			NodePointer lNode = NULL, rNode = NULL;
			preParent = ptr->parent;
			if (preParent == root)
			{
				BSTree<T>::delNode(k);
				lNode = root->left;
				rNode = root->right;
				if (getHeight(preParent->left) - getHeight(preParent->right) == 2)
				{
					if (getHeight(lNode->left) >= getHeight(lNode->right))
					{
						root = leftLeftRotation(preParent);
					}
					else
					{
						root = leftRightRotation(preParent);
					}
				}
				else if (getHeight(preParent->right) - getHeight(preParent->left) == 2)
				{
					if (getHeight(rNode->right) >= getHeight(rNode->left))
					{
						root = rightRightRotation(preParent);
					}
					else
					{
						root = rightLeftRotation(preParent);
					}
				}
			}
			else
			{
				NodePointer prePreParent = NULL;
				NodePointer lNode = NULL, rNode = NULL;
				BSTree<T>::delNode(k);
				// 这里需要递归查找不平衡点,以防止高层结点不平衡而低层结点平衡的情况
				NodePointer tmpPtr = findImbalanceNode(preParent);
				if (tmpPtr != NULL)
				{
					if (tmpPtr == root)
					{
						lNode = tmpPtr->left;
						rNode = tmpPtr->right;
						if (getHeight(tmpPtr->left) - getHeight(tmpPtr->right) == 2)
						{
							if (getHeight(lNode->left) >= getHeight(lNode->right))
							{
								root = leftLeftRotation(tmpPtr);
							}
							else
							{
								root = leftRightRotation(tmpPtr);
							}
						}
						else if (getHeight(tmpPtr->right) - getHeight(tmpPtr->left) == 2)
						{
							if (getHeight(rNode->right) >= getHeight(rNode->left))
							{
								root = rightRightRotation(tmpPtr);
							}
							else
							{
								root = rightLeftRotation(tmpPtr);
							}
						}
					}
					else
					{
						prePreParent = tmpPtr->parent;
						lNode = tmpPtr->left;
						rNode = tmpPtr->right;
						if (getHeight(tmpPtr->left) - getHeight(tmpPtr->right) == 2)
						{
							if (tmpPtr == prePreParent->left)
							{
								if (getHeight(lNode->left) >= getHeight(lNode->right))
								{
									prePreParent->left = leftLeftRotation(tmpPtr);
								}
								else
								{
									prePreParent->left = leftRightRotation(tmpPtr);
								}
							}
							else
							{
								if (getHeight(lNode->left) >= getHeight(lNode->right))
								{
									prePreParent->right = leftLeftRotation(tmpPtr);
								}
								else
								{
									prePreParent->right = leftRightRotation(tmpPtr);
								}
							}
						}
						else if (getHeight(tmpPtr->right) - getHeight(tmpPtr->left) == 2)
						{
							if (tmpPtr == prePreParent->left)
							{
								if (getHeight(rNode->right) >= getHeight(rNode->left))
								{
									prePreParent->left = rightRightRotation(tmpPtr);
								}
								else
								{
									prePreParent->left = rightLeftRotation(tmpPtr);
								}
							}
							else
							{
								if (getHeight(rNode->right) >= getHeight(rNode->left))
								{
									prePreParent->right = rightRightRotation(tmpPtr);
								}
								else
								{
									prePreParent->right = rightLeftRotation(tmpPtr);
								}
							}
						}
					}
				}
			}
		}
	}
	else
	{
		isSuccess = false;
	}

	return isSuccess;
}