Ejemplo n.º 1
0
AVL_node* insert_AVL(AVL_node *node,int key)
{
	if(node == NULL)
	return newNode(key);
	
	if(node->key > key)
	node->left = insert_AVL(node->left, key);
	else
	node->right = insert_AVL(node->right, key);
	
	node->height = max(height(node->left), height(node->right)) +1;
	
	int balance = height(node->left) -  height(node->right);
	
	if(balance > 1 && node->left->key > key)
	return rightrotate(node);
	
	if(balance > 1 && node->left->key < key)
	{
		node->left = leftrotate(node->left);
		return rightrotate(node);
	}
	
	if(balance < -1 && node->right->key < key)
	return leftrotate(node);
	
	if(balance < -1 && node->right->key > key)
	{
		node->right = rightrotate(node->right);
		return leftrotate(node);
	}
	
	return node;
}
int main(void)
{
	int a[] = {6, 2, 4, 1, 0, 84, 3};
	int lena = sizeof(a) / sizeof(int);
	
	//printf("Size of a = %d\n", lena);
	
	printf("Originally, a = ");
	output(a, lena);
	
	//reverse(a, 0, lena - 1);
	//printf("Reversed a = ");
	//output(a, lena);
	
	//reverse(a, 0, lena - 1);
	//printf("After 2 reversals, a = ");
	//output(a, lena);
	
	rightrotate(a, lena);
	printf("After 1 right-rotation, a = ");
	output(a, lena);
	
	rightrotate(a, lena);
	printf("After 2 right-rotations, a = ");
	output(a, lena);
	
	return 0;
}
Ejemplo n.º 3
0
struct node* insert(struct node* root, int value)
{

	if(root == NULL){
		return newnode(value);
	}
	if(value < root->data){
		root->left = insert(root->left, value);
	} else {
		root->right = insert(root->right, value);
	}
	root->height = compare(heightof(root->left), heightof(root->right)) + 1;
	
