home *** CD-ROM | disk | FTP | other *** search
- /* Binary tree delete procedure
- *
- * Input parameter "s" points to a scan for an XOR
- * chained binary tree.
- * The procedure deletes s->child from the tree,
- * and returns with s set by GoParent(s)
- */
-
- void Delete(s)
- struct Scan *s;
- {
- struct Scan temp, *t;
- struct Item *i, *j, *k;
-
- i = s->child; /*i is the item to be deleted*/
- GoParent(s);
-
- if (i->llink == i->rlink) /* Case 1 */
- {
- /*
- * adjust the pointers for s->child,
- * i's parent
- */
- if (i->key < s->child->key)
- s->child->llink = s->parent;
- else
- s->child->rlink = s->parent;
- }
-
- else if (i->llink == s->child) /* Case 2 */
- {
- /*
- * adjust the pointers for s->child,
- * i's parent
- */
-
- if (i->key < s->child->key)
- s->child->llink = i->rlink ^
- s->parent ^ s->child;
- else
- s->child->rlink = i->rlink ^
- s->parent ^ s->child;
-
- /*
- * adjust the pointers for i's child
- */
-
- j = i->rlink ^ s->child;
- j->rlink ^= i ^ s->child;
- j->llink ^= i ^ s->child;
- }
-
- else if (i->rlink == s->child) /* Case 3 */
- {
- /*
- * adjust the pointers for s->child,
- * i's parent
- */
- if( i->key < s->child->key )
- s->child->llink = i->llink ^
- s->parent ^
- s->child;
- else
- s->child->rlink = i->llink ^
- s->parent ^
- s->child;
- /*
- * adjust the pointers for i's child
- */
-
- j = i->llink ^ s->child;
- j->rlink ^= i ^ s->child;
- j->llink ^= i ^ s->child;
- }
-
- else /* Case 4 */
- {
- j = i->llink ^ s->child;
-
- if (j->rlink == i) /* Case 4a */
- {
- /*
- * adjust the pointers for s->child,
- * i's parent
- */
-
- if (i->key < s->child->key)
- s->child->llink = j ^ s->parent;
- else
- s->child->rlink = j ^ s->parent;
-
- /*
- * adjust the pointers for i's children
- */
-
- j->llink ^= i ^ s->child;
- j->rlink = i->rlink;
- k = i->rlink ^ s->child;
- k->llink ^= i ^ j;
- k->rlink ^= i ^ j;
- }
-
- else /* Case 4b */
- {
- /*
- * locate the replacement item
- */
-
- t = &temp;
- Associate(t,s->root);
- t->parent = s->child;
- t->child = i;
- GoLeft(t);
-
- while( t->child->rlink != t->parent )
- GoRight(t);
-
- /*
- * adjust the pointers to free t->child
- */
-
- t->parent->rlink ^= t->child->llink ^
- t->parent ^
- t->child;
-
- if (t->child->llink != t->parent)
- {
- k = t->child->llink ^ t->parent;
- k->llink ^= t->parent ^ t->child;
- k->rlink ^= t->parent ^ t->child;
- }
-
- /*
- * adjust the pointers for s->child,
- * i's parent
- */
-
- if (i->key < s->child->key)
- s->child->llink = t->child ^ s->parent;
- else
- s->child->rlink = t->child ^ s->parent;
-
- t->child->llink = i->llink;
- t->child->rlink = i->rlink;
-
- /*
- * adjust the pointers for i's children
- */
-
- j->llink ^= i ^ t->child;
- j->rlink ^= i ^ t->child;
- k = i->rlink ^ s->child;
- k->llink ^= i ^ j;
- k->rlink ^= i ^ j;
- }
- }
- DropItem(i);
- }
-