Skip to content

Update Tree Traversal C#. #209

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

Merged
merged 2 commits into from
Jul 4, 2018
Merged
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
1 change: 1 addition & 0 deletions chapters/tree_traversal/code/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static void Main(string[] args)
tree.BFSQueue();
Console.WriteLine("DFSRecursivePostorder");
tree.DFSRecursivePostorder();

// Uncommenting the following 2 lines will result in an exception thrown because at least one Node of the Tree has more than 2 children and therefor a DFSRecursiveInorderBinary doesn't work.
// Console.WriteLine("DFSRecursiveInorder (fail)");
// tree.DFSRecursiveInorderBinary();
Expand Down
91 changes: 43 additions & 48 deletions chapters/tree_traversal/code/cs/Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,111 +6,106 @@ namespace TreeTraversal
{
public class Tree
{
private class Node
public int Id { get; private set; }
private List<Tree> _children = new List<Tree>();

public Tree(int depthCount, int childrenCount)
{
public List<Node> Children { get; set; } = new List<Node>();
public int Id { get; set; }
this.Id = 1;

public Node(int id) => this.Id = id;
if (!(depthCount <= 1))
{
for (int i = 0; i < childrenCount; i++)
this._children.Add(new Tree(this.Id * 10 + i + 1, depthCount - 1, childrenCount));
}
}

private Node root;

public Tree(int depthCount, int childrenCount)
private Tree(int id, int depthCount, int childrenCount)
{
root = new Node(1);
CreateAllChildren(root, depthCount, childrenCount);
this.Id = id;

if (!(depthCount <= 1))
{
for (int i = 0; i < childrenCount; i++)
this._children.Add(new Tree(this.Id * 10 + i + 1, depthCount - 1, childrenCount));
}
}

public void DFSRecursive()
{
DFSRecursive(root);
DFSRecursive(this);

void DFSRecursive(Node node)
void DFSRecursive(Tree tree)
{
Console.WriteLine(node.Id);
Console.WriteLine(tree.Id);

foreach (var c in node.Children)
foreach (var c in tree._children)
DFSRecursive(c);
}
}

public void DFSRecursivePostorder()
{
DFSRecursivePostorder(root);
DFSRecursivePostorder(this);

void DFSRecursivePostorder(Node node)
void DFSRecursivePostorder(Tree tree)
{
foreach (var c in node.Children)
foreach (var c in tree._children)
DFSRecursivePostorder(c);

Console.WriteLine(node.Id);
Console.WriteLine(tree.Id);
}
}

public void DFSRecursiveInorderBinary()
{
DFSRecursiveInorderBinary(root);
DFSRecursiveInorderBinary(this);

// This assumes only 2 children
void DFSRecursiveInorderBinary(Node node)
void DFSRecursiveInorderBinary(Tree tree)
{
if (node.Children.Count > 2)
throw new Exception("Not binary tree!");
if (tree._children.Count > 2)
throw new Exception("Not binary tree!");

if (node.Children.Count > 0)
if (tree._children.Count > 0)
{
DFSRecursiveInorderBinary(node.Children[0]);
Console.WriteLine(node.Id);
DFSRecursiveInorderBinary(node.Children[1]);
DFSRecursiveInorderBinary(tree._children[0]);
Console.WriteLine(tree.Id);
DFSRecursiveInorderBinary(tree._children[1]);
}
else
Console.WriteLine(node.Id);
Console.WriteLine(tree.Id);
}
}

public void DFSStack()
{
var stack = new Stack<Node>();
stack.Push(root);
Node temp;
var stack = new Stack<Tree>();
stack.Push(this);

while (stack.Count != 0)
{
Console.WriteLine(stack.Peek().Id);
temp = stack.Pop();
var temp = stack.Pop();

foreach (var c in temp.Children)
foreach (var c in temp._children)
stack.Push(c);
}
}

public void BFSQueue()
{
var queue = new Queue<Node>();
queue.Enqueue(root);
Node temp;
var queue = new Queue<Tree>();
queue.Enqueue(this);

while (queue.Count != 0)
{
Console.WriteLine(queue.Peek().Id);
temp = queue.Dequeue();
var temp = queue.Dequeue();

foreach (var c in temp.Children)
foreach (var c in temp._children)
queue.Enqueue(c);
}
}

private void CreateAllChildren(Node node, int rowCount, int childrenCount)
{
if (rowCount <= 1)
return;

for (int i = 0; i < childrenCount; i++)
{
node.Children.Add(new Node(node.Id * 10 + i + 1));
CreateAllChildren(node.Children[i], rowCount - 1, childrenCount);
}
}
}
}
14 changes: 7 additions & 7 deletions chapters/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Trees are naturally recursive data structures, and because of this, we cannot ac
{% sample lang="cpp" %}
[import:15-18, lang:"c_cpp"](code/c++/tree_example.cpp)
{% sample lang="cs" %}
[import:9-15, lang:"csharp"](code/cs/Tree.cs)
[import:7-11, lang:"csharp"](code/cs/Tree.cs)
{% sample lang="c" %}
[import:7-11, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
Expand All @@ -35,7 +35,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
{% sample lang="cpp" %}
[import:20-27, lang:"c_cpp"](code/c++/tree_example.cpp)
{% sample lang="cs" %}
[import:25-36, lang:"csharp"](code/cs/Tree.cs)
[import:34-45, lang:"csharp"](code/cs/Tree.cs)
{% sample lang="c" %}
[import:37-45, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
Expand Down Expand Up @@ -71,7 +71,7 @@ Now, in this case the first element searched through is still the root of the tr
This has not been implemented in your chosen language, so here is the Julia code
[import:18-26, lang:"julia"](code/julia/Tree.jl)
{% sample lang="cs" %}
[import:38-49, lang:"csharp"](code/cs/Tree.cs)
[import:47-58, lang:"csharp"](code/cs/Tree.cs)
{% sample lang="c" %}
[import:47-53, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
Expand Down Expand Up @@ -103,7 +103,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
This has not been implemented in your chosen language, so here is the Julia code
[import:28-43, lang:"julia"](code/julia/Tree.jl)
{% sample lang="cs" %}
[import:51-70, lang:"csharp"](code/cs/Tree.cs)
[import:60-79, lang:"csharp"](code/cs/Tree.cs)
{% sample lang="c" %}
[import:55-73, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
Expand Down Expand Up @@ -144,7 +144,7 @@ In code, it looks like this:
{% sample lang="cpp" %}
[import:29-45, lang:"c_cpp"](code/c++/tree_example.cpp)
{% sample lang="cs" %}
[import:72-86, lang:"csharp"](code/cs/Tree.cs)
[import:81-94, lang:"csharp"](code/cs/Tree.cs)
{% sample lang="c" %}
[import:75-93, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
Expand Down Expand Up @@ -177,7 +177,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
{% sample lang="cpp" %}
[import:47-61, lang:"c_cpp"](code/c++/tree_example.cpp)
{% sample lang="cs" %}
[import:88-102, lang:"csharp"](code/cs/Tree.cs)
[import:96-109, lang:"csharp"](code/cs/Tree.cs)
{% sample lang="c" %}
[import:95-113, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
Expand Down Expand Up @@ -225,7 +225,7 @@ MainClass.java
### JavaScript
[import, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
### Python
### Python
[import, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
### Scratch
Expand Down