Skip to content

Update ImmutableBinaryTree.cs#45

Merged
ewdlop merged 1 commit intomainfrom
ewdlop-patch-35
May 4, 2025
Merged

Update ImmutableBinaryTree.cs#45
ewdlop merged 1 commit intomainfrom
ewdlop-patch-35

Conversation

@ewdlop
Copy link
Copy Markdown
Owner

@ewdlop ewdlop commented May 4, 2025

public class ImmutableBinaryTree<T>
{
    public T Value { get; }
    public ImmutableBinaryTree<T>? Left { get; }
    public ImmutableBinaryTree<T>? Right { get; }

    public ImmutableBinaryTree(T value, ImmutableBinaryTree<T>? left = null, ImmutableBinaryTree<T>? right = null)
    {
        Value = value;
        Left = left;
        Right = right;
    }

    public ImmutableBinaryTree<T> AddLeft(T newValue)
    {
        return new ImmutableBinaryTree<T>(Value, new ImmutableBinaryTree<T>(newValue), Right);
    }

    public ImmutableBinaryTree<T> AddRight(T newValue)
    {
        return new ImmutableBinaryTree<T>(Value, Left, new ImmutableBinaryTree<T>(newValue));
    }

    public ImmutableBinaryTree<T> ReplaceLeft(ImmutableBinaryTree<T>? newLeft)
    {
        return new ImmutableBinaryTree<T>(Value, newLeft, Right);
    }

    public ImmutableBinaryTree<T> ReplaceRight(ImmutableBinaryTree<T>? newRight)
    {
        return new ImmutableBinaryTree<T>(Value, Left, newRight);
    }

     // Replace the root value but keep structure
    public ImmutableBinaryTree<T> ReplaceRootValue(T newValue)
    {
        return new ImmutableBinaryTree<T>(newValue, Left, Right);
    }

    // Example Insert pattern: replace root on match
    public ImmutableBinaryTree<T> InsertAtRoot(T newValue)
    {
        return ReplaceRootValue(newValue);
    }
}

```csharp
public class ImmutableBinaryTree<T>
{
    public T Value { get; }
    public ImmutableBinaryTree<T>? Left { get; }
    public ImmutableBinaryTree<T>? Right { get; }

    public ImmutableBinaryTree(T value, ImmutableBinaryTree<T>? left = null, ImmutableBinaryTree<T>? right = null)
    {
        Value = value;
        Left = left;
        Right = right;
    }

    public ImmutableBinaryTree<T> AddLeft(T newValue)
    {
        return new ImmutableBinaryTree<T>(Value, new ImmutableBinaryTree<T>(newValue), Right);
    }

    public ImmutableBinaryTree<T> AddRight(T newValue)
    {
        return new ImmutableBinaryTree<T>(Value, Left, new ImmutableBinaryTree<T>(newValue));
    }

    public ImmutableBinaryTree<T> ReplaceLeft(ImmutableBinaryTree<T>? newLeft)
    {
        return new ImmutableBinaryTree<T>(Value, newLeft, Right);
    }

    public ImmutableBinaryTree<T> ReplaceRight(ImmutableBinaryTree<T>? newRight)
    {
        return new ImmutableBinaryTree<T>(Value, Left, newRight);
    }

     // Replace the root value but keep structure
    public ImmutableBinaryTree<T> ReplaceRootValue(T newValue)
    {
        return new ImmutableBinaryTree<T>(newValue, Left, Right);
    }

    // Example Insert pattern: replace root on match
    public ImmutableBinaryTree<T> InsertAtRoot(T newValue)
    {
        return ReplaceRootValue(newValue);
    }
}
```
@ewdlop ewdlop self-assigned this May 4, 2025
@ewdlop ewdlop merged commit b1a7c5c into main May 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant