Skip to content

Commit 71ff692

Browse files
authored
Enhance the code of TreeTraversal and EuclideanAlgorithm for C# (#120)
* Add all additional methods from TreeTraversalMdAdditional to TreeTraversal. Fix incorrect ConsoleWriteLine. Use local functions. Create constructor for Node, which takes an id. Remove unnecessary curly braces. Remove redundant CreateTree method and just put the code into the constructor. Remove unnecessary comment. Remove the now redundant TreeTraversalMdAdditional. Sort the methods so that they have the same order as in the text. * Delete redundant EuclideanAlgorithmMdAdditional. * Make EuclideanAlgorithm non-static. * Make file be "ASCII text" instead of "UTF-8 Unicode (with BOM) text, with CRLF line terminators". * Provide explanation for commented lines of code.
1 parent 773c32e commit 71ff692

File tree

11 files changed

+91
-241
lines changed

11 files changed

+91
-241
lines changed

chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm/EuclideanAlgorithm.cs chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace EuclideanAlgorithm
55
{
6-
public static class EuclideanAlgorithm
6+
public class EuclideanAlgorithm
77
{
8-
public static int EuclidSub(int a, int b)
8+
public int EuclidSub(int a, int b)
99
{
1010
// Math.Abs for negative number support
1111
a = Math.Abs(a);
@@ -22,7 +22,7 @@ public static int EuclidSub(int a, int b)
2222
return a;
2323
}
2424

25-
public static int EuclidMod(int a, int b)
25+
public int EuclidMod(int a, int b)
2626
{
2727
// Math.Abs for negative number support
2828
a = Math.Abs(a);

chapters/euclidean_algorithm/code/cs/EuclideanAlgorithmMdAdditional/EuclideanAlgorithmMdAdditional.cs

-31
This file was deleted.

chapters/euclidean_algorithm/code/cs/EuclideanAlgorithmMdAdditional/Program.cs

-18
This file was deleted.

chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm/Program.cs chapters/euclidean_algorithm/code/cs/Program.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ class Program
88
static void Main(string[] args)
99
{
1010
Console.WriteLine("EuclideanAlgorithm");
11-
int check = EuclideanAlgorithm.EuclidMod(64 * 67, 64 * 81);
12-
int check2 = EuclideanAlgorithm.EuclidSub(128 * 12, 128 * 77);
11+
var euclideanAlgorithm = new EuclideanAlgorithm();
12+
int check = euclideanAlgorithm.EuclidMod(64 * 67, 64 * 81);
13+
int check2 = euclideanAlgorithm.EuclidSub(128 * 12, 128 * 77);
1314

1415
Console.WriteLine(check);
1516
Console.WriteLine(check2);

chapters/euclidean_algorithm/euclidean.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
88
{% sample lang="c" %}
99
[import:17-30, lang="c_cpp"](code/c/euclidean_example.c)
1010
{% sample lang="cs" %}
11-
[import:6-17, lang="csharp"](code/cs/EuclideanAlgorithmMdAdditional/EuclideanAlgorithmMdAdditional.cs)
11+
[import:8-23, lang="csharp"](code/cs/EuclideanAlgorithm.cs)
1212
{% sample lang="clj" %}
1313
[import:2-8, lang="clojure"](code/clojure/euclidean_example.clj)
1414
{% sample lang="cpp" %}
@@ -39,7 +39,7 @@ Modern implementations, though, often use the modulus operator (%) like so
3939
{% sample lang="c" %}
4040
[import:4-16, lang="c_cpp"](code/c/euclidean_example.c)
4141
{% sample lang="cs" %}
42-
[import:19-29, lang="csharp"](code/cs/EuclideanAlgorithmMdAdditional/EuclideanAlgorithmMdAdditional.cs)
42+
[import:25-39, lang="csharp"](code/cs/EuclideanAlgorithm.cs)
4343
{% sample lang="clj" %}
4444
[import:9-13, lang="clojure"](code/clojure/euclidean_example.clj)
4545
{% sample lang="cpp" %}
@@ -75,9 +75,9 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
7575
{% sample lang="cs" %}
7676
### C# #
7777
EuclideanAlgorithm.cs
78-
[import, lang="csharp"](code/cs/EuclideanAlgorithm/EuclideanAlgorithm.cs)
78+
[import, lang="csharp"](code/cs/EuclideanAlgorithm.cs)
7979
Program.cs
80-
[import, lang="csharp"](code/cs/EuclideanAlgorithm/Program.cs)
80+
[import, lang="csharp"](code/cs/Program.cs)
8181
{% sample lang="clj" %}
8282
### Clojure
8383
[import 2-20, lang="clojure"](code/clojure/euclidean_example.clj)
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// submitted by Julian Schacher (jspp)
2+
using System;
3+
4+
namespace TreeTraversal
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
Console.WriteLine("TreeTraversal");
11+
var tree = new Tree(3, 3);
12+
Console.WriteLine("DFSRecursive:");
13+
tree.DFSRecursive();
14+
Console.WriteLine("DFSStack:");
15+
tree.DFSStack();
16+
Console.WriteLine("BFSQueue:");
17+
tree.BFSQueue();
18+
Console.WriteLine("DFSRecursivePostorder");
19+
tree.DFSRecursivePostorder();
20+
// 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.
21+
// Console.WriteLine("DFSRecursiveInorder (fail)");
22+
// tree.DFSRecursiveInorderBinary();
23+
tree = new Tree(3, 2);
24+
Console.WriteLine("DFSRecursiveInorder (succeed)");
25+
tree.DFSRecursiveInorderBinary();
26+
}
27+
}
28+
}

chapters/tree_traversal/code/cs/Tree/Tree.cs chapters/tree_traversal/code/cs/Tree.cs

+45-26
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,63 @@ private class Node
1010
{
1111
public List<Node> Children { get; set; } = new List<Node>();
1212
public int Id { get; set; }
13+
14+
public Node(int id) => this.Id = id;
1315
}
1416

1517
private Node root;
1618

1719
public Tree(int depthCount, int childrenCount)
1820
{
19-
CreateTree(depthCount, childrenCount);
21+
root = new Node(1);
22+
CreateAllChildren(root, depthCount, childrenCount);
2023
}
2124

22-
public void CreateTree(int depthCount, int childrenCount)
25+
public void DFSRecursive()
2326
{
24-
root = new Node
27+
DFSRecursive(root);
28+
29+
void DFSRecursive(Node node)
2530
{
26-
Id = 1
27-
};
28-
CreateAllChildren(root, depthCount, childrenCount);
31+
Console.WriteLine(node.Id);
32+
33+
foreach (var c in node.Children)
34+
DFSRecursive(c);
35+
}
2936
}
3037

