Skip to content

Commit bf5bc96

Browse files
authored
Merge pull request #44 from ewdlop/ewdlop-patch-34
Create ImmutableBinaryTree.cs
2 parents c9b263a + d32b0ea commit bf5bc96

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

C#/ChatGPT/ImmutableBinaryTree.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class ImmutableBinaryTree<T>
2+
{
3+
public T Value { get; }
4+
public ImmutableBinaryTree<T>? Left { get; }
5+
public ImmutableBinaryTree<T>? Right { get; }
6+
7+
public ImmutableBinaryTree(T value, ImmutableBinaryTree<T>? left = null, ImmutableBinaryTree<T>? right = null)
8+
{
9+
Value = value;
10+
Left = left;
11+
Right = right;
12+
}
13+
14+
public ImmutableBinaryTree<T> AddLeft(T newValue)
15+
{
16+
return new ImmutableBinaryTree<T>(Value, new ImmutableBinaryTree<T>(newValue), Right);
17+
}
18+
19+
public ImmutableBinaryTree<T> AddRight(T newValue)
20+
{
21+
return new ImmutableBinaryTree<T>(Value, Left, new ImmutableBinaryTree<T>(newValue));
22+
}
23+
24+
public ImmutableBinaryTree<T> ReplaceLeft(ImmutableBinaryTree<T>? newLeft)
25+
{
26+
return new ImmutableBinaryTree<T>(Value, newLeft, Right);
27+
}
28+
29+
public ImmutableBinaryTree<T> ReplaceRight(ImmutableBinaryTree<T>? newRight)
30+
{
31+
return new ImmutableBinaryTree<T>(Value, Left, newRight);
32+
}
33+
}

0 commit comments

Comments
 (0)