示例#1
0
            /// <summary>
            /// Called after removing a node - can handle more than 2 or -2 balances on self
            /// </summary>
            internal static void BalanceSelfBubbleUp(AVLNode node, object[] HashMap, int index)
            {
                while (node != null)
                {
                    node._depthL = node.left == null ? 0 : (GetMaxDepth(node.left) + 1);
                    node._depthR = node.right == null ? 0 : (GetMaxDepth(node.right) + 1);

                    if (Math.Abs(node.balance) > 1)
                    {
                        node.Rebalance(HashMap, index);
                    }
                    else
                    {
                        node = node.head;
                    }
                }
            }
            /// <summary>
            /// Called after removing a node - can handle more than 2 or -2 balances on self
            /// </summary>
            internal static void BalanceSelfBubbleUp(AVLNode node, QueuedAVLTree <TKey, TValue> tree)
            {
                while (node != null)
                {
                    node._depthL = node.left == null ? 0 : (GetMaxDepth(node.left) + 1);
                    node._depthR = node.right == null ? 0 : (GetMaxDepth(node.right) + 1);

                    if (Math.Abs(node.balance) > 1)
                    {
                        node.Rebalance(tree);
                    }
                    else
                    {
                        node = node.head;
                    }
                }
            }
示例#3
0
            private void Rebalance(object[] HashMap, int index)
            {
                if (Math.Abs(balance) > 2)
                {
                    if (balance < -2)
                    {
                        left.Rebalance(HashMap, index);
                    }
                    else
                    {
                        right.Rebalance(HashMap, index);
                    }
                }

                if (balance > 1)
                {
                    //          5_2             |              7
                    //      2        7_1        |        5          8
                    //            6       8_1   |     2      6          9
                    //                       9  |

                    //            5             |              5
                    //     2            6       |       3           6
                    //        3                 |    2      4
                    //         4                |

                    //          5               |          5                |           7
                    //    2           8         |     2        7            |      5          8
                    //             7     9      |            6    8         |   2     6          9
                    //            6             |                   9       |

                    if (right.balance > 0)
                    {
                        RotateLeft(right, HashMap, index);
                    }
                    else
                    {
                        RotateRight(right.left, HashMap, index);

                        RotateLeft(right, HashMap, index);
                    }
                }
                else if (balance < -1)
                {
                    //              5           |            5
                    //         4          9     |       4          8
                    //                 8        |                7   9
                    //                7         |

                    //            5             |              5            |           3
                    //     1            6       |       3           6       |      1           5
                    //  0     3                 |    1     4                |   0           4      6
                    //          4               |  0                        |

                    if (left.balance < 0)
                    {
                        RotateRight(left, HashMap, index);
                    }
                    else
                    {
                        RotateLeft(left.right, HashMap, index);

                        RotateRight(left, HashMap, index);
                    }
                }

#if TEST
                checkNodeSelf(this);
#endif
            }