31-
public void StartDFSRecursive()
38+
public void DFSRecursivePostorder()
3239
{
33-
DFSRecursive(root);
40+
DFSRecursivePostorder(root);
41+
42+
void DFSRecursivePostorder(Node node)
43+
{
44+
foreach (var c in node.Children)
45+
DFSRecursivePostorder(c);
46+
47+
Console.WriteLine(node.Id);
48+
}
49+
}
50+
51+
public void DFSRecursiveInorderBinary()
52+
{
53+
DFSRecursiveInorderBinary(root);
54+
55+
// This assumes only 2 children
56+
void DFSRecursiveInorderBinary(Node node)
57+
{
58+
if (node.Children.Count > 2)
59+
throw new Exception("Not binary tree!");
60+
61+
if (node.Children.Count > 0)
62+
{
63+
DFSRecursiveInorderBinary(node.Children[0]);
64+
Console.WriteLine(node.Id);
65+
DFSRecursiveInorderBinary(node.Children[1]);
66+
}
67+
else
68+
Console.WriteLine(node.Id);
69+
}
3470
}
3571

3672
public void DFSStack()
@@ -45,9 +81,7 @@ public void DFSStack()
4581
temp = stack.Pop();
4682

4783
foreach (var c in temp.Children)
48-
{
4984
stack.Push(c);
50-
}
5185
}
5286
}
5387

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

6599
foreach (var c in temp.Children)
66-
{
67100
queue.Enqueue(c);
68-
}
69101
}
70102
}
71103

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

77109
for (int i = 0; i < childrenCount; i++)
78110
{
79-
node.Children.Add(new Node
80-
{
81-
Id = node.Id * 10 + i + 1
82-
});
111+
node.Children.Add(new Node(node.Id * 10 + i + 1));
83112
CreateAllChildren(node.Children[i], rowCount - 1, childrenCount);
84113
}
85114
}
86-
87-
private void DFSRecursive(Node node)
88-
{
89-
Console.WriteLine(node.Id);
90-
91-
foreach (var c in node.Children)
92-
{
93-
DFSRecursive(c);
94-
}
95-
}
96115
}
97116
}

chapters/tree_traversal/code/cs/Tree/Program.cs

-20
This file was deleted.

chapters/tree_traversal/code/cs/TreeMdAdditional/Program.cs

-23
This file was deleted.

0 commit comments

Comments
 (0)