Beispiel #1
0
/*Faz a insercao a direita na AVL*/
AVL insertRight(AVL p, void* info, int *cresceu,int tipo_AVL) {
	p->right=insert(p->right, info, cresceu,tipo_AVL);
	if(*cresceu) {
		switch(p->bf) {
		case LH:
			p->bf=EH;
			*cresceu=0;
			break;
		case EH:
			p->bf=RH;
			*cresceu=1;
			break;
		case RH:
			p=balanceRight(p);
			*cresceu=0;
		}
	}
	return p;
}
Beispiel #2
0
Tree insertRight(Tree t, TreeEntry e, int *cresceu) {

  t -> right = insertTree(t -> right, e, cresceu);

  if(*cresceu) {
    switch(t->bf) {
      case LH:
        t -> bf = EH;
        *cresceu = 0;
        break;
      case EH:
        t -> bf = RH;
        *cresceu = 1;
        break;
      case RH:
        t = balanceRight(t);
        *cresceu = 0;
    }
  }

  return t;
}
Beispiel #3
0
Tree deleteLeft(Tree t, TreeEntry e, int *diminuiu) {

  t -> left = deleteTree(t -> left, e, diminuiu);

  if(*diminuiu) {
    switch(t->bf) {
      case LH:
        t -> bf = EH;
        *diminuiu = 1;
        break;
      case EH:
        t -> bf = RH;
        *diminuiu = 0;
        break;
      case RH:
        printf("[bR]\n");
        t = balanceRight(t);
        *diminuiu = 1;
    }
  }

  return t;
}
Beispiel #4
0
avlTreeNode* insertNode(avlTreeNode* root , int key )
{
  if( !root )
    {
      printf("\nIn leaf Insertion\n");
      root = creatNode( key );
      return root;
    }
  
  if( root -> data == key )
    {
      printf("\nDuplicate Key Not permitted\n");
      return root;
    }
  
  else if( root -> data > key )
    {
      printf("\nBefore Left Insertion\n");
      root -> leftTree = insertNode( root -> leftTree , key ) ;
      printf("\nAfter Left Insertion\n");

      int balanceDifference;

      balanceDifference = calculateMaxHight( root );

      switch (balanceDifference) 
	{
	  
	case -15:
	  {
	    printf("\nDon have left and right child\n");
	    root -> leftTree -> height = 1;
	    root -> leftTree -> balanceFactor = EQ;
	    break;
	  }
	  

	case 0:
	  {
	    printf("\nBoth equal hight subtrees\n");
	    root -> height = 1 + ( root-> leftTree -> height ) ;
	    root -> balanceFactor = EQ ;
	    break;
	  }

	case 1:
	  {
	    /*	    printf("\nright subtree highted\n");*/
	    root -> height = 1 + (root -> leftTree -> height) ;
	    root -> balanceFactor = LH ;
	    break;
	  }
	
	case 2:
	  {
	    printf("\nLeft subtree highted calling balanceLeft\n");
	    root  = balanceLeft( root );
	    break;
	  }
	} 
	
      return root;
    }

  else
    {
      printf("\nBefore Rightt Insertion\n");
      root -> rightTree = insertNode( root -> rightTree , key ) ;
      printf("\nAfter Right Insertion\n");

      int balanceDifference;

      balanceDifference = calculateMaxHight( root );

      switch (balanceDifference) 
	{
	  
	case -15:
	  {
	    printf("\nDon have left and right child\n");
	    root -> rightTree -> height = 1;
	    root -> rightTree -> balanceFactor = EQ;
	    break;
	  }
	  

	case 0:
	  {
	    printf("\nBoth equal hight subtrees\n");
	    root -> height = 1 + ( root -> rightTree -> height ) ;
	    root -> balanceFactor = EQ ;
	    break;
	  }

	case -2:
	  {
	    printf("\nRight highted, calling balanceRight\n");
	    root  = balanceRight( root );
	    break;
	  }

	case -1:
	  {
	    /*	    printf("\nLeft subtree hightedt\n");*/
	    root -> height = 1 + ( root -> rightTree -> height );
	    root -> balanceFactor = RH ;
	    break;
	  }
	} 

      return root;
    }

  return root;
}
struct node * deleteNode(struct node * root, char * word, int * found)
{
    int match;
    struct node *p;

    if (root == NULL)
    {
        return NULL;
    }
    else
    {
        match = strcmp(word, root->word);

    }
    /*If the word is found, found is set to true. If the frequency of the word is greated than 1, it is decreased by 1. Else, the node must be deleted. */
    if(match == 0)
    {
        (*found) = 1;
        if(root->frequency > 1)
        {
            root->frequency--;
        }
        else
        {
            /*If there is a right child, the tree must be balanced to maintain its integrity. The containing variable is set to the right child, unless the right child has a left child. The node to be deleted then swaps with the containing variable. A recursive call is then made with the right child. Afterwards the balanceRight function is called*/
            if(root->right !=NULL)
            {
                p = root->right;
                if(p->left != NULL)
                {
                    p = p->left;
                }
                root->word = p->word;
                root->frequency = p->frequency;
                root->right = deleteNode(root->right, p->word, found);
                if((height(root->left) - height(root->right)) == 2)
                {
                    balanceRight(root, word);
                }
            }
            else
            {
                return(root->left);
            }
        }
    }
    else
    {
        /*Recursive calls are made while the word isn't matched*/
        if(match > 0)
        {
            root->right = deleteNode(root->right, word, found);

        }
        if(match < 0)
        {
            root->left = deleteNode(root->left, word, found);
        }
    }
    
    
    root->height = height(root);
    return root;
}