/* Rebalances the subtree in BT rooted at SUBTREE, which contains exactly COUNT nodes. */ static void rebalance_subtree (TreeMap *bt, struct Node *subtree, size_t count) { struct Node *up = subtree->up; struct Node **q = down_link (bt, subtree); tree_to_vine (q); vine_to_tree (q, count); (*q)->up = up; }
/* Deletes P from BT. */ void bt_delete (struct bt *bt, struct bt_node *p) { struct bt_node **q = down_link (bt, p); struct bt_node *r = p->down[1]; if (r == NULL) { *q = p->down[0]; if (*q) (*q)->up = p->up; } else if (r->down[0] == NULL) { r->down[0] = p->down[0]; *q = r; r->up = p->up; if (r->down[0] != NULL) r->down[0]->up = r; } else { struct bt_node *s = r->down[0]; while (s->down[0] != NULL) s = s->down[0]; r = s->up; r->down[0] = s->down[1]; s->down[0] = p->down[0]; s->down[1] = p->down[1]; *q = s; if (s->down[0] != NULL) s->down[0]->up = s; s->down[1]->up = s; s->up = p->up; if (r->down[0] != NULL) r->down[0]->up = r; } bt->size--; /* We approximate .707 as .75 here. This is conservative: it will cause us to do a little more rebalancing than strictly necessary to maintain the scapegoat tree's height invariant. */ if (bt->size < bt->max_size * 3 / 4 && bt->size > 0) { rebalance_subtree (bt, bt->root, bt->size); bt->max_size = bt->size; } }
/* Deletes P from BT. */ static void Delete(TreeMap *bt, struct Node *p) { struct Node **q = down_link (bt, p); struct Node *r = p->down[1]; if (r == NULL) { *q = p->down[0]; if (*q) (*q)->up = p->up; } else if (r->down[0] == NULL) { r->down[0] = p->down[0]; *q = r; r->up = p->up; if (r->down[0] != NULL) r->down[0]->up = r; } else { struct Node *s = r->down[0]; while (s->down[0] != NULL) s = s->down[0]; r = s->up; r->down[0] = s->down[1]; s->down[0] = p->down[0]; s->down[1] = p->down[1]; *q = s; if (s->down[0] != NULL) s->down[0]->up = s; s->down[1]->up = s; s->up = p->up; if (r->down[0] != NULL) r->down[0]->up = r; } bt->count--; /* We approximate .707 as .75 here. This is conservative: it will cause us to do a little more rebalancing than strictly necessary to maintain the scapegoat tree's height invariant. */ if (bt->count < bt->max_size * 3 / 4 && bt->count > 0) { rebalance_subtree (bt, bt->root, bt->count); bt->max_size = bt->count; } if (bt->DestructorFn) bt->DestructorFn(p); iHeap.AddToFreeList(bt->Heap,p); bt->timestamp++; }
t_case *my_graph(t_case *cas, int length) { t_case *elem; elem = cas->next; while (elem != cas) { if (!elem->pass) { if (up_link(elem, cas, length)) return (NULL); if (left_link(elem, cas)) return (NULL); if (right_link(elem, cas, length)) return (NULL); if (down_link(elem, cas, length)) return (NULL); } elem = elem->next; } return (cas); }