示例#1
0
        // Suppose t != null.
        void RemoveTarget(AvlMapNode <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 AvlMapNode <TKey, TValue>;
                t.Key   = t2.Key;
                t.Value = t2.Value;
                RemoveTarget(t2);
            }
        }
示例#2
0
        AvlMapNode <TKey, TValue> Add(AvlMapNode <TKey, TValue> node, TKey key, TValue value)
        {
            if (node == null)
            {
                ++Count;
                return(new AvlMapNode <TKey, TValue> {
                    Key = key, Value = value
                });
            }

            var d = compare(key, node.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 AvlMapNode <TKey, TValue>;
                node.TypedRight.UpdateHeight();
            }
            else if (lrh < -2 || lrh == -2 && node.TypedRight.LeftHeight <= node.TypedRight.RightHeight)
            {
                node = node.RotateToLeft() as AvlMapNode <TKey, TValue>;
                node.TypedLeft.UpdateHeight();
            }

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