	b = balance(root);
	if(b > 1){ 
		if(value < root->left->data){		//left left
			return rightrotate(root);
		} else {				//left right
			root->left = leftrotate(root->left);
			return rightrotate(root);
		}
	}
	if(b < -1){
		if(value > root->right->data){		//right right
			return leftrotate(root);
		} else {				//right left
			root->right = rightrotate(root->right);
			return leftrotate(root);
		}
	}
	return root;		 
}
Ejemplo n.º 4
0
void splay(int x, int goal)
{
    int y, z;
    for(;;)
    {
        if((y = pre[x]) == goal)
            break;
        if((z = pre[y]) == goal)
            right[y] == x ? leftrotate(y) : rightrotate(y);
        else
        {
            if(right[z] == y)
            {
                if(right[y] == x)
                    leftrotate(z), leftrotate(y);
                else
                    rightrotate(y), leftrotate(z);
            }
            else
            {
                if(left[y] == x)
                    rightrotate(z), rightrotate(y);
                else
                    leftrotate(y), rightrotate(z);
            }
        }
    }
    update(x);
}
Ejemplo n.º 5
0
AVL_node* delete_AVL(int key, AVL_node* root)
{
	if(root==NULL)
	return NULL;
	if(root->key > key)
	root->left = delete_AVL(key,root->left);
	else if(root->key < key)
	root->right = delete_AVL(key,root->right);
	
	else
	{
		if(root->left == NULL)
		{
			AVL_node* temp = root;
			root = root->right;
			free(temp);
			return(root);
		}
		else if(root->right == NULL)
		{
			AVL_node* temp = root;
			root = root->left;
			free(temp);
			return(root);
		}
		
		AVL_node* temp = minValueNode(root->right);
		root->key = temp->key;
		root->right = delete_AVL(temp->key, root->right);
		return root;
	}
	if (root == NULL)
      return root;
 
    root->height = max(height(root->left), height(root->right)) + 1;
 
    int balance = height(root->left) - height(root->right);
 
    if (balance > 1 && height(root->left->left) - height(root->left->right) >= 0)
        return rightrotate(root);
 
    if (balance > 1 && height(root->left->left) - height(root->left->right) < 0)
    {
        root->left =  leftrotate(root->left);
        return rightrotate(root);
    }

    if (balance < -1 && height(root->right->left) - height(root->right->right) <= 0)
        return leftrotate(root);
 
    if (balance < -1 && height(root->right->left) - height(root->right->right) > 0)
    {
        root->right = rightrotate(root->right);
        return leftrotate(root);
    }
 
    return root;
	
}
Ejemplo n.º 6
0
/* Controllo se le proprietà dopo la cancellazione vanno bene */
void fixup(rbtree *tree, rbnode *x)
{
	while(x != tree->root && x->c == black) {                   /* Finchè x non è la radice e il padre è black */
		if(x == x->up->left) {                                  
             rbnode *w = x->up->right;                             
			 if(w->c == red) {
                     w->c = black;                                 
				     x->up->c = red;                               
     				 leftrotate(tree, x->up);                       
                     w = x->up->right;                             
			}
			if(w->left->c == black && w->right->c == black) {
				w->c = red;                                   
				x = x->up;                                    
			} else {
				if(w->right->c == black) {
					w->left->c = black;                       
					w->c = red;                               
					rightrotate(tree, w);                      
					w = x->up->right;                         
				}
				w->c = x->up->c;                              
				x->up->c = black;                             
				w->right->c = black;                          
				leftrotate(tree, x->up);                       
				x = tree->root;                               
			}
		} else {          
               rbnode *w = x->up->left;                                    
			   if(w->c == red) {  
                       w->c = black;                                 
  				       x->up->c = red;                              
  				       rightrotate(tree, x->up);                     
  				       w = x->up->left;                              
                }
			if(w->right->c == black && w->left->c == black) {
				w->c = red;                                   
				x = x->up;                                    
			} else {
				if(w->left->c == black) {
					w->right->c = black;                      
					w->c = red;                               
					leftrotate(tree, w);                     
					w = x->up->left;                          
				}
				w->c = x->up->c;                              
				x->up->c = black;                             
				w->left->c = black;                          
				rightrotate(tree, x->up);                     
				x = tree->root;                              
			}
		}
	}
	x->c = black;
}
Ejemplo n.º 7
0
static void
rb_insert(rbnode_t *tree, rbnode_t node)
{
  rbnode_t root, uncle, parent;
  root = *tree;
  node->color = _RED_;
  while (node != root && node->parent->color == _RED_) {
    parent = node->parent ;
    if (parent == parent->parent->left) {
      uncle = parent->parent->right;
      if (uncle!=nil && uncle->color == _RED_) {
        parent->color = _BLACK_;
        uncle->color = _BLACK_;
        parent->parent->color = _RED_;
        node = parent->parent;
      } else {
        if (node == parent->right) {
          node = parent;
          leftrotate(tree, node);
          parent = node->parent;
        }
        parent->color = _BLACK_;
        parent->parent->color = _RED_;
        rightrotate(tree,parent->parent);
      }
    } else {
      uncle = parent->parent->left;
      if (uncle != nil && uncle->color == _RED_) {
        parent->color = _BLACK_;
        uncle->color = _BLACK_;
        parent->parent->color = _RED_;
        node = parent->parent;
      } else {
        if (node == parent->left) {
          node = parent;
          rightrotate(tree, node);
          parent = node->parent;
        }
        parent->color = _BLACK_;
        parent->parent->color = _RED_;
        leftrotate(tree,parent->parent);
      }
    }
  }
  (*tree)->color = _BLACK_;
}
Ejemplo n.º 8
0
 void maintain(node_ptr &R) {
     if (R->lchild != nilptr) {
         if (R->lchild->lchild->h == R->rchild->h + 1) {
             rightrotate(R);
         } else if (R->lchild->rchild->h == R->rchild->h + 1) {
             leftrotate(R->lchild);
             rightrotate(R);
         }
     }
     if (R->rchild != nilptr) {
         if (R->rchild->rchild->h == R->lchild->h + 1) {
             leftrotate(R);
         } else if (R->rchild->lchild->h == R->lchild->h + 1) {
             rightrotate(R->rchild);
             leftrotate(R);
         }
     }
 }
