Пример #1
0
void *ptree_Successor(pwr_tStatus *sts, ptree_sTable *tp, void *np)
{
  pool_tRef nr = pool_Reference( sts, tp->php, np);

  nr = successorNode( tp, nr);
  if (nr == tp->g->null)
    return NULL;

  np = pool_Address( sts, tp->php, nr);
  return np;
}
Пример #2
0
void *
tree_Successor(pwr_tStatus *sts, tree_sTable *tp, void *np)
{

    np = successorNode(tp, np);
    if (np == tp->null) {
        return NULL;
    } else {
        return np;
    }
}
Пример #3
0
void *ptree_FindSuccessor(pwr_tStatus *sts, ptree_sTable *tp, void *key)
{
  ptree_sNode *np;
  pool_tRef nr;

  nr = findNearNode(tp, key);
  if ( tp->last == tp->g->null)
    pwr_Return(NULL, sts, TREE__NOTFOUND);
  
  if ( nr != tp->g->null)
    nr = successorNode(tp, nr);
  else if ( tp->lastComp < 0)
    nr = tp->last;
  else
    nr = successorNode(tp, tp->last);

  if ( nr == tp->g->null)
    pwr_Return(NULL, sts, TREE__NOTFOUND);

  np = pool_Address(sts, tp->php, nr);
  pwr_Return((void *)np, sts, TREE__FOUND);
}
Пример #4
0
static void emptyTree( pwr_tStatus *sts, ptree_sTable *tp)
{
  pool_tRef nr;
  pool_tRef mr;
  
  if ( tp == NULL) return;

  for ( mr = minimumNode( tp, tp->g->root); mr != pool_cNRef;) {
    if ( mr == tp->g->null) return;
    nr = successorNode( tp, mr);
    mr = deleteNode( tp, mr);
    freeNode( tp, mr);
    mr = nr;
  }
}
Пример #5
0
void *
tree_FindSuccessor(pwr_tStatus *sts, tree_sTable *tp, void *key)
{
    tree_sNode *np;

    np = findNearNode(tp, key);
    if (tp->last == tp->null)
        pwr_Return(NULL, sts, TREE__NOTFOUND);
  
    if (np != tp->null) {
        np = successorNode(tp, np);
    } else if (tp->lastComp < 0) {
        np = tp->last;
    } else {
        np = successorNode(tp, tp->last);
    }

    if (np == tp->null) {
        pwr_Return(NULL, sts, TREE__NOTFOUND);
    } else {
        pwr_Return((void *)np, sts, TREE__FOUND);
    }

}
Пример #6
0
static void
emptyTree(pwr_tStatus *sts, tree_sTable *tp)
{
    tree_sNode *np;
    tree_sNode *mp;
  
    if (tp == NULL) return;

    for (mp = minimumNode(tp, tp->root); mp != NULL;) {
        if (mp == tp->null) return;
        np = successorNode(tp, mp);
        mp = deleteNode(tp, mp);
        freeNode(tp, mp);
	mp = np;
    }
}
Пример #7
0
static pool_tRef deleteNode( ptree_sTable *tp, 
			     pool_tRef zr)
{
  ptree_sNode *p;
  pool_tRef   pr;
  ptree_sNode *x;
  pool_tRef   xr;
  ptree_sNode *y;
  pool_tRef   yr;
  ptree_sNode *p1;
  pool_tRef   p1r;
  ptree_sNode *p2;
  pool_tRef   p2r;
  int        h;
  int        b1;
  int        b2;
  ptree_sNode *left, *right, *parent;
  pwr_tStatus sts;
  ptree_sNode *z = pool_Address(&sts, tp->php, zr);
  
  tp->g->nDelete++;
  tp->g->nNode--;

  if (z->left == tp->g->null || z->right == tp->g->null) {
    y = z;
    yr = zr;
  }
  else {
    yr = successorNode(tp, zr);
    y = pool_Address(&sts, tp->php, yr);
  }

  if (y->left != tp->g->null)
    xr = y->left;
  else
    xr = y->right;

  x = pool_Address(&sts, tp->php, xr);
  x->parent = y->parent;
  parent = pool_Address(&sts, tp->php, y->parent);
  if (y->parent == tp->g->null) {
    tp->g->root = xr;
    h = 0;
  } else if (yr == parent->left) {
    parent->left = xr;
    h = 1; /* left branch has shrunk */
  } else {
    parent->right = xr;
    h = -1;  /* right branch has shrunk */
  }
  
  if (y->parent == zr) {
    p = y;
    pr = yr;
  }
  else {
    pr = y->parent;
    p = parent;
  }
  
  if (z != y) { /* Replace z with y */
    y->bal = z->bal;
    y->parent = z->parent;
    parent = pool_Address(&sts, tp->php, z->parent);
    if (z->parent == tp->g->null) {
      tp->g->root = yr;
    } else if (zr == parent->left) {
      parent->left = yr;
    } else {
      parent->right = yr;
    }
    y->left = z->left;
    if (z->left != tp->g->null) {
      left = pool_Address(&sts, tp->php, z->left);
      left->parent = yr;
    }
    y->right = z->right;
    if (z->right != tp->g->null) {
      right = pool_Address(&sts, tp->php, z->right);
      right->parent = yr;
    }
  }

  for (; pr != tp->g->null && h != 0; 
       pr = p->parent, p = pool_Address(&sts, tp->php, pr)) {
    if (h == -1) { /* right branch has shrunk */
      switch (p->bal) {
      case 1:
	p->bal = 0;
	break;
      case 0:
	p->bal = -1;
	h = 0;
	break;
      case -1:
	p1r = p->left;
	p1 = pool_Address(&sts, tp->php, p1r);
	b1 = p1->bal;
	if (b1 < 1) { /* single LL rotation */
	  tp->g->nLL++;
	  p->left = p1->right;
	  right = pool_Address(&sts, tp->php, p1->right);
	  right->parent = pr;
	  p1->right = pr;
	  if (b1 == 0) {
	    p->bal = -1;
	    p1->bal = 1;
	    h = 0;
	  } else {
	    p->bal = p1->bal = 0;
	  }
	  p1->parent = p->parent;
	  parent = pool_Address(&sts, tp->php, p->parent);
	  if (p->parent == tp->g->null)
	    tp->g->root = p1r;
	  else if (pr == parent->left)
	    parent->left = p1r;
	  else
	    parent->right = p1r;
	  p->parent = p1r;
	  p = p1;
	  pr = p1r;
	} else { /* double LR rotation */
	  tp->g->nLR++;
	  p2r = p1->right;
	  p2 = pool_Address(&sts, tp->php, p2r);
	  b2 = p2->bal;
	  p1->right = p2->left;
	  left = pool_Address(&sts, tp->php, p2->left);
	  left->parent = p1r;
	  p2->left = p1r;
	  p->left = p2->right;
	  right = pool_Address(&sts, tp->php, p2->right);
	  right->parent = pr;
	  p2->right = pr;
	  p2->parent = p->parent;
	  parent = pool_Address(&sts, tp->php, p->parent);
	  if (p->parent == tp->g->null)
	    tp->g->root = p2r;
	  else if (pr == parent->left)
	    parent->left = p2r;
	  else
	    parent->right = p2r;
	  p->parent = p1->parent = p2r;
	  if (b2 == -1)
	    p->bal = 1;
	  else
	    p->bal = 0;
	  if (b2 == 1)
	    p1->bal = -1;
	  else
	    p1->bal = 0;
	  p2->bal = 0;
	  p = p2;
	  pr = p2r;
	}
	break;
      }
    } else { /* left branch has grown */
      switch (p->bal) {
      case -1:
	p->bal = 0;
	break;
      case 0:
	p->bal = 1;
	h = 0;
	break;
      case 1:
	p1r = p->right;
	p1 = pool_Address(&sts, tp->php, p1r);
	b1 = p1->bal;
	if (b1 > -1) { /* single RR rotation */
	  tp->g->nRR++;
	  p->right = p1->left;
	  left = pool_Address(&sts, tp->php, p1->left);
	  left->parent = pr;
	  p1->left = pr;
	  if (b1 == 0) {
	    p->bal = 1;
	    p1->bal = -1;
	    h = 0;
	  } else {
	    p->bal = p1->bal = 0;
	  }
	  p1->parent = p->parent;
	  parent = pool_Address(&sts, tp->php, p->parent);
	  if (p->parent == tp->g->null)
	    tp->g->root = p1r;
	  else if (pr == parent->left)
	    parent->left = p1r;
	  else
	    parent->right = p1r;
	  p->parent = p1r;
	  p = p1;
	  pr = p1r;
	} else { /* double RL rotation */
	  tp->g->nRL++;
	  p2r = p1->left;
	  p2 = pool_Address(&sts, tp->php, p2r);
	  b2 = p2->bal;
	  p1->left = p2->right;
	  right = pool_Address(&sts, tp->php, p2->right);
	  right->parent = p1r;
	  p2->right = p1r;
	  p->right = p2->left;
	  left = pool_Address(&sts, tp->php, p2->left);
	  left->parent = pr;
	  p2->left = pr;
	  p2->parent = p->parent;
	  parent = pool_Address(&sts, tp->php, p->parent);
	  if (p->parent == tp->g->null)
	    tp->g->root = p2r;
	  else if (pr == parent->left)
	    parent->left = p2r;
	  else
	    parent->right = p2r;
	  p->parent = p1->parent = p2r;
	  if (b2 == 1)
	    p->bal = -1;
	  else
	    p->bal = 0;
	  if (b2 == -1)
	    p1->bal = 1;
	  else
	    p1->bal = 0;
	  p2->bal = 0;
	  p = p2;
	  pr = p2r;
	}
	break;
      }
    }
    parent =  pool_Address(&sts, tp->php, p->parent);
    if (h!= 0 && p->parent != tp->g->null && pr == parent->left)
      h = 1; /* left branch has shrunk */
    else if (h!= 0 && p->parent != tp->g->null && pr == parent->right)
      h = -1;
  }    
  
  return zr;
}
Пример #8
0
static tree_sNode*
deleteNode(tree_sTable *tp, tree_sNode *z)
{
    tree_sNode *p;
    tree_sNode *x;
    tree_sNode *y;
    tree_sNode *p1;
    tree_sNode *p2;
    int        h;
    int        b1;
    int        b2;
  
    tp->nDelete++;
    tp->nNode--;

    if (z->left == tp->null || z->right == tp->null)
        y = z;
    else
        y = successorNode(tp, z);

    if (y->left != tp->null)
        x = y->left;
    else
        x = y->right;

    x->parent = y->parent;
    if (y->parent == tp->null) {
        tp->root = x;
        h = 0;
    } else if (y == y->parent->left) {
        y->parent->left = x;
        h = 1; /* left branch has shrunk */
    } else {
        y->parent->right = x;
        h = -1;  /* right branch has shrunk */
    }

    if (y->parent == z)
        p = y;
    else
        p = y->parent;

    if (z != y) { /* Replace z with y */
        y->bal = z->bal;
        y->parent = z->parent;
        if (z->parent == tp->null) {
            tp->root = y;
        } else if (z == z->parent->left) {
            z->parent->left = y;
        } else {
            z->parent->right = y;
        }
        y->left = z->left;
        if (z->left != tp->null)
            z->left->parent = y;
        y->right = z->right;
        if (z->right != tp->null)
            z->right->parent = y;
    }

    for (; p != tp->null && h != 0; p = p->parent) {
        if (h == -1) { /* right branch has shrunk */
            switch (p->bal) {
            case 1:
                p->bal = 0;
                break;
            case 0:
                p->bal = -1;
                h = 0;
                break;
            case -1:
                p1 = p->left;
                b1 = p1->bal;
                if (b1 < 1) { /* single LL rotation */
                    tp->nLL++;
                    p->left = p1->right;
                    p1->right->parent = p;
                    p1->right = p;
                    if (b1 == 0) {
                        p->bal = -1;
                        p1->bal = 1;
                        h = 0;
                    } else {
                        p->bal = p1->bal = 0;
                    }
                    p1->parent = p->parent;
                    if (p->parent == tp->null)
                        tp->root = p1;
                    else if (p == p->parent->left)
                        p->parent->left = p1;
                    else
                        p->parent->right = p1;
                    p->parent = p1;
                    p = p1;
                } else { /* double LR rotation */
                    tp->nLR++;
                    p2 = p1->right;
                    b2 = p2->bal;
                    p1->right = p2->left;
                    p2->left->parent = p1;
                    p2->left = p1;
                    p->left = p2->right;
                    p2->right->parent = p;
                    p2->right = p;
                    p2->parent = p->parent;
                    if (p->parent == tp->null)
                        tp->root = p2;
                    else if (p == p->parent->left)
                        p->parent->left = p2;
                    else
                        p->parent->right = p2;
                    p->parent = p1->parent = p2;
                    if (b2 == -1)
                        p->bal = 1;
                    else
                        p->bal = 0;
                    if (b2 == 1)
                        p1->bal = -1;
                    else
                        p1->bal = 0;
                    p2->bal = 0;
                    p = p2;
                }
                break;
            }
        } else { /* left branch has grown */
            switch (p->bal) {
            case -1:
                p->bal = 0;
                break;
            case 0:
                p->bal = 1;
                h = 0;
                break;
            case 1:
                p1 = p->right;
                b1 = p1->bal;
                if (b1 > -1) { /* single RR rotation */
                    tp->nRR++;
                    p->right = p1->left;
                    p1->left->parent = p;
                    p1->left = p;
                    if (b1 == 0) {
                        p->bal = 1;
                        p1->bal = -1;
                        h = 0;
                    } else {
                        p->bal = p1->bal = 0;
                    }
                    p1->parent = p->parent;
                    if (p->parent == tp->null)
                        tp->root = p1;
                    else if (p == p->parent->left)
                        p->parent->left = p1;
                    else
                        p->parent->right = p1;
                    p->parent = p1;
                    p = p1;
                } else { /* double RL rotation */
                    tp->nRL++;
                    p2 = p1->left;
                    b2 = p2->bal;
                    p1->left = p2->right;
                    p2->right->parent = p1;
                    p2->right = p1;
                    p->right = p2->left;
                    p2->left->parent = p;
                    p2->left = p;
                    p2->parent = p->parent;
                    if (p->parent == tp->null)
                        tp->root = p2;
                    else if (p == p->parent->left)
                        p->parent->left = p2;
                    else
                        p->parent->right = p2;
                    p->parent = p1->parent = p2;
                    if (b2 == 1)
                        p->bal = -1;
                    else
                        p->bal = 0;
                    if (b2 == -1)
                        p1->bal = 1;
                    else
                        p1->bal = 0;
                    p2->bal = 0;
                    p = p2;
                }
                break;
            }
        }
        if (h!= 0 && p->parent != tp->null && p == p->parent->left)
            h = 1; /* left branch has shrunk */
        else if (h!= 0 && p->parent != tp->null && p == p->parent->right)
            h = -1;
    }    

    return z;
}