wip balance identical for erase and insert now

This commit is contained in:
Hugo LAMY
2022-07-06 04:10:07 +02:00
parent 81b76ae040
commit 3280f4a754
4 changed files with 67 additions and 57 deletions

View File

@@ -176,7 +176,7 @@ MP_TPL pair<typename MP::iterator, bool> MP::
next->up = n;
_size++;
_balance(n, INSERT);
_balance(n);
return (ft::make_pair(iterator(next, _sentinel), true));
}
MP_TPL typename MP::iterator MP::
@@ -238,7 +238,7 @@ MP_TPL void MP::
_allocator_node.deallocate(n, 1);
_size--;
_balance(replace, ERASE);
_balance(replace);
}
MP_TPL void MP::
erase(iterator first, iterator last) {
@@ -417,7 +417,7 @@ MP_TPL typename MP::allocator_type MP::
* private functions :
*********************/
MP_TPL void MP::
_balance(node<value_type>* n, bool action) {
_balance(node<value_type>* n) {
node<value_type>* old_n;
node<value_type>* parent = NULL;
@@ -425,20 +425,25 @@ MP_TPL void MP::
while (n)
{
n->height = _compute_height(n);
// n->height = _balance_f(n);
if (_balance_factor(n) > 1) // Left Heavy
// if (n->height > 1) // Left Heavy
{
parent = n->up;
if (_balance_factor(n->left) < 0) // Left-Right Case (BF == -1)
// if (n->left->height < 0) // Left-Right Case (BF == -1)
n->left = _rotate_left(n->left);
// Left-Left Case
n = _rotate_right(n);
old_n = n->right;
}
else if (_balance_factor(n) < -1) // Right Heavy
// else if (_balance_factor(n) < -1) // Right Heavy
else if (n->height < -1) // Right Heavy
{
parent = n->up;
if (_balance_factor(n->right) > 0) // Right-Left Case (BF == 1)
// if (n->right->height > 0) // Right-Left Case (BF == 1)
n->right = _rotate_right(n->right);
// Right-Right Case
n = _rotate_left(n);
@@ -451,23 +456,11 @@ MP_TPL void MP::
parent->left = n;
else
parent->right = n;
if (action == INSERT)
break;
else if (action == ERASE)
parent = NULL;
parent = NULL;
}
n = n->up;
}
if (action == INSERT)
{
while (n)
{
n->height = _compute_height(n);
n = n->up;
}
}
}
MP_TPL short MP::
_compute_height(node<value_type>* n) {
@@ -481,6 +474,18 @@ MP_TPL short MP::
else
return 1;
}
MP_TPL short MP::
_balance_f(node<value_type>* n) {
if (n->left && n->right)
return n->left->height - n->right->height;
else if (n->left)
return (n->left->height + 1);
else if (n->right)
return (-1 - (n->right->height));
else
return 0;
}
MP_TPL short MP::
_balance_factor(node<value_type>* n) {
@@ -508,6 +513,8 @@ MP_TPL node<typename MP::value_type>* MP::
n->height = _compute_height(n);
ori_right->height = _compute_height(ori_right);
// n->height = _balance_f(n);
// ori_right->height = _balance_f(ori_right);
if (n == _root)
{
@@ -532,6 +539,8 @@ MP_TPL node<typename MP::value_type>* MP::
n->height = _compute_height(n);
ori_left->height = _compute_height(ori_left);
// n->height = _balance_f(n);
// ori_left->height = _balance_f(ori_left);
if (n == _root)
{