Exemplo n.º 1
0
        // Suppose t != null.
        void RemoveTarget(AvlSetNode <KeyValuePair <TKey, TValue> > t)
        {
            if (t.TypedLeft == null || t.TypedRight == null)
            {
                var c = t.TypedLeft ?? t.TypedRight;

                if (t.TypedParent == null)
                {
                    Root = c;
                }
                else if (t.TypedParent.TypedLeft == t)
                {
                    t.TypedParent.TypedLeft = c;
                }
                else
                {
                    t.TypedParent.TypedRight = c;
                }

                t.TypedParent?.UpdateHeight(true);
            }
            else
            {
                var t2 = t.SearchNextNode() as AvlSetNode <KeyValuePair <TKey, TValue> >;
                t.Key = t2.Key;
                RemoveTarget(t2);
            }
        }
Exemplo n.º 2
0
        AvlSetNode <KeyValuePair <TKey, TValue> > Add(AvlSetNode <KeyValuePair <TKey, TValue> > node, TKey key, TValue value)
        {
            if (node == null)
            {
                ++Count;
                return(new AvlSetNode <KeyValuePair <TKey, TValue> > {
                    Key = new KeyValuePair <TKey, TValue>(key, value)
                });
            }

            var d = compare(key, node.Key.Key);

            if (d == 0)
            {
                return(node);
            }

            if (d < 0)
            {
                node.TypedLeft = Add(node.TypedLeft, key, value);
            }
            else
            {
                node.TypedRight = Add(node.TypedRight, key, value);
            }

            var lrh = node.LeftHeight - node.RightHeight;

            if (lrh > 2 || lrh == 2 && node.TypedLeft.LeftHeight >= node.TypedLeft.RightHeight)
            {
                node = node.RotateToRight() as AvlSetNode <KeyValuePair <TKey, TValue> >;
                node.TypedRight.UpdateHeight();
            }
            else if (lrh < -2 || lrh == -2 && node.TypedRight.LeftHeight <= node.TypedRight.RightHeight)
            {
                node = node.RotateToLeft() as AvlSetNode <KeyValuePair <TKey, TValue> >;
                node.TypedLeft.UpdateHeight();
            }

            node.UpdateHeight();
            return(node);
        }
Exemplo n.º 3
0
        AvlSetNode <T> Add(AvlSetNode <T> node, T item)
        {
            if (node == null)
            {
                ++Count;
                return(new AvlSetNode <T> {
                    Key = item
                });
            }

            var d = compare(item, node.Key);

            if (d == 0)
            {
                return(node);
            }

            if (d < 0)
            {
                node.TypedLeft = Add(node.TypedLeft, item);
            }
            else
            {
                node.TypedRight = Add(node.TypedRight, item);
            }

            var lrh = node.LeftHeight - node.RightHeight;

            if (lrh > 2 || lrh == 2 && node.TypedLeft.LeftHeight >= node.TypedLeft.RightHeight)
            {
                node = node.RotateToRight() as AvlSetNode <T>;
                node.TypedRight.UpdateHeight();
            }
            else if (lrh < -2 || lrh == -2 && node.TypedRight.LeftHeight <= node.TypedRight.RightHeight)
            {
                node = node.RotateToLeft() as AvlSetNode <T>;
                node.TypedLeft.UpdateHeight();
            }

            node.UpdateHeight();
            return(node);
        }