NodeT *balancing(NodeT *p) { preorderEchilibru(p); NodeT *w; if(p->ech==-2) { w=p->left; if(w->ech==1) { p=doubleRotationRight(p); } else { p=simpleRotationRight(p); } } else if(p->ech==2) { w=p->right; if(w->ech==-1) { p=doubleRotationLeft(p); } else { p=simpleRotationLeft(p); } } return p; }
/** * Fixes a tree configuration by rotations. * - If a node has a inverse balance in relation to * his parent after the insertion, a double rotation * on parent must be done. The direction depends only * of the balance. * - If both has the same balance signal, a single rotation * on unbalanced node must be done. * @param node * @return */ Node *fix(Node* node) { if (node != NULL) { if (node->balance >= 2) { if (node->left && node->left->balance > 0) { cout << "Rotacao simples a direita no node " << node->info << endl; node = singleRotationRight(node); } else { cout << "Rotacao dupla a direita no node " << node->info << endl; node = doubleRotationRight(node); } } else if (node->balance <= -2) { if (node->right && node->right->balance < 0) { cout << "Rotacao simples a esquerda no node " << node->info << endl; node = singleRotationLeft(node); } else { cout << "Rotacao dupla a esquerda no node " << node->info << endl; node = doubleRotationLeft(node); } } resetHeightAndBalance(node); node->left = fix(node->left); node->right = fix(node->right); } return node; }