Simple implementation of generic BINARY TREE in C#

using System;
public class BinaryTree
{
	public BinaryTree(
		T value,
		BinaryTree leftNode = null,
		BinaryTree rightNode = null)
	{
		this.Value = value;
		this.LeftNode = leftNode;
		this.RightNode = rightNode;
	}

	public T Value { get; private set; }

	public BinaryTree LeftNode { get; private set; }

	public BinaryTree RightNode { get; private set; }

	public void EachPreOrder(Action action)
	{
		action(this.Value);

		if (this.LeftNode != null)
		{
			this.LeftNode.EachPreOrder(action);
		}

		if (this.RightNode != null)
		{
			this.RightNode.EachPreOrder(action);
		}
	}

	public void EachInOrder(Action action)
	{
		if (this.LeftNode != null)
		{
			this.LeftNode.EachPreOrder(action);
		}

		action(this.Value);

		if (this.RightNode != null)
		{
			this.RightNode.EachPreOrder(action);
		}
	}

	public void EachPostOrder(Action action)
	{
		if (this.LeftNode != null)
		{
			this.LeftNode.EachPreOrder(action);
		}

		if (this.RightNode != null)
		{
			this.RightNode.EachPreOrder(action);
		}
		
		action(this.Value);
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *