public static string DepthSearch(TreeNode tree) { // Build the stack of the tree search Stack<Record> stack = new Stack<Record>(); // Start from the root stack.Push(new Record(tree, 0)); string output = ""; while (stack.Count > 0) { Record topRecord = stack.Peek(); TreeNode current = topRecord.Node; int childrenChecked = topRecord.ChildrenChecked; // Process the node when the first time discovered if (0 == childrenChecked) { ProcessNode(current, ref output); } // Remove the node from stack when no more children to process if (childrenChecked == current.Children.Count) { stack.Pop(); continue; } // Put the next not processed child on the stack topRecord.ChildrenChecked += 1; stack.Push(new Record( current.Children[childrenChecked], 0 )); } return output; }
public static void Main() { TreeNode tree = new TreeNode(1); TreeNode node = new TreeNode(2); tree.AddChildren(node, new TreeNode(3), new TreeNode(4)); node.AddChildren(new TreeNode(5), new TreeNode(6)); Console.WriteLine("Breadth Search:"); BreadthSearch(tree); Console.WriteLine(); Console.WriteLine("Depth Search:"); DepthSearch(tree); }
public void Initialize() { // Tree with 6 nodes // Nodes are enumerated according to breadth search order tree6Nodes = new TreeNode(1); tree6Nodes.AddChildren(new TreeNode(2, new TreeNode(5), new TreeNode(6)), new TreeNode(3), new TreeNode(4)); // Tree with 12 nodes // Nodes are enumerated according to depth search order tree12Nodes = new TreeNode(1); TreeNode auxNode1 = new TreeNode(2, new TreeNode(3), new TreeNode(4, new TreeNode(5), new TreeNode(6))); TreeNode auxNode2 = new TreeNode(7, new TreeNode(8, new TreeNode(9))); TreeNode auxNode3 = new TreeNode(10, new TreeNode(11), new TreeNode(12)); tree12Nodes.AddChildren(auxNode1, auxNode2, auxNode3); }
public static string BreadthSearch(TreeNode tree) { // Build the queue of the tree search Queue<TreeNode> queue = new Queue<TreeNode>(); // Start from the root queue.Enqueue(tree); string output = ""; while (queue.Count > 0) { TreeNode current = queue.Dequeue(); ProcessNode(current, ref output); // Add all children of the current node to the queue foreach(TreeNode node in current.Children) { queue.Enqueue(node); } } return output; }
public Record(TreeNode node, int childrenChecked) { Node = node; ChildrenChecked = childrenChecked; }
public static void ProcessNode(TreeNode node, ref string output) { Console.Write(node.Value + " "); output += node.Value + " "; }