Example #1
0
        private void Replace(Node x, Node n)
        {
            if (x.Equals(_root))
            {
                _root = n;

                if (n != null)
                {
                    n.SetParent(null);
                }
            }
            else
            {
                Set(x.GetParent(), From(x), n);
            }
        }
Example #2
0
        public Node Next(Node x)
        {
            if ((++_needCleanUp & 127) == 0)
            {
                x.rData.CleanUpCache();
            }

            Node r = x.GetRight();

            if (r != null)
            {
                x = r;

                Node l = x.GetLeft();

                while (l != null)
                {
                    if (Trace.StopEnabled)
                    {
                        Trace.Stop();
                    }

                    x = l;
                    l = x.GetLeft();
                }

                return(x);
            }

            Node ch = x;

            x = x.GetParent();

            while (x != null && ch.Equals(x.GetRight()))
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                ch = x;
                x  = x.GetParent();
            }

            return(x);
        }
Example #3
0
        private void Replace(Node x, Node n)
        {
            if (x.Equals(_root))
            {
                _root = n;

                if (n != null)
                {
                    n.SetParent(null);
                }
            }
            else
            {
                Set(x.GetParent(), From(x), n);
            }
        }
Example #4
0
        public void Insert(Node i)
        {
            object[] data = i.GetData();
            Node     n = _root, x = n;
            bool     way     = true;
            int      compare = -1;

            while (true)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                if (n == null)
                {
                    if (x == null)
                    {
                        _root = i;

                        return;
                    }

                    Set(x, way, i);

                    break;
                }

                x       = n;
                compare = CompareRow(data, x.GetData());

                Trace.Check(compare != 0, Trace.VIOLATION_OF_UNIQUE_INDEX);

                way = compare < 0;
                n   = Child(x, way);
            }

            while (true)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                int sign = way ? 1 : -1;

                switch (x.GetBalance() * sign)
                {
                case 1:
                    x.SetBalance(0);

                    return;

                case 0:
                    x.SetBalance(-sign);

                    break;

                case -1:
                    Node l = Child(x, way);

                    if (l.GetBalance() == -sign)
                    {
                        Replace(x, l);
                        Set(x, way, Child(l, !way));
                        Set(l, !way, x);
                        x.SetBalance(0);
                        l.SetBalance(0);
                    }
                    else
                    {
                        Node r = Child(l, !way);

                        Replace(x, r);
                        Set(l, !way, Child(r, way));
                        Set(r, way, l);
                        Set(x, way, Child(r, !way));
                        Set(r, !way, x);

                        int rb = r.GetBalance();

                        x.SetBalance((rb == -sign) ? sign : 0);
                        l.SetBalance((rb == sign) ? -sign : 0);
                        r.SetBalance(0);
                    }

                    return;
                }

                if (x.Equals(_root))
                {
                    return;
                }

                way = From(x);
                x   = x.GetParent();
            }
        }
Example #5
0
        private bool From(Node x)
        {
            if (x.Equals(_root))
            {
                return true;
            }

            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(x.GetParent() != null);
            }

            return x.Equals(x.GetParent().GetLeft());
        }