Esempio n. 1
0
avlTreeNode* balanceLeft(avlTreeNode* root)
{
  int balanceDifference;
  
  balanceDifference = calculateMaxHight( root-> leftTree );
  printf("\nIn balanceLeft Insertion\n");

  switch ( balanceDifference )
    {

    case 1: case 0:
      {
	printf("\nIn RightRotation Insertion\n");
	return rightRotation( root );
      }

    case -1:
      {
	printf("\nIn LeftRotation Insertion\n");
	root -> leftTree = leftRotation( root -> leftTree );
	printf("\nIn RightRotation Insertion\n");
	return rightRotation( root );
      }

    }

  return root;
}
Esempio n. 2
0
NodeT *Balance(NodeT *root)
{
        int balance = getBalance(root);

        if(balance == -2)
        {
            balance = getBalance(root->left);
            if(balance >=1)
            {
                root -> left = leftRotation(root->left);
                root = rightRotation(root);
            }
            else
                root = rightRotation(root);
        }
        else
        {
            if(balance == 2)
            {
                balance = getBalance(root);
                if(balance <= -1)
                {
                    root -> right = rightRotation(root->right);
                    root = leftRotation(root);
                }
                else
                    root = leftRotation(root);
            }
        }

    getHeights(root);

    return root;
}
// Worst case insertion O(2*logn)
struct Node *insert(struct Node *root,int data)
{
	if(root == NULL)
		return create(data);
	if(data > root->data)
		root->right = insert(root->right,data);
	else
		root->left  = insert(root->left,data);
	root->height = max(height(root->left),height(root->right)) + 1;
	int balance = getbalance(root);
	// left left case
	if(balance >  1 && data < root->left->data)
		return rightRotation(root);
	else // left right case
	if(balance > 1 && data > root->left->data)
	{
		root->left = leftRotation(root->left);
		return rightRotation(root);
	}
	else // right-right case
	if(balance < -1 && data > root->right->data)
	{
		return leftRotation(root);
	}
	else // right left case;
	if(balance < -1 && data < root->right->data)
	{
		root->right = rightRotation(root->right);
		return leftRotation(root);
	}
	// no change required;
	return root;

}
Esempio n. 4
0
avlTreeNode* balanceRight( avlTreeNode* root )
{
  int balanceDifference;

  printf("\nIn BalnaceRight Insertion\n");

  balanceDifference = calculateMaxHight( root-> rightTree );
  
  printf("========== %d\n",balanceDifference);
  switch ( balanceDifference )
    {
      
    case 1:
      {
	printf("\nIn RightRotation Insertion\n");
	root -> rightTree = rightRotation( root -> rightTree );
	printf("\nIn leftRotation Insertion\n");	
	return leftRotation( root );
      }

    case -1:  case 0 :
      {
	return leftRotation( root );
      }

    }

  return root;
}
Esempio n. 5
0
NodeT* insertNodeAVLtree(NodeT* root, int data)
{
    if(root==NULL)
        return createNodeT(data);
    else
    {
        if(root->data < data)
            root->right=insertNodeAVLtree(root->right, data);
        else
            root->left=insertNodeAVLtree(root->left, data);
    }

    root->height=maxx(height(root->left), height(root->right))+1;
    int aux=balanceFactor(root);

    if(aux>1)
    {
        if(root->left->data > data)//RR
            rightRotation(&root);
        else//LR
            LeftRight(&root);
    }
    else if(aux<-1)
    {
        if(root->right->data < data)//LL
            leftRotation(&root);
        else//RL
            RightLeft(&root);
    }
    //else
    return root;
}
Esempio n. 6
0
TREE* insert(char * name, TREE * t){
    addNum++;
	if(t == NULL){
		t = (TREE*)malloc(sizeof(TREE));
		t -> name = name;
		t -> height = 0;
		t -> ls = t -> rs = NULL;
		return t;
	}
	else if(strcmp(name, t -> name) <= 0){
		t -> ls = insert(name, t -> ls);
		if(height(t -> ls) - height(t -> rs) == 2 ){
			if(strcmp(name, t -> ls -> name) <= 0)
				t = leftRotation(t);
			else
				t = doubleLeftRotation(t);
		}
	}
	else if(strcmp(name, t -> name) > 0){
		t -> rs = insert(name, t -> rs);
		if(height(t -> rs) - height(t -> ls) == 2){
			if(strcmp(name, t -> rs -> name) > 0)
				t = rightRotation(t);
			else
				t = doubleRightRotation(t);
		}
	}
	t->height = MAX(height(t -> ls), height(t -> rs)) + 1;
	return t;
}
Esempio n. 7
0
File: E.c Progetto: jpbat/aed2011
nodePtr splay(char* word, nodePtr leaf)
{
	if(leaf->left != NULL && strcmp(leaf->left->info.word, word)==0)
		return rightRotation(leaf);

	else if (leaf->right != NULL && strcmp(leaf->right->info.word, word)==0)
		return leftRotation(leaf);
	
	return leaf;
}
Esempio n. 8
0
void BinarySearchTree::twoRotations(Node *TopNode, Node *SubNode, bool direction) {
    if(direction == LINKED_LEFT) {
        // If top node was linked left then rotate to the right 
        // and link to balanced tree
        TopNode->assignNewRightNode(rightRotation(SubNode));
        rotateAndLink(TopNode, LINKED_LEFT);
    } else {
        TopNode->assignNewLeftNode(leftRotation(SubNode));
        rotateAndLink(TopNode, LINKED_RIGHT);
    }
}
Esempio n. 9
0
void BinarySearchTree::rotateAndLink(Node* TopNode, bool direction) {
    // Link the balanced subtree back to the Binary Search Tree
    if(direction == LINKED_LEFT) {
        if(TopNode == RootNode)
            RootNode = leftRotation(TopNode);
        else {
            Node *TreeNodeLink = TopNode->getHigherNodePtr();
            if(TopNode->getHigherNodeDir() == LINKED_LEFT) 
                TreeNodeLink->assignNewLeftNode(leftRotation(TopNode));
            else
                TreeNodeLink->assignNewRightNode(leftRotation(TopNode));
        }
    } else {
        if(TopNode == RootNode)
            RootNode = rightRotation(TopNode);
        else {
            Node *TreeNodeLink = TopNode->getHigherNodePtr();
            if(TopNode->getHigherNodeDir() == LINKED_LEFT) 
                TreeNodeLink->assignNewLeftNode(rightRotation(TopNode));
            else
                TreeNodeLink->assignNewRightNode(rightRotation(TopNode));
        }
    }
}
Esempio n. 10
0
File: F.c Progetto: jpbat/aed2011
nodePtr rootInsertion(char* word, nodePtr leaf)
{
	if(leaf == NULL)
		return createNode(word);

	if(strcmp(word,leaf->info.word ) < 0)
	{
		leaf->left = rootInsertion(word, leaf->left);
		leaf = rightRotation(leaf);
	}
	
	else if((strcmp(word,leaf->info.word) > 0))
	{
		leaf->right = rootInsertion(word,leaf->right);
		leaf = leftRotation(leaf);
	}
	
	return leaf;
}
Esempio n. 11
0
void avlInsert(treePointer *parent, element x, int *unbalanced)
{
    if (!*parent) {
        *unbalanced = TRUE;
        *parent = malloc(sizeof(Node));
        (*parent)->leftChild = (*parent)->rightChild = NULL;
        (*parent)->bf = 0;
        (*parent)->data = x;
    } else if (x.key < (*parent)->data.key) {
        avlInsert(&(*parent)->leftChild, x, unbalanced);
        if (*unbalanced)
            switch ((*parent)->bf) {
            case -1:
                (*parent)->bf = 0;
                *unbalanced = FALSE;
                break;
            case 0:
                (*parent)->bf = 1;
                break;
            case 1:
                leftRotation(parent, unbalanced);
            }
    } else if (x.key > (*parent)->data.key) {
        avlInsert(&((*parent)->rightChild), x, unbalanced);
        if (*unbalanced)
            switch ((*parent)->bf) {
            case 1:
                (*parent)->bf = 0;
                *unbalanced = FALSE;
                break;
            case 0:
                (*parent)->bf = -1;
                break;
            case -1:
                rightRotation(parent, unbalanced);
            }
    } else {
        *unbalanced = FALSE;
        printf("The key is already in the tree");
    }
}
Esempio n. 12
0
File: avl2.c Progetto: Surtr04/misc
Tree* checkBalance(Tree *t) {
    
    int h;
    
    if(t->left != NULL && t->right != NULL)        
        h = height(t);
        
    if(height(t->left) - height(t->right) > 1) {
        if (height(t->left->left) - height(t->left->right) > 0) 
            t = rightRotation(t);
        else   
            t = leftAndRightRotation(t);
    }
    else  
        if(height(t->left) - height(t->right) < -1) {
            if (height(t->right->left) - height(t->right->right) < 0)
                t = leftRotation(t);
            else
                t = rightAndLeftRotation(t);
        }                            
    return t;
}
Esempio n. 13
0
void avlInsert(treePointer *parent,element x,int *unbalanced)
{
  void leftRotation(treePointer *parent,int *unbalanced);
  void rightRotation(treePointer *parent,int *unbalanced);

  if(!*parent)  {  // insert element into null tree
    *unbalanced=TRUE;
    MALLOC(*parent,sizeof(treePointer));
    (*parent)->leftChild=(*parent)->rightChild=NULL;
    (*parent)->bf=0;
    (*parent)->data=x;
  }
  else if(x.key<(*parent)->data.key)  {
    avlInsert(&(*parent)->leftChild,x,unbalanced);
    if(*unbalanced)
    // left branch has grown higher
      switch((*parent)->bf)  {
        case -1: (*parent)->bf=0;
                 *unbalanced=FALSE; break;
        case 0: (*parent)->bf=1; break;
        case 1: leftRotation(parent,unbalanced);  
      }
  }
  else if(x.key>(*parent)->data.key)  {
    avlInsert(&(*parent)->rightChild,x,unbalanced);
    if(*unbalanced)
    // right branch has grown higher
      switch((*parent)->bf)  {
        case 1: (*parent)->bf=0;
                 *unbalanced=FALSE; break;
        case 0: (*parent)->bf=-1; break;
        case -1: rightRotation(parent,unbalanced);
      }
  }
  else  {
    *unbalanced=FALSE;
    printf("The key is already in the tree\n");
  }
}
Esempio n. 14
0
void rightLeftRotation(NodeT **root3)
{
    rightRotation(&((*root3)->right));
    leftRotation(root3);
}
Esempio n. 15
0
void leftRightRotation(NodeT **root3)
{
    leftRotation(&((*root3)->left));
    rightRotation(root3);
}
Esempio n. 16
0
void RightLeft(NodeT** root)
{
    rightRotation(& ((*root)->right));
    leftRotation(root);
}
Esempio n. 17
0
void LeftRight(NodeT** root)
{
    leftRotation(& ((*root)->left));
    rightRotation(root);
}
Esempio n. 18
0
TREE* doubleRightRotation(TREE * k1){
	k1 -> rs = leftRotation(k1 -> rs);
	return rightRotation(k1);
}
Esempio n. 19
0
File: avl2.c Progetto: Surtr04/misc
Tree* rightAndLeftRotation(Tree *t) {
    t = rightRotation(t);
    return leftRotation(t);
}
Esempio n. 20
0
File: avl2.c Progetto: Surtr04/misc
Tree* leftAndRightRotation(Tree *t) {
    t = leftRotation(t);
    return rightRotation(t);
}
Esempio n. 21
0
TREE* doubleLeftRotation(TREE * k3){
	k3 -> ls = rightRotation(k3 -> ls);
	return leftRotation(k3);
}