using System.Collections.Generic;
public static void Main()
Tree<int> root = new Tree<int>(15,
new Tree<int>(1, null, null), null),
new Tree<int>(16,new Tree<int>(28,null, new Tree<int>(5, null, null)), new Tree<int>(10, null, null)), null));
string depth_result = "";
root.Depth(root, ref depth_result, true);
Console.WriteLine(depth_result);
string across_result = "";
root.Width(root, ref across_result, true);
Console.WriteLine(across_result);
public T Value { get; private set; }
public Tree<T> Left { get; private set; }
public Tree<T> Right { get; private set; }
public Tree(T value, Tree<T> left, Tree<T> right)
public void Depth(Tree<T> node, ref string res, bool detailed)
res += " Value " + node.Value.ToString() + Environment.NewLine;
res += node.Value.ToString() + " ";
if (detailed) res += "Left subtree" + Environment.NewLine;
Depth(node.Left, ref res, detailed);
if (detailed) res += "Right subtree" + Environment.NewLine;
Depth(node.Right, ref res, detailed);
else if (detailed) res += " Value is null" + Environment.NewLine;
public void Width(Tree<T> node, ref string res, bool detailed)
var queue = new Queue<Tree<T>>();
if (detailed) res += "Put value in the queue " + node.Value.ToString() + Environment.NewLine; queue.Enqueue(node);
if (queue.Peek().Left != null)
if (detailed) res += "Put value in the queue " + queue.Peek().Left.Value.ToString() + " from left subtree" + Environment.NewLine;
queue.Enqueue(queue.Peek().Left);
if (queue.Peek().Right != null)
if (detailed) res += "Put value in the queue " + queue.Peek().Right.Value.ToString() + " from right subtree" + Environment.NewLine;
queue.Enqueue(queue.Peek().Right);
if (detailed) res += "Extract a value from queue: " + queue.Peek().Value.ToString() + Environment.NewLine;
else res += queue.Peek().Value.ToString() + " ";