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

@@ -39,8 +39,8 @@ EXT = cpp
CFLAGS = -Wall -Wextra -Werror $(INCLUDES)
CFLAGS += -MMD -MP #see end-page note on header dependencie
#CFLAGS += -std=c++98
#CFLAGS += -g3
CFLAGS += -std=c++98
CFLAGS += -g3
CFLAGS_STL = $(CFLAGS)
CFLAGS_STL += -D STL

View File

@@ -175,9 +175,10 @@ private:
// BBST
enum {INSERT, ERASE};
void _balance(node<value_type>* n, bool action);
void _balance(node<value_type>* n);
short _compute_height(node<value_type>* n);
short _balance_factor(node<value_type>* n);
short _balance_f(node<value_type>* n);
node<value_type>* _rotate_left(node<value_type>* n);
node<value_type>* _rotate_right(node<value_type>* n);
};

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)
{

View File

@@ -4,35 +4,35 @@
int main() {
// VECTOR
tests_vector_constructor();
tests_vector_operator_assignation();
tests_vector_begin();
tests_vector_end();
tests_vector_rbegin();
tests_vector_rend();
tests_vector_size();
tests_vector_max_size();
tests_vector_resize();
tests_vector_capacity();
tests_vector_empty();
tests_vector_reserve();
tests_vector_operator_access();
tests_vector_at();
tests_vector_front();
tests_vector_back();
tests_vector_assign();
tests_vector_push_back();
tests_vector_pop_back();
tests_vector_insert();
tests_vector_erase();
tests_vector_swap();
tests_vector_clear();
tests_vector_get_allocator();
tests_vector_swap_non_member();
tests_vector_reverse_iterators();
tests_vector_relational_operators();
// MAP
// tests_vector_constructor();
// tests_vector_operator_assignation();
// tests_vector_begin();
// tests_vector_end();
// tests_vector_rbegin();
// tests_vector_rend();
// tests_vector_size();
// tests_vector_max_size();
// tests_vector_resize();
// tests_vector_capacity();
// tests_vector_empty();
// tests_vector_reserve();
// tests_vector_operator_access();
// tests_vector_at();
// tests_vector_front();
// tests_vector_back();
// tests_vector_assign();
// tests_vector_push_back();
// tests_vector_pop_back();
// tests_vector_insert();
// tests_vector_erase();
// tests_vector_swap();
// tests_vector_clear();
// tests_vector_get_allocator();
// tests_vector_swap_non_member();
// tests_vector_reverse_iterators();
// tests_vector_relational_operators();
//
// // MAP
tests_map_simple();
tests_map_constructor();
tests_map_operator_assignation();
@@ -58,14 +58,14 @@ int main() {
tests_map_get_allocator();
tests_map_swap_non_member();
tests_map_relational_operators();
// STACK
tests_stack_constructor();
tests_stack_empty();
tests_stack_size();
tests_stack_top();
tests_stack_push();
tests_stack_pop();
//
// // STACK
// tests_stack_constructor();
// tests_stack_empty();
// tests_stack_size();
// tests_stack_top();
// tests_stack_push();
// tests_stack_pop();
// execute tests and print them :
int size = test_list.size();