Example #1
0
        internal ImHashMap <K, V> Remove(int hash, K key, bool ignoreKey = false)
        {
            if (Height == 0)
            {
                return(this);
            }

            ImHashMap <K, V> result;

            if (hash == Hash) // found node
            {
                if (ignoreKey || Equals(Key, key))
                {
                    if (!ignoreKey && Conflicts != null)
                    {
                        return(ReplaceRemovedWithConflicted());
                    }

                    if (Height == 1) // remove node
                    {
                        return(Empty);
                    }

                    if (Right.IsEmpty)
                    {
                        result = Left;
                    }
                    else if (Left.IsEmpty)
                    {
                        result = Right;
                    }
                    else
                    {
                        // we have two children, so remove the next highest node and replace this node with it.
                        var successor = Right;
                        while (!successor.Left.IsEmpty)
                        {
                            successor = successor.Left;
                        }
                        result = new ImHashMap <K, V>(successor._data,
                                                      Left, Right.Remove(successor.Hash, default, true));