summaryrefslogtreecommitdiffstats
path: root/src/tree.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-08-18 16:44:28 -0400
committerDavid Robillard <d@drobilla.net>2022-08-18 16:44:28 -0400
commit9ac3f4e009fd0d393d2c41449b121498e5b7ad54 (patch)
tree72f0f32f1a9167d10006988005b64e9a451b1d02 /src/tree.c
parentf16da4ef66665cff0f10bdf04b0437cf5e397522 (diff)
downloadzix-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.c18
1 files changed, 6 insertions, 12 deletions
diff --git a/src/tree.c b/src/tree.c
index f745fa6..3355a62 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -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) {