Ejemplo n.º 9
0
/* Inserimento di un nuovo elemento nell'albero */
int rbinsert(rbtree *tree, int xa, int ya)
{
	
	rbnode *x = simpleinsert(tree, xa, ya);
	if (x == NULL)
			return -1;
	while(x != tree->root && x->up->c == red) {     /* Finchè x non è la radice e il padre è red */
		if(x->up == x->up->up->left) {              /* Se il padre di x è uguale al figlio sinistro di suo nonno, cioè il padre di x */
			rbnode *y = x->up->up->right;           /* y è uguale allo zio di x */
            if(y->c == red) {                       /* se il colore di y è red */
				x->up->c = black;                   /* il colore del padre di x è black */
				y->c = black;                       /* il colore di y è balck */
				x->up->up->c = red;                 /* il colore del nonno di x è red */
				x = x->up->up;                      /* x è uguale a suo nonno */
			} else {
				if(x == x->up->right){              /* se x è uguale a suo fratello */
                     x = x->up;              
					 leftrotate(tree,x);            /* leftrotate con x */
                     }            				
				x->up->c = black;                   /* il colore del padre di x è black */
				x->up->up->c = red;                 /* il colore del nonno di x è red */
				rightrotate(tree, x->up->up);       /* rightrotate con il nonno di x */
			}
		} else {                                    
			rbnode *y = x->up->up->left;            /* y è uguale al figlio sinistro del nonno di x */
			if(y->c == red) {                       /* se il colore di y è red */
				x->up->c = black;                   /* il colore del padre di x è black */
				y->c = black;                       /* il colore di y è black */
				x->up->up->c = red;                 /* il colore del nonno di x è red */
				x = x->up->up;                      /* x è uguale a suo nonno */
			} else {
				if(x == x->up->left){               /* se x è uguale a suo fratello */
                     x = x->up;                     /* x è uguale a suo padre */        
					 rightrotate(tree,x);           /* rightrotate con x */ 
                     }   					
				x->up->c = black;                   /* il colore del padre di x è black */      
				x->up->up->c = red;                 /* il colore del nonno di x è red */
				leftrotate(tree, x->up->up);        /* leftrotate con il nonno di x */
			}
		}
	}
	tree->root->c = black;                          /* il colore della root è black */
	
}
Ejemplo n.º 10
0
static void
rb_delete_fixup(struct rbtree_elem **rootp, struct rbtree_elem *node)
{
  struct rbtree_elem *root, *parent, *sibling ;

  root = *rootp;
  parent = node->parent;
  while (node != root && node->color == _BLACK_) {
    if (node == parent->left) {
      sibling = parent->right;
      if (sibling->color == _RED_) {
        sibling->color = _BLACK_;
        parent->color = _RED_;
        leftrotate(rootp, parent);
        parent = node->parent;
        sibling = parent->right;
      }
      if (   sibling->left->color == _BLACK_
          && sibling->right->color == _BLACK_) {
        sibling->color = _RED_;
        node = parent;
        parent = node->parent;
      } else {
        if (sibling->right->color == _BLACK_) {
          sibling->left->color = _BLACK_;
          sibling->color = _RED_;
          rightrotate(rootp, sibling);
          parent = node->parent;
          sibling = parent->right;
        }
        sibling->color = parent->color;
        parent->color = _BLACK_;
        sibling->right->color = _BLACK_;
        leftrotate(rootp, parent);
        node = root;
      }
    } else { //node != parent->left
      sibling = parent->left ;
      if (sibling->color == _RED_) {
        sibling->color = _BLACK_;
        parent->color = _RED_;
        rightrotate(rootp, parent);
        parent = node->parent;
        sibling = parent->left;
      }
      if (   sibling->left->color == _BLACK_
          && sibling->right->color==_BLACK_) {
        sibling->color = _RED_;
        node = parent;
        parent = node->parent;
      } else {
        if (sibling->left->color == _BLACK_) {
          sibling->right->color = _BLACK_;
          sibling->color = _RED_;
          leftrotate(rootp, sibling);
          parent = node->parent;
          sibling = parent->left;
        }
        sibling->color = parent->color ;
        parent->color = _BLACK_ ;
        sibling->left->color = _BLACK_ ;
        rightrotate(rootp, parent);
        node = root ;
      }
    }
  }
  node->color = _BLACK_ ;
}
Ejemplo n.º 11
0
// inert a new key into the Red Black Tree
// if key already exist no operation is done
RedBlackNode* RedBlackTree::insert(int newKey){
    // Step 1:
    // create the node; and insert the node with standard BST insert
    
	RedBlackNode * insert = BSTinsert(newKey);
	RedBlackNode * retVal = insert;
	
	// Node is in tree already
	if(insert == 0){
		return 0;
	}
	
    // Step 2:
    // restore Red Black Tree property
    // note that the insertion could only violate the following:
    // of "If a node is red, then its parent is black".
    // All other Red Black Tree properties hold.
    
    // Move up the tree
    while(insert->parent->red){
		
		// When parent is on the left of it's parent
		if(insert->parent == insert->parent->parent->left){
			
			RedBlackNode * y = insert->parent->parent->right;
			
			if(y->red){
				
				// Case 1
				insert->parent->red = false;
				y->red = false;
				insert->parent->parent->red = true;
				insert = insert->parent->parent;
			}
			else{
				
				// Case 2
				if(insert == insert->parent->right){
					insert = insert->parent;
					leftrotate(insert);
				}
				
				// Case 3
				insert->parent->red = false;
				insert->parent->parent->red = true;
				rightrotate(insert->parent->parent);
			}
		}
		// When parent is on the right of it's parent
		else{
			
			RedBlackNode * y = insert->parent->parent->left;
			
			if(y->red){
				
				// Case 1
				insert->parent->red = false;
				y->red = false;
				insert->parent->parent->red = true;
				insert = insert->parent->parent;
			}
			else{
				
				// Case 2
				if(insert == insert->parent->left){
					insert = insert->parent;
					rightrotate(insert);
				}
				
				// Case 3
				insert->parent->red = false;
				insert->parent->parent->red = true;
				leftrotate(insert->parent->parent);
				
			}
		}
	}
	
	// Root's left must be black
	root->left->red = false;
    return retVal;
}
/* insert word into a redblack tree */
int insert(char *word)
{
    TREEREC    *curr = ans->root, *par, *gpar, *prev = NULL, *wcreate();
    int		val;

    if( ans->root == NULL )
    {
	ans->ans = ans->root = wcreate(word, NULL);
	return 1;
    }
    while( curr != NULL && (val = scmp(word, curr->word)) != 0 )
    {
	prev = curr;
	if( val > 0 )
	    curr = curr->right;
	else
	    curr = curr->left;
    }

    ans->ans = curr;

    if( curr == NULL ) /* insert a new node, rotate up if necessary */
    {
	if( val > 0 )
	    curr = prev->right = wcreate(word, prev);
	else
	    curr = prev->left = wcreate(word, prev);

	curr->colour = RED;
	while( (par = curr->par) != NULL
		&& ( gpar = par->par ) != NULL
		&& curr->par->colour == RED )
	{
	    if( par == gpar->left )
	    {
		if( gpar->right!=NULL && gpar->right->colour == RED )
		{
		    par->colour = BLACK;
		    gpar->right->colour = BLACK;
		    gpar->colour = RED;
		    curr = gpar;
		}
		else
		{
		    if( curr == par->right )
		    {
			curr = par;
			leftrotate(ans, curr);
			par = curr->par;
		    }
		    par->colour = BLACK;
		    if( ( gpar=par->par ) != NULL )
		    {
			gpar->colour = RED;
			rightrotate(ans, gpar);
		    }
		}
	    }
	    else
	    {
		if( gpar->left!=NULL && gpar->left->colour == RED )
		{
		    par->colour = BLACK;
		    gpar->left->colour = BLACK;
		    gpar->colour = RED;
		    curr = gpar;
		}
		else
		{
		    if( curr == par->left )
		    {
			curr = par;
			rightrotate(ans, curr);
			par = curr->par;
		    }
		    par->colour = BLACK;
		    if( ( gpar=par->par ) != NULL )
		    {
			gpar->colour = RED;
			leftrotate(ans, gpar);
		    }
		}
	    }
	}
	if( curr->par == NULL )
	    ans->root = curr;
	ans->root->colour = BLACK;
        return 1;
    }
    
    return 0;
}
Ejemplo n.º 13
0
void del(nd* p)
{
    nd *ptr, *temp=root, *imbal=NULL;
    int b;
    if(p!=root)

    {
        while(temp)
    {
        if(temp->left==p||temp->right==p)
            break;
        if(p->data<temp->data)
            temp=temp->left;
        else
            temp=temp->right;
    }
    }
        if(p->left==NULL&&p->right==NULL)
        {
            if(p==root)
                root=NULL;
             else if(temp->left==p)
                temp->left=NULL;
            else
                temp->right=NULL;
            free(p);
        }
        else
        {
            ptr=p;
            ptr=ptr->right;
            if(ptr)
            {
                while(ptr->left)
                    ptr=ptr->left;
                p->data=ptr->data;
                del(ptr);
            }
            else
            {
                if(p==root)
                    root=root->left;
                else if(temp->right==p)
                    temp->right=p->left;
                else
                    temp->left=p->left;
                free(p);
            }

        }
        correctheight(temp->data);
        printf("\nDeleting....");
    ptr=root;
    while(ptr)
    {
        b=bal(ptr);
        if(b<-1||b>1)
            imbal=ptr;
        if(temp->data<ptr->data)
            ptr=ptr->left;
        else
            ptr=ptr->right;
    }


    if(imbal)
    {
        printf("\nImbalance Detected. Proceeding to balance.");
        b=bal(imbal);
        if(b>0)
        {

                if(bal(imbal->left)<0)
                {
                    leftrotate(imbal->left);
                    rightrotate(imbal);
                }
                else
                    rightrotate(imbal);
        }
        if(b<0)
        {

                if(bal(imbal->right)>0)
                {
                    rightrotate(imbal->right);
                    leftrotate(imbal);
                }
                else
                    leftrotate(imbal);
        }
    }
}
Ejemplo n.º 14
0
void insert(int val)
{
    nd *ptr=root, *parent, *temp, *imbal=NULL;
    int b;
    while(ptr)
    {
        parent=ptr;
        if(val<ptr->data)
            ptr=ptr->left;
        else
            ptr=ptr->right;
    }
    temp=createnode(val);
    if(val<parent->data)
        parent->left=temp;
    else
        parent->right=temp;
    printf("\nNode Created. Proceeding to balance.");
    correctheight(temp->data);
    printf("\nHeight Corrected.");
    ptr=root;
    while(ptr)
    {
        b=bal(ptr);
        if(b<-1||b>1)
            imbal=ptr;
        if(temp->data<ptr->data)
            ptr=ptr->left;
        else
            ptr=ptr->right;
    }

    if(imbal)
    {
        printf("\nImbalance Detected. Proceeding to balance.");
        b=bal(imbal);
        if(b>0)
        {

                if(bal(imbal->left)<0)
                {
                    leftrotate(imbal->left);
                    rightrotate(imbal);
                }
                else

                        rightrotate(imbal);




        }
        if(b<0)
        {

                if(bal(imbal->right)>0)
                {
                    rightrotate(imbal->right);
                    leftrotate(imbal);
                }
                else
                    leftrotate(imbal);


    }
    }
    printf("\nTree balanced.");
}