예제 #1
0
/*
 * removal
 */
int
AVLTree::Remove (AVLTree * &racine, bool rebalance)
{
  AVLTree *startNode = NULL;
  int remDiff = 0;
  int res = Remove (racine, startNode, remDiff);
  if (res == avl_no_err && rebalance && startNode)
    res = startNode->RestoreBalances (remDiff, racine);
  return res;
}
예제 #2
0
int
AVLTree::RestoreBalances (int diff, AVLTree * &racine)
{
  if (balance > 0)
    {
      if (diff < 0)
	{
	  balance = 0;
	  if (parent)
	    {
	      if (this == parent->child[RIGHT])
		return parent->RestoreBalances (1, racine);
	      if (this == parent->child[LEFT])
		return parent->RestoreBalances (-1, racine);
	    }
	  return avl_no_err;
	}
      else if (diff == 0)
	{
	}
      else if (diff > 0)
	{
	  if (child[LEFT] == NULL)
	    {
//                              cout << "un probleme\n";
	      return avl_bal_err;
	    }
	  AVLTree *r = parent;
	  AVLTree *a = this;
	  AVLTree *b = child[RIGHT];
	  AVLTree *e = child[LEFT];
	  AVLTree *f = e->child[RIGHT];
	  AVLTree *g = e->child[LEFT];
	  if (e->balance > 0)
	    {
	      e->child[RIGHT] = a;
	      e->child[LEFT] = g;
	      a->child[RIGHT] = b;
	      a->child[LEFT] = f;
	      if (a)
		a->parent = e;
	      if (g)
		g->parent = e;
	      if (b)
		b->parent = a;
	      if (f)
		f->parent = a;
	      e->parent = r;
	      if (r)
		{
		  if (r->child[LEFT] == a)
		    r->child[LEFT] = e;
		  if (r->child[RIGHT] == a)
		    r->child[RIGHT] = e;
		}
	      if (racine == this)
		racine = e;
	      e->balance = 0;
	      a->balance = 0;
	      if (r)
		{
		  if (e == r->child[RIGHT])
		    return r->RestoreBalances (1, racine);
		  if (e == r->child[LEFT])
		    return r->RestoreBalances (-1, racine);
		}
	      return avl_no_err;
	    }
	  else if (e->balance == 0)
	    {
	      e->child[RIGHT] = a;
	      e->child[LEFT] = g;
	      a->child[RIGHT] = b;
	      a->child[LEFT] = f;
	      if (a)
		a->parent = e;
	      if (g)
		g->parent = e;
	      if (b)
		b->parent = a;
	      if (f)
		f->parent = a;
	      e->parent = r;
	      if (r)
		{
		  if (r->child[LEFT] == a)
		    r->child[LEFT] = e;
		  if (r->child[RIGHT] == a)
		    r->child[RIGHT] = e;
		}
	      if (racine == this)
		racine = e;
	      e->balance = -1;
	      a->balance = 1;
	      return avl_no_err;
	    }
	  else if (e->balance < 0)
	    {
	      if (child[LEFT]->child[RIGHT] == NULL)
		{
//                                      cout << "un probleme\n";
		  return avl_bal_err;
		}
	      AVLTree *i = child[LEFT]->child[RIGHT]->child[RIGHT];
	      AVLTree *j = child[LEFT]->child[RIGHT]->child[LEFT];

	      f->child[RIGHT] = a;
	      f->child[LEFT] = e;
	      a->child[RIGHT] = b;
	      a->child[LEFT] = i;
	      e->child[RIGHT] = j;
	      e->child[LEFT] = g;
	      if (b)
		b->parent = a;
	      if (i)
		i->parent = a;
	      if (g)
		g->parent = e;
	      if (j)
		j->parent = e;
	      if (a)
		a->parent = f;
	      if (e)
		e->parent = f;
	      f->parent = r;
	      if (r)
		{
		  if (r->child[LEFT] == a)
		    r->child[LEFT] = f;
		  if (r->child[RIGHT] == a)
		    r->child[RIGHT] = f;
		}
	      if (racine == this)
		racine = f;
	      int oBal = f->balance;
	      f->balance = 0;
	      if (oBal > 0)
		{
		  a->balance = -1;
		  e->balance = 0;
		}
	      else if (oBal == 0)
		{
		  a->balance = 0;
		  e->balance = 0;
		}
	      else if (oBal < 0)
		{
		  a->balance = 0;
		  e->balance = 1;
		}
	      if (r)
		{
		  if (f == r->child[RIGHT])
		    return r->RestoreBalances (1, racine);
		  if (f == r->child[LEFT])
		    return r->RestoreBalances (-1, racine);
		}
	      return avl_no_err;
	    }
	}
    }
  else if (balance == 0)
    {
      if (diff < 0)
	{
	  balance = -1;
	}
      else if (diff == 0)
	{
	}
      else if (diff > 0)
	{
	  balance = 1;
	}
      return avl_no_err;
    }
  else if (balance < 0)
    {
      if (diff < 0)
	{
	  if (child[RIGHT] == NULL)
	    {
//                              cout << "un probleme\n";
	      return avl_bal_err;
	    }
	  AVLTree *r = parent;
	  AVLTree *a = this;
	  AVLTree *b = child[LEFT];
	  AVLTree *e = child[RIGHT];
	  AVLTree *f = e->child[LEFT];
	  AVLTree *g = e->child[RIGHT];
	  if (e->balance < 0)
	    {
	      e->child[LEFT] = a;
	      e->child[RIGHT] = g;
	      a->child[LEFT] = b;
	      a->child[RIGHT] = f;
	      if (a)
		a->parent = e;
	      if (g)
		g->parent = e;
	      if (b)
		b->parent = a;
	      if (f)
		f->parent = a;
	      e->parent = r;
	      if (r)
		{
		  if (r->child[LEFT] == a)
		    r->child[LEFT] = e;
		  if (r->child[RIGHT] == a)
		    r->child[RIGHT] = e;
		}
	      if (racine == this)
		racine = e;
	      e->balance = 0;
	      a->balance = 0;
	      if (r)
		{
		  if (e == r->child[RIGHT])
		    return r->RestoreBalances (1, racine);
		  if (e == r->child[LEFT])
		    return r->RestoreBalances (-1, racine);
		}
	      return avl_no_err;
	    }
	  else if (e->balance == 0)
	    {
	      e->child[LEFT] = a;
	      e->child[RIGHT] = g;
	      a->child[LEFT] = b;
	      a->child[RIGHT] = f;
	      if (a)
		a->parent = e;
	      if (g)
		g->parent = e;
	      if (b)
		b->parent = a;
	      if (f)
		f->parent = a;
	      e->parent = r;
	      if (r)
		{
		  if (r->child[LEFT] == a)
		    r->child[LEFT] = e;
		  if (r->child[RIGHT] == a)
		    r->child[RIGHT] = e;
		}
	      if (racine == this)
		racine = e;
	      e->balance = 1;
	      a->balance = -1;
	      return avl_no_err;
	    }
	  else if (e->balance > 0)
	    {
	      if (child[RIGHT]->child[LEFT] == NULL)
		{
//                                      cout << "un probleme\n";
		  return avl_bal_err;
		}
	      AVLTree *i = child[RIGHT]->child[LEFT]->child[LEFT];
	      AVLTree *j = child[RIGHT]->child[LEFT]->child[RIGHT];

	      f->child[LEFT] = a;
	      f->child[RIGHT] = e;
	      a->child[LEFT] = b;
	      a->child[RIGHT] = i;
	      e->child[LEFT] = j;
	      e->child[RIGHT] = g;
	      if (b)
		b->parent = a;
	      if (i)
		i->parent = a;
	      if (g)
		g->parent = e;
	      if (j)
		j->parent = e;
	      if (a)
		a->parent = f;
	      if (e)
		e->parent = f;
	      f->parent = r;
	      if (r)
		{
		  if (r->child[LEFT] == a)
		    r->child[LEFT] = f;
		  if (r->child[RIGHT] == a)
		    r->child[RIGHT] = f;
		}
	      if (racine == this)
		racine = f;
	      int oBal = f->balance;
	      f->balance = 0;
	      if (oBal > 0)
		{
		  a->balance = 0;
		  e->balance = -1;
		}
	      else if (oBal == 0)
		{
		  a->balance = 0;
		  e->balance = 0;
		}
	      else if (oBal < 0)
		{
		  a->balance = 1;
		  e->balance = 0;
		}
	      if (r)
		{
		  if (f == r->child[RIGHT])
		    return r->RestoreBalances (1, racine);
		  if (f == r->child[LEFT])
		    return r->RestoreBalances (-1, racine);
		}
	      return avl_no_err;
	    }
	}
      else if (diff == 0)
	{
	}
      else if (diff > 0)
	{
	  balance = 0;
	  if (parent)
	    {
	      if (this == parent->child[RIGHT])
		return parent->RestoreBalances (1, racine);
	      if (this == parent->child[LEFT])
		return parent->RestoreBalances (-1, racine);
	    }
	  return avl_no_err;
	}
    }
  return avl_no_err;
}