Поиск в ширину в двоичном бинарном дереве - C#
Формулировка задачи:
Ребят привет. У меня есть класс двоичного бинарного дерева, представленный как связь узлов. Помогите написать алгоритм поиска в ширину(breadth-first search) через очередь и функцию подсчитывающую высоту дерева...
class Uzel<TNode> : IComparable<TNode> where TNode : IComparable<TNode>
{
public Uzel<TNode> Left { get; set; }
public Uzel<TNode> Right { get; set; }
public TNode Value { get; private set; }
public Uzel(TNode value)
{
Value = value;
}
public int CompareTo(TNode other)
{
return Value.CompareTo(other);
}
public int CompareNode(Uzel<TNode> other)
{
return Value.CompareTo(other.Value);
}
}
public class Tree<T> : IEnumerable<T> where T : IComparable<T>
{
private Uzel<T> top;
}Решение задачи: «Поиск в ширину в двоичном бинарном дереве»
textual
Листинг программы
public IEnumerator<T> BFS()
{
if (top == null)
yield break;
Queue<Uzel<T>> queue = new Queue<Uzel<T>>();
Uzel<T> current = top;
queue.Enqueue(current);
while (queue.Count != 0)
{
Uzel<T> index = queue.Dequeue();
Console.WriteLine(index);
yield return current.Value;
if (current.Left != null)
{
queue.Enqueue(current.Left);
}
if (current.Right != null)
{
queue.Enqueue(current.Right);
}
}