tree_t *rotateright(tree_t *p) { tree_t *q = p->left; p->left = q->right; q->right = p; fixheight(p); fixheight(q); return q; }
tree_t *rotateleft(tree_t *q) { tree_t *p = q->right; q->right = p->left; p->left = q; fixheight(q); fixheight(p); return p; }
void deletemin (avltree ** n, void **dataptr) { avltree *temp; short dif; if ((*n)->left != NULL) /* keep going for leftmost node */ deletemin (&(*n)->left, dataptr); else { /* leftmost node found */ *dataptr = (*n)->data; /* get pointer to data */ temp = *n; *n = (*n)->right; /* return pointer to right subtree */ free (temp); /* of leftmost node */ } if (*n != NULL) { fixheight (*n); dif = difference (*n); if (dif > 1) /* deletion caused left subtree to be too high */ balanceleft (n, -1); else if (dif < -1) /* deletion caused right subtree to be too high */ balanceright (n, -1); } }
tree_t *balance(tree_t *p) { fixheight(p); if (bfactor(p) == 2) { if (bfactor(p->right) < 0) p->right = rotateright(p->right); return rotateleft(p); } if (bfactor(p) == -2) { if (bfactor(p->left) > 0) p->left = rotateleft(p->left); return rotateright(p); } return p; }