void AVLDict::balance(node *& x){ if( x == NULL ) return; int b = height(x->left) - height(x->right); if( b >= -1 && b <= 1 ) return; if( b == 2 ) { if( height(x->left->left) > height(x->left->right) ) { rotate_left(x); } else { double_rotate_left(x); } } else { if( height(x->right->right) > height(x->right->left) ) { rotate_right(x); } else { double_rotate_right(x); } } }
static void fviol_(bst& b, node* A) { if(!A) { return; } fviol_(b, A->left); fviol_(b, A->right); std::pair<int, bool> left_depth = depth(A->left); std::pair<int, bool> right_depth = depth(A->right); if(std::abs(left_depth.first - right_depth.first) > 1) { //imbalance if(left_depth.first > right_depth.first) { //left subtree if(left_depth.second) { //left child node* k1 = A; node* k2 = A->left; std::cout << "Left-Left: Performing left child single rotate on subtree: " << print(A) << " with k1=" << k1->data << " k2=" << k2->data << std::endl; single_rotate_left(b, k1, k2); } else { //right child node* k1 = A->left; node* k2 = k1->right; node* k3 = A; std::cout << "Left-Right: Performing right child double rotate on subtree: " << print(A) << " with k1=" << k1->data << " k2=" << k2->data << " k3=" << k3->data << std::endl; double_rotate_right(b, k1, k2, k3); } } else { //right subtree if(right_depth.second) { //left child node* k1 = A->right; node* k2 = k1->left; node* k3 = A; std::cout << "Right-Left: Performing left child double rotate on subtree: " << print(A) << " with k1=" << k1->data << " k2=" << k2->data << " k3=" << k3->data << std::endl; double_rotate_left(b, k1, k2, k3); } else { //right child node* k1 = A; node* k2 = A->right; std::cout << "Right-Right: Performing right child single rotate on subtree: " << print(A) << " with k1=" << k1->data << " k2=" << k2->data << std::endl; single_rotate_right(b, k1, k2); } } } }