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(K), true)); } } else if (Conflicts != null) { return(TryRemoveConflicted(key)); } else { return(this); // if key is not matching and no conflicts to lookup - just return } } else if (hash < Hash) { result = new ImHashMap <K, V>(_data, Left.Remove(hash, key, ignoreKey), Right); } else { result = new ImHashMap <K, V>(_data, Left, Right.Remove(hash, key, ignoreKey)); } if (result.Height == 1) { return(result); } return(result.KeepBalance()); }