Beispiel #1
0
link LLRBinsert(link h, Item *item) {
    Key v = key(item);
    /* Insert a new node at the bottom*/
    
    conflict = NULL;
    
    if (h == z) {
        return NEW(item, z, z, 1, 1);
    }
    
    if (less(v, key(h->item)))
        hl = LLRBinsert(hl, item);
    
    else if (eq(v, key(h->item))) {   /* If the object we are trying to insert is already there,
                                        we opt to return a pointer to the existing item, so that
                                        the user may choose what to do (i.e. create a list of items) */
        conflict = h->item;
    }
    
    else
        hr = LLRBinsert(hr, item);
    
    
    /* Enforce left-leaning condition */
    if (hr->red && !hl->red) h = rotL(h);
    if (hl->red && hll->red) h = rotR(h);
    if (hl->red && hr->red) colorFlip(h);
    
    return fixNr(h);
}
Beispiel #2
0
Datei: AVL.c Projekt: Arch23/AVL
Node* OPfunction(Node *N){
   switch(verifBal(N)){
      case(NO_ACTION):{
         break;
      }
      case(SIMPLE_LEFT):{
         printf("\nSIMPLE LEFT");
         N = rotL(N);
         break;
      }
      case(SIMPLE_RIGHT):{
         printf("\nSIMPLE RIGHT");
         N = rotR(N);
         break;
      }
      case(DOUBLE_LR):{
         printf("\nDOUBLE LR");
         N = rotLR(N);
         break;
      }
      case(DOUBLE_RL):{
         printf("\nDOUBLE RL");
         N = rotRL(N);
         break;
      }
   }
   updateBal(N);
   return(N);
}
Beispiel #3
0
BTNode* insert_node(BTNode* t ,int item, int sw){//sw是分左右的,key 0是黑,1是红
        if(t==null) return NODE(item,null,null,1);
        if(t->l->key && t->r->key){
                t->key=1;t->l->key=0;t->r->key=0;
        }
        if(item<t->item){
                t->l=insert_node(t->l,item,0);
                if(t->key && t->l->key && sw) t=rotR(t);
                if(t->l->key && t->l->l->key)
                { t=rotR(t); t->key=0; t->r->key=1; }
        }else{
                t->r=insert_node(t->r,item,1);
                if(t->key && t->r->key && !sw) t=rotL(t);
                if(t->r->key && t->r->r->key)
                { t=rotL(t); t->key=0; t->l->key=1; }
        }
        return t;
}
Beispiel #4
0
link rsRBinsert(link h, Item item, int sw)
// Sedgewick's code, with details included - NOT TESTED
{
Key v = key(item);

if (h == z)
  return NEW(item, z, z, 1);

if ((h->l->red) && (h->r->red)) 
{
  h->red = 1;
  h->l->red = 0;
  h->r->red = 0;
}

if (less(v, key(h->item))) 
{ 
  h->l = rsRBinsert(h->l, item, 0); 
  if (h->red && h->l->red && sw)
    h = rotR(h); 
  if (h->l->red && h->l->l->red) 
  {
    h = rotR(h);
    h->red = 0;
    h->r->red = 1;
   }
}
else
{ 
  h->r = rsRBinsert(h->r, item, 1); 
  if (h->red && h->r->red && !sw)
    h = rotL(h); 
  if (h->r->red && h->r->r->red) 
  {
    h = rotL(h);
    h->red = 0;
    h->l->red = 1;
  }
}
fixRank(h);
return h;
}
// BST根插入
link insertT(link h, Item item){
	Key v = key(item);
	if(h == z)
		return NEW(item,z,z,1);
	if(less(v,key(h->item))) {
		h->l = insertT(h->l, item);
		h = rotR(h);
	}
	else {
		h->r = insertT(h->r, item);
		h = rotL(h);
	}
	return h;
}
static link LLRBinsert(link h, EnglishWord newWord){ 
	Key v = makeKey(newWord);
	int val = 0;

	if (compareNodes(h,z))
		return NEW(newWord, z, z, 1);
	
	val = compareKeys(v,makeKey(h->englishWord));
	if ( val < 0)
		hl = LLRBinsert(hl, newWord);
    else
		hr = LLRBinsert(hr, newWord);
    if (hr->red && !hl->red)
		h = rotL(h);
    if (hl->red && hll->red) 
		h = rotR(h);
    if (hl->red && hr->red) 
		colorFlip(h);

	return h;
}
Beispiel #7
0
/******************************************************************************************
* AVLbalance()
*
* Arguments:    h:  ponteiro para um no da arvore
*
* Returns: link retorna um no da arvore
* Description:  balanceia a arvore AVL
*****************************************************************************************/
link AVLbalance(link h){
    int balanceFactor;
    if (h==NULL) 
        return h;
    balanceFactor = Balance(h);
    if(balanceFactor > 1){
        if (Balance(h->l) >= 0) 
            h=rotR(h);
        else                 
            h=rotLR(h);
    }
    else if(balanceFactor < -1){
        if (Balance(h->r) <= 0) 
            h = rotL(h);
        else                 
            h = rotRL(h);
    } else{
        int peso_left = peso(h->l); 
        int peso_right = peso(h->r);
        h->peso = peso_left > peso_right ?  peso_left + 1 : peso_right + 1;
    }
    return h; 
}
Beispiel #8
0
link RBinsert(link h, Item item, int sw)
// Program 13.6 coded to be a bit clearer and make mutually exclusive
// cases obvious.  Also includes tracing.  See 2320 notes.  BPW
// h is present node in search down tree.
// Returns root of modified subtree.
// item is the Item to be inserted.
// sw == 1 <=> h is to the right of its parent.
{
Key v = key(item);
link before;  // Used to trigger printing of an intermediate tree

tracePrint("Down",h);
if (h == z)
  return NEW(item, z, z, 1);  // Attach red leaf

if ((h->l->red) && (h->r->red))      // Flip colors before searching down
{
  tracePrint("Color flip",h);
  h->red = 1;
  h->l->red = 0;
  h->r->red = 0;
  if (trace==2)
    STprintTree();
}

if (less(v, key(h->item))) 
{ 
  tracePrint("Insert left",h);
  before=h->l;
  h->l = RBinsert(h->l, item, 0);    // Insert in left subtree
  if (trace==2 && before!=h->l)      // Has a rotation occurred?
    STprintTree();
  if (h->l->red)
    if (h->red)
      if (sw)
      {
        tracePrint("Case ~1",h);
        h = rotR(h);                 // Set up case ~2 after return
      }
      else
        ;
    else if (h->l->l->red)
    {
      tracePrint("Case 2",h);
      h = rotR(h);
      h->red = 0;
      h->r->red = 1;
    }
}
else
{
  tracePrint("Insert right",h);
  before=h->r;
  h->r = RBinsert(h->r, item, 1);    // Insert in right subtree
  if (trace==2 && before!=h->r)      // Has a rotation occurred?
    STprintTree();
  if (h->r->red)
    if (h->red)
      if (!sw)
      {
        tracePrint("Case 1",h);
        h = rotL(h);                 // Set up case 2 after return
      }
      else
        ;
    else if (h->r->r->red)
    {
      tracePrint("Case ~2",h);
      h = rotL(h);
      h->red = 0;
      h->l->red = 1;
    }
}

fixRank(h); //fix the rank after rotating
tracePrint("Up",h);
return h;
}
Beispiel #9
0
/******************************************************************************************
* rotRL()
*
* Arguments:    h:  ponteiro para um no da arvore
*
* Returns: link
* Description:  faz uma rotacao dupla direita esquerda
*****************************************************************************************/
link rotRL(link h){
    if (h==NULL) 
        return h;
    h->r = rotR(h->r);
    return rotL(h);
}
Beispiel #10
0
/******************************************************************************************
* rotLR()
*
* Arguments:    h:  ponteiro para um no da arvore
*
* Returns: link
* Description:  faz uma rotacao dupla esquerda direita
*****************************************************************************************/
link rotLR(link h){
    if (h == NULL) 
        return h;
    h->l = rotL(h->l);
    return rotR(h);
}
Beispiel #11
0
Datei: AVL.c Projekt: Arch23/AVL
Node* rotRL(Node *N){
   N->r = rotR(N->r);
   N = rotL(N);
   return(N);
}
Beispiel #12
0
Datei: AVL.c Projekt: Arch23/AVL
Node* rotLR(Node *N){
   N->l = rotL(N->l);
   N = rotR(N);
   return(N);
}
Beispiel #13
0
link balance(link h) {
    if (hr->red) h = rotL(h);
    if (hl->red && hll->red) h = rotR(h);
    if (hl->red && hr->red) colorFlip(h);
    return fixNr(h);
}
Beispiel #14
0
link mvRedL(link h) {
    colorFlip(h);
    if (hrl->red) { hr = rotR(hr); h = rotL(h); }
    return h;
}
	link tmp = NULL;
	nr++;
	if (h == z)
		return NULL;
	if eq(v, t)
		return h;
	if less(v, t){
		if ((tmp = searchR(h->l, v)) != NULL){
			h->l = tmp;	h = rotR(h);
		}
		else
			return NULL;
	}
	else{
		if ((tmp = searchR(h->r, v)) != NULL){
			h->r = tmp; h = rotL(h);
		}
		else
			return NULL;
	}

	return h;
}

Item STsearch(Key v){
	//link tmp = NULL;
	head = searchR(head, v);
	if ((head != NULL) && (head != z))
		return (head->item);
	else
		return 0;