Skip to content

Enhance the code of TreeTraversal and EuclideanAlgorithm for C# #120

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 5 commits into from
Jun 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace EuclideanAlgorithm
{
public static class EuclideanAlgorithm
public class EuclideanAlgorithm
{
public static int EuclidSub(int a, int b)
public int EuclidSub(int a, int b)
{
// Math.Abs for negative number support
a = Math.Abs(a);
Expand All @@ -22,7 +22,7 @@ public static int EuclidSub(int a, int b)
return a;
}

public static int EuclidMod(int a, int b)
public int EuclidMod(int a, int b)
{
// Math.Abs for negative number support
a = Math.Abs(a);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class Program
static void Main(string[] args)
{
Console.WriteLine("EuclideanAlgorithm");
int check = EuclideanAlgorithm.EuclidMod(64 * 67, 64 * 81);
int check2 = EuclideanAlgorithm.EuclidSub(128 * 12, 128 * 77);
var euclideanAlgorithm = new EuclideanAlgorithm();
int check = euclideanAlgorithm.EuclidMod(64 * 67, 64 * 81);
int check2 = euclideanAlgorithm.EuclidSub(128 * 12, 128 * 77);

Console.WriteLine(check);
Console.WriteLine(check2);
Expand Down
8 changes: 4 additions & 4 deletions chapters/euclidean_algorithm/euclidean.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
{% sample lang="c" %}
[import:17-30, lang="c_cpp"](code/c/euclidean_example.c)
{% sample lang="cs" %}
[import:6-17, lang="csharp"](code/cs/EuclideanAlgorithmMdAdditional/EuclideanAlgorithmMdAdditional.cs)
[import:8-23, lang="csharp"](code/cs/EuclideanAlgorithm.cs)
{% sample lang="clj" %}
[import:2-8, lang="clojure"](code/clojure/euclidean_example.clj)
{% sample lang="cpp" %}
Expand Down Expand Up @@ -39,7 +39,7 @@ Modern implementations, though, often use the modulus operator (%) like so
{% sample lang="c" %}
[import:4-16, lang="c_cpp"](code/c/euclidean_example.c)
{% sample lang="cs" %}
[import:19-29, lang="csharp"](code/cs/EuclideanAlgorithmMdAdditional/EuclideanAlgorithmMdAdditional.cs)
[import:25-39, lang="csharp"](code/cs/EuclideanAlgorithm.cs)
{% sample lang="clj" %}
[import:9-13, lang="clojure"](code/clojure/euclidean_example.clj)
{% sample lang="cpp" %}
Expand Down Expand Up @@ -75,9 +75,9 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
{% sample lang="cs" %}
### C# #
EuclideanAlgorithm.cs
[import, lang="csharp"](code/cs/EuclideanAlgorithm/EuclideanAlgorithm.cs)
[import, lang="csharp"](code/cs/EuclideanAlgorithm.cs)
Program.cs
[import, lang="csharp"](code/cs/EuclideanAlgorithm/Program.cs)
[import, lang="csharp"](code/cs/Program.cs)
{% sample lang="clj" %}
### Clojure
[import 2-20, lang="clojure"](code/clojure/euclidean_example.clj)
Expand Down
28 changes: 28 additions & 0 deletions chapters/tree_traversal/code/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// submitted by Julian Schacher (jspp)
using System;

namespace TreeTraversal
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("TreeTraversal");
var tree = new Tree(3, 3);
Console.WriteLine("DFSRecursive:");
tree.DFSRecursive();
Console.WriteLine("DFSStack:");
tree.DFSStack();
Console.WriteLine("BFSQueue:");
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();
tree = new Tree(3, 2);
Console.WriteLine("DFSRecursiveInorder (succeed)");
tree.DFSRecursiveInorderBinary();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,63 @@ private class Node
{
public List<Node> Children { get; set; } = new List<Node>();
public int Id { get; set; }

public Node(int id) => this.Id = id;
}

private Node root;

public Tree(int depthCount, int childrenCount)
{
CreateTree(depthCount, childrenCount);
root = new Node(1);
CreateAllChildren(root, depthCount, childrenCount);
}

public void CreateTree(int depthCount, int childrenCount)
public void DFSRecursive()
{
root = new Node
DFSRecursive(root);

void DFSRecursive(Node node)
{
Id = 1
};
CreateAllChildren(root, depthCount, childrenCount);
Console.WriteLine(node.Id);

foreach (var c in node.Children)
DFSRecursive(c);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a C# expert, but I'm pretty sure that omitting brackets for loops and conditions is dangerous no matter which language you're using.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gustorn What do you think about that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be dangerous, sure, but C#'s bracket style kinda encourages it. Having a single-line statement stretch into 3 eats up a lot of vertical space. I don't really mind either way but as long as it's a short, single line I think omitting the parens is fine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shall allow it, then.

}
}

public void StartDFSRecursive()
public void DFSRecursivePostorder()
{
DFSRecursive(root);
DFSRecursivePostorder(root);

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

Console.WriteLine(node.Id);
}
}

public void DFSRecursiveInorderBinary()
{
DFSRecursiveInorderBinary(root);

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

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

public void DFSStack()
Expand All @@ -45,9 +81,7 @@ public void DFSStack()
temp = stack.Pop();

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

Expand All @@ -63,9 +97,7 @@ public void BFSQueue()
temp = queue.Dequeue();

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

Expand All @@ -76,22 +108,9 @@ private void CreateAllChildren(Node node, int rowCount, int childrenCount)

for (int i = 0; i < childrenCount; i++)
{
node.Children.Add(new Node
{
Id = node.Id * 10 + i + 1
});
node.Children.Add(new Node(node.Id * 10 + i + 1));
CreateAllChildren(node.Children[i], rowCount - 1, childrenCount);
}
}

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

foreach (var c in node.Children)
{
DFSRecursive(c);
}
}
}
}
20 changes: 0 additions & 20 deletions chapters/tree_traversal/code/cs/Tree/Program.cs

This file was deleted.

23 changes: 0 additions & 23 deletions chapters/tree_traversal/code/cs/TreeMdAdditional/Program.cs

This file was deleted.

Loading