void fixup(struct Tree * T, struct node * temp) { struct node * z = temp; while((z->parent)->color == RED) { if(z->parent == ((z->parent)->parent)->lchild) { struct node * y = ((z->parent)->parent)->rchild; if(y->color == RED) { (z->parent)->color = BLACK; y->color = BLACK; ((z->parent)->parent)->color = RED; z = (z->parent)->parent; } else { if(z == (z->parent)->rchild) { z = z->parent; lrotate(T,z); } (z->parent)->color = BLACK; ((z->parent)->parent)->color = RED; rrotate(T,(z->parent)->parent); } } else { struct node * y = ((z->parent)->parent)->lchild; if(y->color == RED) { (z->parent)->color = BLACK; y->color = BLACK; ((z->parent)->parent)->color = RED; z = (z->parent)->parent; } else { if(z == (z->parent)->lchild) { z = z->parent; rrotate(T,z); } (z->parent)->color = BLACK; ((z->parent)->parent)->color = RED; lrotate(T,(z->parent)->parent); } } } (T->root)->color = BLACK; }
int main(void) { printf("Testing lrotate: should get 0x00400000, 0x00000001\n"); printf("lrotate(0x00040000, 4) = 0x%08lx\n", lrotate(0x40000,4)); printf("lrotate(0x00040000, 14) = 0x%08lx\n", lrotate(0x40000,14)); printf("This string should read `hello, world': `%s'\n", asmstr); printf("The integers here should be 1234, 1235 and 4321:\n"); integer = 1234; commvar = 4321; greet(); printf("These pointers should be equal: %p and %p\n", &greet, textptr); printf("So should these: %p and %p\n", selfptr, &selfptr); }
static void do_rotation(struct avltree *tree, struct avlnode *parent) { struct avlnode *node; /* do rotation */ while (parent) { int balance; struct avlnode *left, *right; node = parent; parent = node->parent; left = node->left; right = node->right; balance = avl_balance(node); if (balance < -1) { int son_balance = avl_balance(right); /* RR */ if (son_balance <= 0) { if (!parent) { lrotate(&tree->root); } else { lrotate(node == parent->left ? &parent->left : &parent->right); } continue; } /* RL */ if (son_balance > 0) { rrotate(&node->right); if (!parent) { lrotate(&tree->root); } else { lrotate(node == parent->left ? &parent->left : &parent->right); } continue; } assert(0); } else if (balance > 1) { int son_balance = avl_balance(left); /* LL */ if (son_balance >= 0) { if (!parent) { rrotate(&tree->root); } else { rrotate(node == parent->left ? &parent->left : &parent->right); } continue; } /* LR */ if (son_balance < 0) { lrotate(&node->left); if (!parent) { rrotate(&tree->root); } else { rrotate(node == parent->left ? &parent->left : &parent->right); } continue; } assert(0); } else { avl_update_height(node); } } }