Previous 199869 Revisions Next

r33941 Thursday 18th December, 2014 at 07:27:14 UTC by Fabrice Bellet
debug: fix the avl trees logic

The ancestor heights of a node were not updated properly.
[src/lib/util]simple_set.h

trunk/src/lib/util/simple_set.h
r242452r242453
103103         // See if it's a leaf
104104         if (currNode->isLeaf())
105105         {
106            // Get the parent object
107            tree_node* parentNode = currNode->parent;
108
106109            // If we're a leaf and we have no parent, then the tree will be emptied
107110            if (!currNode->parent)
108111            {
r242452r242453
112115            // If it's a leaf node, simply remove it
113116            removeNode(currNode);
114117            global_free(currNode);
118
119            // Update the balance of the parent of
120            // currentNode after having disconnected
121            // it
122            if (parentNode)
123               parentNode->calcHeights();
115124         }
116125         else
117126         {
r242452r242453
129138               replaceNode = findMax(currNode->left);
130139            }
131140
141            tree_node* parentReplaceNode = replaceNode->parent;
142
132143            // Disconnect the replacement node's branch
133144            removeNode(replaceNode);
134145
146            // Update the balance of the parent of
147            // replaceNode after having disconnected
148            // it
149            if (parentReplaceNode)
150               parentReplaceNode->calcHeights();
151
135152            // Disconnect the current node
136153            removeNode(currNode);
137154
r242452r242453
286303         if (retVal)
287304         {
288305            t->left->setParent(t);
306            t->calcHeights();
307
289308            if(t->balanceFactor() < -1)
290309            {
291310               // See if it went left of the left
r242452r242453
311330         if (retVal)
312331         {
313332            t->right->setParent(t);
333            t->calcHeights();
314334
315335            if (t->balanceFactor() > 1)
316336            {
r242452r242453
389409            if (retVal && !t->left->parent)
390410            {
391411               t->left->setParent(t);
412               t->calcHeights();
392413            }
393414         }
394415         else if (t->element < b->element)
r242452r242453
399420            if (retVal && !t->right->parent)
400421            {
401422               t->right->setParent(t);
423               t->calcHeights();
402424            }
403425
404426            return retVal;
r242452r242453
502524
503525      k2 = k1;
504526      k2->setParent(k2Parent);
527
528      k2->right->calcHeights();
529
505530   }
506531
507532
r242452r242453
520545
521546      k1 = k2;
522547      k1->setParent(k1Parent);
548
549      k1->left->calcHeights();
550
523551   }
524552
525553
r242452r242453
722750   {
723751      // Set our new parent
724752      parent = p;
725
726      // If we have a valid parent, set its height
727      if (parent)
728      {
729         // Set the parent's height to include this tree. If the parent
730         // already has a tree that is taller than the one we're attaching
731         // then the parent's height remains unchanged
732         int rightHeight = (parent->right ? parent->right->m_height : 0);
733         int leftHeight = (parent->left ? parent->left->m_height : 0);
734
735         // The height of the tallest branch + 1
736         parent->m_height = maxInt(rightHeight, leftHeight) + 1;
737
738         // Also set the balance factor
739         parent->m_balanceFactor = rightHeight - leftHeight;
740      }
741753   }
742754
743755
r242452r242453
746758   {
747759      // Set our new left node
748760      left = l;
749
750      // Set the height and balance factor
751      int rightHeight = (right ? right->m_height : 0);
752      int leftHeight = (left ? left->m_height : 0);
753
754      m_height = maxInt(rightHeight, leftHeight) + 1;
755      m_balanceFactor = (right ? right->m_height : 0) - (left ? left->m_height : 0);
756761   }
757762
758763
r242452r242453
761766   {
762767      // Set our new right node
763768      right = r;
764
765      // Set the height and balance factor
766      int rightHeight = (right ? right->m_height : 0);
767      int leftHeight = (left ? left->m_height : 0);
768
769      m_height = maxInt(rightHeight, leftHeight) + 1;
770      m_balanceFactor = (right ? right->m_height : 0) - (left ? left->m_height : 0);
771769   }
772770
773771
r242452r242453
799797   // Calculates all of the heights for this node and its ancestors -- O(log n).
800798   void calcHeights()
801799   {
802      // Calculate our own height -- O(1)
803      m_height = maxInt(left ? left->m_height : 0, right ? right->m_height : 0) + 1;
800      int rightHeight = (right ? right->m_height : 0);
801      int leftHeight = (left ? left->m_height : 0);
804802
805      // And our parent's height (and recurse) -- O(log n)
803      // Calculate our own height and balance factor -- O(1)
804      m_height = maxInt(rightHeight, leftHeight) + 1;
805      m_balanceFactor = (rightHeight - leftHeight);
806
807      // And our parent's height and balance factor (and recurse) -- O(log n)
806808      if (parent)
807809      {
808810         parent->calcHeights();


Previous 199869 Revisions Next


© 1997-2024 The MAME Team