Tests/IMToolsTests.cs
/// <summary>Removes or updates value for specified key, or does nothing if key is not found.
/// Based on Eric Lippert http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx </summary>
/// <param name="key">Key to look for.</param> 
/// <returns>New tree with removed or updated value.</returns>
Removes or updates value for specified key, or does nothing if key is not found.
Removes or updates value for specified key or does nothing if key is not found .
[MethodImpl((MethodImplOptions)256)]
        public ImMap<V> Remove(int key)
        {
            if (Height == 0)
                return this;
            if (key == Key) 
            {
                if (Height == 1) 
                    return Empty;
                if (Right.Height == 0)
                    return Left;
                if (Left.Height == 0)
                    return Right;
                var successor = Right;
                while (successor.Left.Height != 0)
                    successor = successor.Left;
                return new ImMap<V>(successor.Key, successor.Value,
                    Left, Right.Remove(successor.Key));
            }
            return key < Key
                ? Balance(Key, Value, Left.Remove(key), Right)
                : Balance(Key, Value, Left, Right.Remove(key));
        }
[ MethodImpl ( ( MethodImplOptions ) 256 ) ] public ImMap < V > Remove ( int key ) { if ( Height == 0 ) return this ; if ( key == Key ) { if ( Height == 1 ) return Empty ; if ( Right . Height == 0 ) return Left ; if ( Left . Height == 0 ) return Right ; var successor = Right ; while ( successor . Left . Height != 0 ) successor = successor . Left ; return new ImMap < V > ( successor . Key , successor . Value , Left , Right . Remove ( successor . Key ) ) ; } return key < Key ? Balance ( Key , Value , Left . Remove ( key ) , Right ) : Balance ( Key , Value , Left , Right . Remove ( key ) ) ; }
Removes or updates value for specified key, or does nothing if key is not found.
Removes or updates value for specified key or does nothing if key is not found .
// we've found the node to remove // remove the leaf node // if we have the on child remaining then just return it // we have two children, // so remove the next highest node and replace this node with it. // remove the node and balance the new tree
New tree with removed or updated value.
New tree with removed or updated value .
key
int
Key to look for.
Key to look for .