0% found this document useful (0 votes)
7 views2 pages

C# Generic Binary Tree Implementation

The document defines a generic BinaryTree class in C# that implements the IEnumerable interface, allowing for the storage and traversal of elements that implement IComparable. It includes methods for adding items to the tree and enumerating through the tree's values in sorted order. Additionally, a static method is provided to create a BinaryTree from an array of items.

Uploaded by

irontmpiewwy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

C# Generic Binary Tree Implementation

The document defines a generic BinaryTree class in C# that implements the IEnumerable interface, allowing for the storage and traversal of elements that implement IComparable. It includes methods for adding items to the tree and enumerating through the tree's values in sorted order. Additionally, a static method is provided to create a BinaryTree from an array of items.

Uploaded by

irontmpiewwy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using System;

using [Link];
using [Link];

namespace [Link]
{
public class BinaryTree<T> : IEnumerable<T>
where T : IComparable
{
public BinaryTree<T> Left { get; private set; }
public BinaryTree<T> Right { get; private set; }
public T Value { get; private set; }

private int treeSize;

public BinaryTree()
{
}

private BinaryTree(T item, BinaryTree<T> left = null, BinaryTree<T> right =


null)
{
Value = item;
Left = left;
Right = right;
treeSize = 1 + (left?.treeSize ?? 0) + (right?.treeSize ?? 0);
}

public void Add(T item)


{
var currentNode = this;
if (treeSize == 0)
{
[Link] = item;
treeSize++;
return;
}
while (true)
{
[Link]++;
if (TryInsertItem(ref currentNode, item))
{
break;
}
}
}

public IEnumerator<T> GetEnumerator()


{
return Enumerate(this).GetEnumerator();
}

IEnumerator [Link]()
{
return GetEnumerator();
}

private static IEnumerable<T> Enumerate(BinaryTree<T> node)


{
if (node is null || [Link] == 0)
{
yield break;
}

foreach (var item in Enumerate([Link]))


{
yield return item;
}

yield return [Link];

foreach (var item in Enumerate([Link]))


{
yield return item;
}
}

private static bool TryInsertItem(ref BinaryTree<T> node, T item)


{
if ([Link]([Link]) <= 0)
{
if ([Link] is null)
{
[Link] = new BinaryTree<T>(item);
return true;
}
node = [Link];
}
else
{
if ([Link] is null)
{
[Link] = new BinaryTree<T>(item);
return true;
}
node = [Link];
}

return false;
}
}

public static class BinaryTree


{
public static BinaryTree<T> Create<T>(params T[] items) where T :
IComparable
{
var bst = new BinaryTree<T>();
foreach (var item in items)
{
[Link](item);
}

return bst;
}
}
}

You might also like