/// <summary> /// Returns a tree /// A /// / \ /// B C /// / \ / \ /// D E F G /// /// </summary> /// <returns></returns> static Tree BuildTree() { var tree = new Tree(); var node = new Node("A", new Node("B", "D", "E"), new Node("C", "F", "G")); tree.Root = node; return tree; }
public void BFS(Tree tree, Action<Node> visit) { if (tree?.Root == null) return; var q = new Queue<Node>(); q.Enqueue(tree.Root); while(q.Count > 0) { var node = q.Dequeue(); visit(node); if (node.Left != null) q.Enqueue(node.Left); if (node.Right != null) q.Enqueue(node.Right); } }