예제 #1
0
            void Reconstruct(bool recursive)
            {
                Count = 1;

                int leftLevel  = 0;
                int rightLevel = 0;

                if (LeftHand != null)
                {
                    leftLevel = LeftHand.Level;
                    Count    += LeftHand.Count;
                }

                if (RightHand != null)
                {
                    rightLevel = RightHand.Level;
                    Count     += RightHand.Count;
                }

                if (leftLevel - rightLevel > 1)
                {
                    int leftNext  = LeftHand.LeftHand == null ? 0 : LeftHand.LeftHand.Level;
                    int rightNext = LeftHand.RightHand == null ? 0 : LeftHand.RightHand.Level;

                    if (leftNext >= rightNext)
                    {
                        LeftHand.Elevate();
                        Reconstruct(true);
                    }
                    else
                    {
                        AVLTreeNode pivot = LeftHand.RightHand;

                        pivot.Elevate();
                        pivot.Elevate();

                        pivot.LeftHand.Reconstruct(false);
                        pivot.RightHand.Reconstruct(true);
                    }
                }
                else if (rightLevel - leftLevel > 1)
                {
                    int leftNext  = RightHand.LeftHand == null ? 0 : RightHand.LeftHand.Level;
                    int rightNext = RightHand.RightHand == null ? 0 : RightHand.RightHand.Level;

                    if (rightNext >= leftNext)
                    {
                        RightHand.Elevate();
                        Reconstruct(true);
                    }
                    else
                    {
                        var pivot = RightHand.LeftHand;

                        pivot.Elevate();
                        pivot.Elevate();

                        pivot.LeftHand.Reconstruct(false);
                        pivot.RightHand.Reconstruct(true);
                    }
                }
                else
                {
                    Level = Math.Max(leftLevel, rightLevel) + 1;

                    if (Parent != null && recursive)
                    {
                        Parent.Reconstruct(true);
                    }
                }
            }