/// <summary> /// Depth-First-Search algorithm. /// </summary> /// <param name="root">the root of the tree to be /// traversed</param> /// <param name="spaces">the spaces used for /// representation of the parent-child relation</param> private void PrintDFS(TreeNode <T> root, string spaces) { if (Root == null) { return; } Console.WriteLine(spaces + root.Value); TreeNode <T> child = null; for (int i = 0; i < root.ChildrenCount; i++) { child = root.GetChild(i); PrintDFS(child, spaces + " "); } }
/// <summary> /// Breadth-First-Search algorithm. /// </summary> /// <param name="root">the root of the tree to be /// traversed</param> private void PrintBFS(TreeNode <T> root) { if (Root == null) { return; } Queue <TreeNode <T> > visitedNodes = new Queue <TreeNode <T> >(); visitedNodes.Enqueue(Root); while (visitedNodes.Count > 0) { TreeNode <T> currentNode = visitedNodes.Dequeue(); Console.WriteLine(currentNode.Value); for (int i = 0; i < currentNode.ChildrenCount; i++) { visitedNodes.Enqueue(currentNode.GetChild(i)); } } }