diff --git a/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm/EuclideanAlgorithm.cs b/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm.cs similarity index 83% rename from chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm/EuclideanAlgorithm.cs rename to chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm.cs index 605ee91c8..261e35396 100644 --- a/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm/EuclideanAlgorithm.cs +++ b/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm.cs @@ -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); @@ -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); diff --git a/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithmMdAdditional/EuclideanAlgorithmMdAdditional.cs b/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithmMdAdditional/EuclideanAlgorithmMdAdditional.cs deleted file mode 100644 index a33ffbd12..000000000 --- a/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithmMdAdditional/EuclideanAlgorithmMdAdditional.cs +++ /dev/null @@ -1,31 +0,0 @@ -// submitted by Julian Schacher (jspp) -namespace EuclideanAlgorithmMdAdditional -{ - public class EuclideanAlgorithmMdAdditional - { - public static int EuclidSub(int a, int b) - { - while (a != b) - { - if (a > b) - a = a - b; - else - b = b - a; - } - - return a; - } - - public static int EuclidMod(int a, int b) - { - while (b != 0) - { - var temp = b; - b = a % b; - a = temp; - } - - return a; - } - } -} diff --git a/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithmMdAdditional/Program.cs b/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithmMdAdditional/Program.cs deleted file mode 100644 index 6254361a9..000000000 --- a/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithmMdAdditional/Program.cs +++ /dev/null @@ -1,18 +0,0 @@ -// submitted by Julian Schacher (jspp) -using System; - -namespace EuclideanAlgorithmMdAdditional -{ - class Program - { - static void Main(string[] args) - { - Console.WriteLine("EuclideanAlgorithmMdAdditional"); - int checkMdAdditional = EuclideanAlgorithmMdAdditional.EuclidMod(64 * 67, 64 * 81); - int checkMdAdditional2 = EuclideanAlgorithmMdAdditional.EuclidSub(128 * 12, 128 * 77); - - Console.WriteLine(checkMdAdditional); - Console.WriteLine(checkMdAdditional2); - } - } -} diff --git a/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm/Program.cs b/chapters/euclidean_algorithm/code/cs/Program.cs similarity index 63% rename from chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm/Program.cs rename to chapters/euclidean_algorithm/code/cs/Program.cs index f88f4eaf6..edf1edfd4 100644 --- a/chapters/euclidean_algorithm/code/cs/EuclideanAlgorithm/Program.cs +++ b/chapters/euclidean_algorithm/code/cs/Program.cs @@ -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); diff --git a/chapters/euclidean_algorithm/euclidean.md b/chapters/euclidean_algorithm/euclidean.md index 3530cdcbf..bfa0dc432 100644 --- a/chapters/euclidean_algorithm/euclidean.md +++ b/chapters/euclidean_algorithm/euclidean.md @@ -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" %} @@ -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" %} @@ -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) diff --git a/chapters/tree_traversal/code/cs/Program.cs b/chapters/tree_traversal/code/cs/Program.cs new file mode 100644 index 000000000..c6e310efd --- /dev/null +++ b/chapters/tree_traversal/code/cs/Program.cs @@ -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(); + } + } +} diff --git a/chapters/tree_traversal/code/cs/Tree/Tree.cs b/chapters/tree_traversal/code/cs/Tree.cs similarity index 58% rename from chapters/tree_traversal/code/cs/Tree/Tree.cs rename to chapters/tree_traversal/code/cs/Tree.cs index 0c8bd2851..e481b2354 100644 --- a/chapters/tree_traversal/code/cs/Tree/Tree.cs +++ b/chapters/tree_traversal/code/cs/Tree.cs @@ -10,27 +10,63 @@ private class Node { public List Children { get; set; } = new List(); 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); + } } - 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() @@ -45,9 +81,7 @@ public void DFSStack() temp = stack.Pop(); foreach (var c in temp.Children) - { stack.Push(c); - } } } @@ -63,9 +97,7 @@ public void BFSQueue() temp = queue.Dequeue(); foreach (var c in temp.Children) - { queue.Enqueue(c); - } } } @@ -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); - } - } } } diff --git a/chapters/tree_traversal/code/cs/Tree/Program.cs b/chapters/tree_traversal/code/cs/Tree/Program.cs deleted file mode 100644 index 3f9569381..000000000 --- a/chapters/tree_traversal/code/cs/Tree/Program.cs +++ /dev/null @@ -1,20 +0,0 @@ -// 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("StartDFSRecursive:"); - tree.StartDFSRecursive(); - Console.WriteLine("DFSStack:"); - tree.DFSStack(); - Console.WriteLine("DFSQueue:"); - tree.BFSQueue(); - } - } -} diff --git a/chapters/tree_traversal/code/cs/TreeMdAdditional/Program.cs b/chapters/tree_traversal/code/cs/TreeMdAdditional/Program.cs deleted file mode 100644 index 7e621aa4b..000000000 --- a/chapters/tree_traversal/code/cs/TreeMdAdditional/Program.cs +++ /dev/null @@ -1,23 +0,0 @@ -// submitted by Julian Schacher (jspp) -using System; - -namespace TreeTraversalMdAdditional -{ - class Program - { - static void Main(string[] args) - { - Console.WriteLine("TreeTraversalMdAdditional"); - var treeMdAdditional = new TreeMdAdditional(3, 3); - Console.WriteLine("StartDFSRecursive"); - treeMdAdditional.StartDFSRecursive(); - Console.WriteLine("StartDFSRecursivePostorder"); - treeMdAdditional.StartDFSRecursivePostorder(); - // Console.WriteLine("StartDFSRecursiveInorder (fail)"); - // treeMdAdditional.StartDFSRecursiveInorderBinary(); - treeMdAdditional = new TreeMdAdditional(3, 2); - Console.WriteLine("StartDFSRecursiveInorder (succeed)"); - treeMdAdditional.StartDFSRecursiveInorderBinary(); - } - } -} diff --git a/chapters/tree_traversal/code/cs/TreeMdAdditional/TreeMdAdditional.cs b/chapters/tree_traversal/code/cs/TreeMdAdditional/TreeMdAdditional.cs deleted file mode 100644 index 022905cda..000000000 --- a/chapters/tree_traversal/code/cs/TreeMdAdditional/TreeMdAdditional.cs +++ /dev/null @@ -1,106 +0,0 @@ -// submitted by Julian Schacher (jspp) -using System; -using System.Collections.Generic; - -namespace TreeTraversalMdAdditional -{ - // This class recreates Tree and inlcudes changed or new methods needed for in-text-code. - // Therefor this file disregards coding standards and best practices. - public class TreeMdAdditional - { - public class Node - { - public List Children { get; set; } = new List(); - public int Id { get; set; } - } - - private Node root; - - public void CreateTree(int depthCount, int childrenCount) - { - root = new Node - { - Id = 1 - }; - CreateAllChildren(root, depthCount, childrenCount); - } - - public TreeMdAdditional(int depthCount, int childrenCount) - { - CreateTree(depthCount, childrenCount); - } - - public void StartDFSRecursive() - { - DFSRecursive(root); - } - - public void StartDFSRecursivePostorder() - { - DFSRecursivePostorder(root); - } - - public void StartDFSRecursiveInorderBinary() - { - DFSRecursiveInorderBinary(root); - } - - public void DFSRecursive(Node node) - { - // Here we are doing something... - Console.WriteLine(node.Id); - - foreach (var c in node.Children) - { - DFSRecursive(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 - { - Id = node.Id * 10 + i + 1 - }); - CreateAllChildren(node.Children[i], rowCount - 1, childrenCount); - } - } - - public void DFSRecursivePostorder(Node node) - { - foreach (var c in node.Children) - { - DFSRecursivePostorder(c); - } - - // Here we are doing something... - Console.WriteLine(node.Id); - } - - // This assumes only 2 children - public 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); - } - } - } -} \ No newline at end of file diff --git a/chapters/tree_traversal/tree_traversal.md b/chapters/tree_traversal/tree_traversal.md index 42dda492a..45a8c5ee6 100644 --- a/chapters/tree_traversal/tree_traversal.md +++ b/chapters/tree_traversal/tree_traversal.md @@ -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:11-15, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs) +[import:9-15, lang:"csharp"](code/cs/Tree.cs) {% sample lang="c" %} [import:7-11, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} @@ -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:48-57, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs) +[import:25-36, lang:"csharp"](code/cs/Tree.cs) {% sample lang="c" %} [import:37-45, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} @@ -72,7 +72,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:75-84, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs) +[import:38-49, lang:"csharp"](code/cs/Tree.cs) {% sample lang="c" %} [import:47-53, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} @@ -107,7 +107,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:86-104, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs) +[import:51-70, lang:"csharp"](code/cs/Tree.cs) {% sample lang="c" %} [import:55-73, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} @@ -151,7 +151,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:36-52, lang:"csharp"](code/cs/Tree/Tree.cs) +[import:72-86, lang:"csharp"](code/cs/Tree.cs) {% sample lang="c" %} [import:75-93, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} @@ -184,7 +184,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:54-70, lang:"csharp"](code/cs/Tree/Tree.cs) +[import:88-102, lang:"csharp"](code/cs/Tree.cs) {% sample lang="c" %} [import:95-113, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} @@ -213,9 +213,9 @@ This has not been implemented in your chosen language, so here is the Julia code {% sample lang="cs" %} ### C# # Tree.cs -[import, lang:"csharp"](code/cs/Tree/Tree.cs) +[import, lang:"csharp"](code/cs/Tree.cs) Program.cs -[import, lang:"csharp"](code/cs/Tree/Program.cs) +[import, lang:"csharp"](code/cs/Program.cs) {% sample lang="c" %} ### C utility.h