Example #1
0
        // Add one to every suitable root, call order if it pass on avl rules
        private void Cal_heigh()//logn
        {
            if (Mum != null)
            {
                int left_h;
                if (Mum.Left == null)
                {
                    left_h = 0;
                }
                else
                {
                    left_h = Mum.Left.Height;
                }

                int right_h;
                if (Mum.Right == null)
                {
                    right_h = 0;
                }
                else
                {
                    right_h = Mum.Right.Height;
                }

                int diff = right_h - left_h;

                //Console.WriteLine("Cal_heigh {0}, {1}, {2}", Mum.Left, Mum, Mum.Right);
                //Console.WriteLine("{0}, {1}, {2}", left_h, Mum.Height, right_h);
                //Console.Read();

                AVL X = Mum;

                if (diff == 2)
                {
                    X           = Mum.Order(Mum.Left);
                    Mum.Height -= 1;        //for setup to order
                    AVL mum_of_mum = Mum.Mum;
                    if (mum_of_mum != null) //set the mum of mum pointers
                    {
                        if (mum_of_mum.Right == Mum)
                        {
                            X = Mum.Order(Mum.Right);
                            mum_of_mum.Right = X;
                        }
                        else
                        {
                            X = Mum.Order(Mum.Right);
                            mum_of_mum.Left = X;
                        }
                    }
                    else
                    {
                        X = Mum.Order(Mum.Right);
                    }
                }
                else
                {
                    if (diff == -2)
                    {
                        Mum.Height -= 1;        //for setup to order
                        AVL mum_of_mum = Mum.Mum;
                        if (mum_of_mum != null) //set the mum of mum pointers
                        {
                            if (mum_of_mum.Right == Mum)
                            {
                                X = Mum.Order(Mum.Left);
                                mum_of_mum.Right = X;
                            }
                            else
                            {
                                X = Mum.Order(Mum.Left);
                                mum_of_mum.Left = X;
                            }
                        }
                        else
                        {
                            X = Mum.Order(Mum.Left);
                        }
                    }
                    else
                    {
                        if (diff > 0)
                        {
                            Mum.Height = right_h + 1;
                        }
                        else
                        {
                            Mum.Height = left_h + 1;
                        }
                    }
                }


                //Console.WriteLine("{0}, {1}, {2}", left_h, Height, right_h);
                //Console.Read();
                X.Cal_heigh();
            }
        }