Skip to content

Added root parameter #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions chapters/tree_traversal/code/cs/Tree/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ class Program
{
static void Main(string[] args)
{
Node root;
Console.WriteLine("TreeTraversal");
var tree = new Tree(3, 3);
Console.WriteLine("StartDFSRecursive:");
tree.StartDFSRecursive();
tree.StartDFSRecursive(root);
Console.WriteLine("DFSStack:");
tree.DFSStack();
tree.DFSStack(root);
Console.WriteLine("DFSQueue:");
tree.BFSQueue();
tree.BFSQueue(root);
}
}
}
14 changes: 6 additions & 8 deletions chapters/tree_traversal/code/cs/Tree/Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ private class Node
public int Id { get; set; }
}

private Node root;

public Tree(int depthCount, int childrenCount)
public Tree(int depthCount, int childrenCount, Node root)
{
CreateTree(depthCount, childrenCount);
CreateTree(depthCount, childrenCount, root);
}

public void CreateTree(int depthCount, int childrenCount)
public void CreateTree(int depthCount, int childrenCount, Node root)
{
root = new Node
{
Expand All @@ -28,12 +26,12 @@ public void CreateTree(int depthCount, int childrenCount)
CreateAllChildren(root, depthCount, childrenCount);
}

public void StartDFSRecursive()
public void StartDFSRecursive(Node root)
{
DFSRecursive(root);
}

public void DFSStack()
public void DFSStack(Node root)
{
var stack = new Stack<Node>();
stack.Push(root);
Expand All @@ -51,7 +49,7 @@ public void DFSStack()
}
}

public void BFSQueue()
public void BFSQueue(Node root)
{
var queue = new Queue<Node>();
queue.Enqueue(root);
Expand Down