Single Or Double Reversed
The AVL rebalance routine picks between one rotation and two. The //@requires line on rotate_right is the real contract from the lecture notes.
tree* rotate_right(tree* T)
//@requires T != NULL && T->left != NULL;
{ ... }
tree* rebalance_right(tree* T) {
if (height(T->right) - height(T->left) == 2) {
if (height(T->right->left) > height(T->right->right)) { // comparison reversed
T = rotate_left(T);
} else {
T->right = rotate_right(T->right);
T = rotate_left(T);
}
} else {
fix_height(T);
}
return T;
}
Insert 30, then 40, then 50 into an empty tree. What happens during the third insert?
Sign in to answer questions and track your progress
Sign In