void AVL_tree::Balance(Node& node)
{
  if(BalanceFactor(node) == 2) // Unbalance on right side
    {
      if(BalanceFactor(node->right) < 0) // If left chain longer
	RotateLeft(node->right);
      RotateRight(node);
    }
  if(BalanceFactor(node) == -2)
    {
      if(BalanceFactor(node->left) > 0)
	RotateRight(node->left);
      RotateLeft(node);
    }
}
Beispiel #2
0
	static void Balance(pNode& p)
	{
		if (!p)
			return;
		FixHeight(p);
		if (BalanceFactor(p) == 2)
		{
			if (BalanceFactor(p->Right) < 0)
				RotateRight(p->Right);
			RotateLeft(p);
		}

		if (BalanceFactor(p) == -2)
		{
			if (BalanceFactor(p->Left) > 0)
				RotateLeft(p->Left);
			RotateRight(p);
		}
	}
TNode* Insert(TNode* root,int data){

if(!root){
return NewNode(data);
}

if(root->data>data){
root->left=Insert(root->left,data);
}else{
root->right=Insert(root->right,data);
}

root->height=max(Height(root->left),Height(root->right))+1;

int bf=BalanceFactor(root);

if(bf>1){

if(data<root->left->data){
return RightRotate(root);
}else if(data>=root->left->data){
root->left=LeftRotate(root->left);
return RightRotate(root);
}

}

if(bf<-1){

if(data>root->right->data){
return LeftRotate(root);
}else if(data<root->right->data){
root->right=RightRotate(root->right);
return LeftRotate(root);
}
}

return root;
}
Beispiel #4
0
NodeT* Insert(NodeT* Node, int data)
{
    int b,r,l;
    if(Node==NULL) return createNode(data);
    if(data<Node->data) Node->left=Insert(Node->left,data);
    else Node->right=Insert(Node->right, data);
    l=NodeHeight(Node->left);
    r=NodeHeight(Node->right);
    Node->height=Max(l,r);
    b=BalanceFactor(Node);
    if(b<-1 && data>Node->right->data) return LeftRotation(Node);
    if(b>1 && data<Node->left->data) return RightRotation(Node);
    if(b<-1 && data<Node->right->data)
        {
        Node->right=RightRotation(Node->right);
        return LeftRotation(Node);
        }
    if(b>1 && data>Node->left->data)
        {
        Node->left=LeftRotation(Node->left);
        return RightRotation(Node);
        }
    return Node;
}
Beispiel #5
0
Datei: avl.c Projekt: jarun/dslib
/*
 * Delete a node from AVL tree
 * Recursive method
 */
bool delete_avl_node(avl_pp head, int val)
{
	avl_p node;
	avl_p tmp;

	if (!head) {
		log(ERROR, "Initialize AVL tree first\n");
		return FALSE;
	}

	node = *head;
	if (!node) {
		log(ERROR, "No nodes to delete\n");
		return FALSE;
	}

	if (val > node->data) {
		if (!node->right)
			return FALSE;

		if (delete_avl_node(&(node->right), val) == FALSE)
			return FALSE;

		if (BalanceFactor(node) == 2) {
			if (BalanceFactor(node->left) >= 0)
				node = LeftLeft(node);
			else
				node = LeftRight(node);
		}
	} else if (val < node->data) {
		if (!node->left)
			return FALSE;

		if (delete_avl_node(&(node->left), val) == FALSE)
			return FALSE;

		if (BalanceFactor(node) == -2) {
			if (BalanceFactor(node->right) <= 0)
				node = RightRight(node);
			else
				node = RightLeft(node);
		}
	} else { /* Match found */
		if (node->right) {  /* Delete the inorder successor */
			tmp = node->right;
			while (tmp->left)
				tmp = tmp->left;

			node->data = tmp->data;
			if (delete_avl_node(&(node->right), tmp->data) == FALSE)
				return FALSE;

			if (BalanceFactor(node) == 2) {
				if (BalanceFactor(node->left) >= 0)
					node = LeftLeft(node);
				else
					node = LeftRight(node);
			}
		} else {
			*head = node->left;
			return TRUE;
		}
	}

	node->height = height(node);
	*head = node;
	return TRUE;
}
Beispiel #6
0
Datei: avl.c Projekt: jarun/dslib
/*
 * Rebalance subtree tmp based on balance factor & skew
 */
bool rebalance(stack_p stack, avl_pp head, avl_p tmp, int data)
{
	nodedata_p p = NULL;
	int direction;
	avl_p parent = NULL;
	bool modified = TRUE;

	if (BalanceFactor(tmp) == -2) { /* Right subtree longer */
		p = pop(stack);
		if (p) {
			parent = p->node;
			direction = p->direction;
		}

		if (data >= tmp->right->data) { /* Right-right skewed subtree */
			if (p)
				direction == RIGHT
					?  (parent->right = RightRight(tmp))
					: (parent->left = RightRight(tmp));
			else /* If p is NULL, this is the topmost node, update *head */
				*head = RightRight(tmp);
		} else { /* Right-left skewed subtree */
			if (p)
				direction == RIGHT
					? (parent->right = RightLeft(tmp))
					: (parent->left = RightLeft(tmp));
			else
				*head = RightLeft(tmp);
		}
	} else if (BalanceFactor(tmp) == 2) { /* Left subtree longer */
		p = pop(stack);
		if (p) {
			parent = p->node;
			direction = p->direction;
		}
		/* If p is NULL, this is the topmost node, update *head */

		if (data < tmp->left->data) { /* Left-left skewed subtree */
			if (p)
				direction == RIGHT
					? (parent->right = LeftLeft(tmp))
					: (parent->left = LeftLeft(tmp));
			else
				*head = LeftLeft(tmp);
		} else { /* Left-right skewed subtree */
			if (p)
				direction == RIGHT
					? (parent->right = LeftRight(tmp))
					: (parent->left = LeftRight(tmp));
			else
				*head = LeftRight(tmp);
		}
	} else
		modified = FALSE;

	if (p)
		free(p);

	tmp->height = height(tmp);

	return modified;
}