Значения элементов бинарного дерева в строку - C#
Формулировка задачи:
Здравствуйте, возникла надобность вывести элементы бинарного дерева в строку. Следующий код не работает(Выводит только первый элемент):Подскажите пожалуйста где ошибка. Далее привожу полный код:
public override string ToString()
{
string s = "";
if (Left != null) Left.ToString();
s += " " +Value.ToString()+" ";
if (Right != null) Right.ToString();
return s;
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BinaryTreeMVP.BL
{
public class BinaryTree<T> where T : IComparable<T>
{
public T Value;
public BinaryTree<T> Right;
public BinaryTree<T> Left;
BinaryTree<T> Parent;
public BinaryTree()
{
Value = default(T);
Right = null;
Left = null;
Parent = null;
}
public void Add(T value)
{
if (this.Value.CompareTo(default(T))== 0)
{
this.Value = value;
}
else
{
if (this.Value.CompareTo(value) > 0)
{
if (Left == null)
{
Left = new BinaryTree<T>();
Left.Value = value;
Left.Parent = this;
return;
}
this.Left.Add(value);
}
else
{
if (Right == null)
{
Right = new BinaryTree<T>();
Right.Value = value;
Right.Parent = this;
return;
}
this.Right.Add(value);
}
}
}
public BinaryTree<T> Find(T value)
{
if (this.Value.CompareTo(value) == 0) return this;
if (this.Value.CompareTo(value) > 0) return this.Left.Find(value);
return this.Right.Find(value);
}
public override string ToString()
{
string s = "";
if (Left != null) Left.ToString();
s += " " +Value.ToString()+" ";
if (Right != null) Right.ToString();
return s;
}
}
}Решение задачи: «Значения элементов бинарного дерева в строку»
textual
Листинг программы
if (Left != null) s += " " +Left.ToString()+" "; s += " " +Value.ToString()+" "; if (Right != null) s += " " +Right.ToString() + " "; return s;