예제 #1
0
파일: chkAvl.c 프로젝트: iamon3/C_DFS
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;
}
예제 #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;
}
예제 #3
0
// 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;

}
예제 #4
0
파일: chkAvl.c 프로젝트: iamon3/C_DFS
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;
}
예제 #5
0
파일: main.c 프로젝트: ISimion/DSA-lab
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;
}
예제 #6
0
파일: AVL_Strom.c 프로젝트: G43riko/ZPRP
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;
}
예제 #7
0
파일: E.c 프로젝트: 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;
}
예제 #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);
    }
}
예제 #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));
        }
    }
}
예제 #10
0
파일: F.c 프로젝트: 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;
}
예제 #11
0
파일: DS-AVL.c 프로젝트: cmkuoster/DS-AVL
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");
    }
}
예제 #12
0
파일: avl2.c 프로젝트: 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;
}
예제 #13
0
파일: avl.c 프로젝트: scienter/voronoi
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");
  }
}
예제 #14
0
void rightLeftRotation(NodeT **root3)
{
    rightRotation(&((*root3)->right));
    leftRotation(root3);
}
예제 #15
0
void leftRightRotation(NodeT **root3)
{
    leftRotation(&((*root3)->left));
    rightRotation(root3);
}
예제 #16
0
파일: main.c 프로젝트: ISimion/DSA-lab
void RightLeft(NodeT** root)
{
    rightRotation(& ((*root)->right));
    leftRotation(root);
}
예제 #17
0
파일: main.c 프로젝트: ISimion/DSA-lab
void LeftRight(NodeT** root)
{
    leftRotation(& ((*root)->left));
    rightRotation(root);
}
예제 #18
0
파일: AVL_Strom.c 프로젝트: G43riko/ZPRP
TREE* doubleRightRotation(TREE * k1){
	k1 -> rs = leftRotation(k1 -> rs);
	return rightRotation(k1);
}
예제 #19
0
파일: avl2.c 프로젝트: Surtr04/misc
Tree* rightAndLeftRotation(Tree *t) {
    t = rightRotation(t);
    return leftRotation(t);
}
예제 #20
0
파일: avl2.c 프로젝트: Surtr04/misc
Tree* leftAndRightRotation(Tree *t) {
    t = leftRotation(t);
    return rightRotation(t);
}
예제 #21
0
파일: AVL_Strom.c 프로젝트: G43riko/ZPRP
TREE* doubleLeftRotation(TREE * k3){
	k3 -> ls = rightRotation(k3 -> ls);
	return leftRotation(k3);
}