diff options
author | David Robillard <d@drobilla.net> | 2022-08-18 16:44:28 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-08-18 16:44:28 -0400 |
commit | 9ac3f4e009fd0d393d2c41449b121498e5b7ad54 (patch) | |
tree | 72f0f32f1a9167d10006988005b64e9a451b1d02 /src/tree.c | |
parent | f16da4ef66665cff0f10bdf04b0437cf5e397522 (diff) | |
download | zix-9ac3f4e009fd0d393d2c41449b121498e5b7ad54.tar.gz zix-9ac3f4e009fd0d393d2c41449b121498e5b7ad54.tar.bz2 zix-9ac3f4e009fd0d393d2c41449b121498e5b7ad54.zip |
Reduce zix_tree_insert() complexity
Diffstat (limited to 'src/tree.c')
-rw-r--r-- | src/tree.c | 18 |
1 files changed, 6 insertions, 12 deletions
@@ -380,17 +380,11 @@ zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti) if (p && p_height_increased) { int height_change = 0; for (ZixTreeNode* i = p; i && i->parent; i = i->parent) { - if (i == i->parent->left) { - if (--i->parent->balance == -2) { - zix_tree_rebalance(t, i->parent, &height_change); - break; - } - } else { - assert(i == i->parent->right); - if (++i->parent->balance == 2) { - zix_tree_rebalance(t, i->parent, &height_change); - break; - } + i->parent->balance += (i == i->parent->left) ? -1 : 1; + + if (i->parent->balance == -2 || i->parent->balance == 2) { + zix_tree_rebalance(t, i->parent, &height_change); + break; } if (i->parent->balance == 0) { @@ -525,7 +519,7 @@ zix_tree_remove(ZixTree* t, ZixTreeIter* ti) replace->parent->right == replace); } - // Rebalance starting at to_balance upwards. + // Rebalance starting at to_balance upwards for (ZixTreeNode* i = to_balance; i; i = i->parent) { i->balance += d_balance; if (d_balance == 0 || i->balance == -1 || i->balance == 1) { |