using System.Collections.ObjectModel;
private NodeList<T> neighbors = null;
public Node(T data): this (data, null)
public Node(T data, NodeList<T> neighbors)
this.neighbors = neighbors;
protected NodeList<T> Neighbors
public class NodeList<T> : Collection<Node<T>>
public NodeList(): base ()
public NodeList(int initialSize)
for (int i = 0; i < initialSize; i++)
base.Items.Add(default (Node<T>));
public Node<T> FindByValue(T value)
foreach (Node<T> node in Items)
if (node.Value.Equals(value))
public class BinaryTreeNode<T> : Node<T>
public BinaryTreeNode(): base ()
public BinaryTreeNode(T data): base (data, null)
public BinaryTreeNode(T data, BinaryTreeNode<T> left, BinaryTreeNode<T> right)
NodeList<T> children = new NodeList<T>(2);
base.Neighbors = children;
public BinaryTreeNode<T> Left
if (base.Neighbors == null)
return (BinaryTreeNode<T>)base.Neighbors[0];
if (base.Neighbors == null)
base.Neighbors = new NodeList<T>(2);
base.Neighbors[0] = value;
public BinaryTreeNode<T> Right
if (base.Neighbors == null)
return (BinaryTreeNode<T>)base.Neighbors[1];
if (base.Neighbors == null)
base.Neighbors = new NodeList<T>(2);
base.Neighbors[1] = value;
public class BinaryTree<T>
private BinaryTreeNode<T> root;
public virtual void Clear()
public BinaryTreeNode<T> Root
public void PreorderTraversal(Node current)
Console.WriteLine(current.Value);
PreorderTraversal(current.Left);
PreorderTraversal(current.Right);
public void InorderTraversal(Node current)
InorderTraversal(current.Left);
Console.WriteLine(current.Value);
InorderTraversal(current.Right);
public bool Contains(T data)
BinaryTreeNode<T> current = root;
result = comparer.Compare(current.Value, data);
public virtual void Add(T data)
BinaryTreeNode<T> n = new BinaryTreeNode<T>(data);
BinaryTreeNode<T> current = root, parent = null;
result = comparer.Compare(current.Value, data);
result = comparer.Compare(parent.Value, data);
public bool Remove(T data)
BinaryTreeNode<T> current = root, parent = null;
int result = comparer.Compare(current.Value, data);
result = comparer.Compare(current.Value, data);
if (current.Right == null)
result = comparer.Compare(parent.Value, current.Value);
parent.Left = current.Left;
parent.Right = current.Left;
else if (current.Right.Left == null)
current.Right.Left = current.Left;
result = comparer.Compare(parent.Value, current.Value);
parent.Left = current.Right;
parent.Right = current.Right;
BinaryTreeNode<T> leftmost = current.Right.Left, lmParent = current.Right;
while (leftmost.Left != null)
leftmost = leftmost.Left;
lmParent.Left = leftmost.Right;
leftmost.Left = current.Left;
leftmost.Right = current.Right;
result = comparer.Compare(parent.Value, current.Value);
public static void Main()
Console.WriteLine("Hello World");