[Xfce4-commits] <squeeze:peter/btree> Balance btree only if it helps in depth

Peter de Ridder noreply at xfce.org
Tue Oct 11 20:48:01 CEST 2011


Updating branch refs/heads/peter/btree
         to 1b7de7a745e0ac8823f31cbc4fac3af52da0cde7 (commit)
       from 2f2c6178e58c4e3eb4e9a0bbaf39239fdd266fd8 (commit)

commit 1b7de7a745e0ac8823f31cbc4fac3af52da0cde7
Author: Peter de Ridder <peter at xfce.org>
Date:   Tue Oct 11 20:43:08 2011 +0200

    Balance btree only if it helps in depth

 libsqueeze/btree.c |  146 ++++++++++++++++++++++++++-------------------------
 1 files changed, 74 insertions(+), 72 deletions(-)

diff --git a/libsqueeze/btree.c b/libsqueeze/btree.c
index f81d098..5d98f0e 100644
--- a/libsqueeze/btree.c
+++ b/libsqueeze/btree.c
@@ -133,47 +133,48 @@ lsq_btree_insert_sorted_single (
              *    /   \       |       /   \
              *  D:1   E:3     |     E:3   C:5
              */
-            /* Swap the data */
             swap_iter = iter->right;
-            swap_entry = iter->entry;
-            iter->entry = swap_iter->entry;
-            swap_iter->entry = swap_entry;
-
-            /* Reformat the tree links */
-            iter->right = swap_iter->right;
-            swap_iter->right = swap_iter->left;
-            swap_iter->left = iter->left;
-            iter->left = swap_iter;
-
-            /* Fix the balance values
-             *
-             * if B > 0
-             *   A = A - 1 - B
-             * else
-             *   A = A - 1
-             *
-             * diff = A - 1 - B
-             * if diff < 0
-             *   B = B - 1 + diff
-             * else
-             *   B = B - 1
-             */
             swap_balance = swap_iter->balance;
-            swap_iter->balance = iter->balance - 1;
+            /* Only balance if it helps */
             if ( 0 < swap_balance )
             {
-                swap_iter->balance -= swap_balance;
-            }
-            iter->balance = iter->balance - 1 - swap_balance;
-            if ( 0 < iter->balance )
-            {
-                iter->balance = 0;
-            }
-            iter->balance += swap_balance - 1;
+                /* Swap the data */
+                swap_entry = iter->entry;
+                iter->entry = swap_iter->entry;
+                swap_iter->entry = swap_entry;
+
+                /* Reformat the tree links */
+                iter->right = swap_iter->right;
+                swap_iter->right = swap_iter->left;
+                swap_iter->left = iter->left;
+                iter->left = swap_iter;
+
+                /* Fix the balance values
+                 *
+                 * if B > 0
+                 *   A = A - 1 - B
+                 * else
+                 *   A = A - 1
+                 *
+                 * diff = A - 1 - B
+                 * if diff < 0
+                 *   B = B - 1 + diff
+                 * else
+                 *   B = B - 1
+                 */
+                swap_iter->balance = iter->balance - 1;
+                if ( 0 < swap_balance )
+                {
+                    swap_iter->balance -= swap_balance;
+                }
+                iter->balance = iter->balance - 1 - swap_balance;
+                if ( 0 < iter->balance )
+                {
+                    iter->balance = 0;
+                }
+                iter->balance += swap_balance - 1;
 
-            /* We added a child so our depth was increased, but we also saved depth by rotation so our parents depth stays the same */
-            if ( 0 < swap_balance )
-            {
+                /* We added a child so our depth was increased, but we also saved depth by rotation so our parents depth stays the same */
                 break;
             }
         }
@@ -183,47 +184,48 @@ lsq_btree_insert_sorted_single (
             /* The code could be easier if we would just overwrite our parent left or right value.
              * But instead we move that data from our left to our self and use the left tree link to be placed in the tree as if it was ourself.
              */
-            /* Swap the data */
             swap_iter = iter->left;
-            swap_entry = iter->entry;
-            iter->entry = swap_iter->entry;
-            swap_iter->entry = swap_entry;
-
-            /* Reformat the tree links */
-            iter->left = swap_iter->left;
-            swap_iter->left = swap_iter->right;
-            swap_iter->right = iter->right;
-            iter->right = swap_iter;
-
-            /* Fix the balance values
-             *
-             * if B < 0
-             *   A = A + 1 - B
-             * else
-             *   A = A + 1
-             *
-             * diff = A + 1 - B
-             * if diff > 0
-             *   B = B + 1 + diff
-             * else
-             *   B = B + 1
-             */
             swap_balance = swap_iter->balance;
-            swap_iter->balance = iter->balance + 1;
+            /* Only balance if it helps */
             if ( 0 > swap_balance )
             {
-                swap_iter->balance -= swap_balance;
-            }
-            iter->balance = iter->balance + 1 - swap_balance;
-            if ( 0 > iter->balance )
-            {
-                iter->balance = 0;
-            }
-            iter->balance += swap_balance + 1;
+                /* Swap the data */
+                swap_entry = iter->entry;
+                iter->entry = swap_iter->entry;
+                swap_iter->entry = swap_entry;
+
+                /* Reformat the tree links */
+                iter->left = swap_iter->left;
+                swap_iter->left = swap_iter->right;
+                swap_iter->right = iter->right;
+                iter->right = swap_iter;
+
+                /* Fix the balance values
+                 *
+                 * if B < 0
+                 *   A = A + 1 - B
+                 * else
+                 *   A = A + 1
+                 *
+                 * diff = A + 1 - B
+                 * if diff > 0
+                 *   B = B + 1 + diff
+                 * else
+                 *   B = B + 1
+                 */
+                swap_iter->balance = iter->balance + 1;
+                if ( 0 > swap_balance )
+                {
+                    swap_iter->balance -= swap_balance;
+                }
+                iter->balance = iter->balance + 1 - swap_balance;
+                if ( 0 > iter->balance )
+                {
+                    iter->balance = 0;
+                }
+                iter->balance += swap_balance + 1;
 
-            /* We added a child so our depth was increased, but we also saved depth by rotation so our parents depth stays the same */
-            if ( 0 > swap_balance )
-            {
+                /* We added a child so our depth was increased, but we also saved depth by rotation so our parents depth stays the same */
                 break;
             }
         }


More information about the Xfce4-commits mailing list