private string ShowQueue(Col.Queue <Node <T> > queue) { var arr = queue.ToArray(); string res = ""; foreach (var item in arr) { res += item.ToString() + " "; } return(res); }
public string BFS() { visited = new List <Node <T> >(); var protocol = ""; int currentBFSNumber = 1; var currentNode = ParentNode; currentNode.Number = currentBFSNumber; visited.Add(currentNode); var queue = new Col.Queue <Node <T> >(); queue.Enqueue(currentNode); protocol += $"{currentNode.Data} {currentNode.Number} {ShowQueue(queue)}\n"; while (queue.Count != 0) { currentNode = queue.Peek(); if (currentNode.RelatedNodes.All(x => visited.Contains(x))) { queue.Dequeue(); protocol += $"- - {ShowQueue(queue)}\n"; } else { foreach (var node in currentNode.RelatedNodes) { if (!visited.Contains(node)) { visited.Add(node); node.Number = ++currentBFSNumber; queue.Enqueue(node); protocol += $"{node.Data} {node.Number} {ShowQueue(queue)}\n"; break; } } } } return(protocol); }