From bccfb32f89ca11aace0a27cdbe581f57deec97e4 Mon Sep 17 00:00:00 2001 From: Henrik Christensen Date: Fri, 22 Oct 2021 06:31:28 +0200 Subject: [PATCH 1/4] fixed index out of range issue --- contents/tree_traversal/code/csharp/Tree.cs | 33 ++++++++++++--------- contents/tree_traversal/tree_traversal.md | 6 ++-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/contents/tree_traversal/code/csharp/Tree.cs b/contents/tree_traversal/code/csharp/Tree.cs index e38be26fc..55f9636ff 100644 --- a/contents/tree_traversal/code/csharp/Tree.cs +++ b/contents/tree_traversal/code/csharp/Tree.cs @@ -11,23 +11,23 @@ public class Tree public Tree(int depthCount, int childrenCount) { - this.Id = 1; + Id = 1; if (!(depthCount <= 1)) { for (int i = 0; i < childrenCount; i++) - this._children.Add(new Tree(this.Id * 10 + i + 1, depthCount - 1, childrenCount)); + _children.Add(new Tree(Id * 10 + i + 1, depthCount - 1, childrenCount)); } } private Tree(int id, int depthCount, int childrenCount) { - this.Id = id; + 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)); + _children.Add(new Tree(Id * 10 + i + 1, depthCount - 1, childrenCount)); } } @@ -61,20 +61,25 @@ public void DFSRecursiveInorderBinary() { DFSRecursiveInorderBinary(this); - // This assumes only 2 children void DFSRecursiveInorderBinary(Tree tree) { - if (tree._children.Count > 2) - throw new Exception("Not binary tree!"); - - if (tree._children.Count > 0) + switch (tree._children.Count) { - DFSRecursiveInorderBinary(tree._children[0]); - Console.WriteLine(tree.Id); - DFSRecursiveInorderBinary(tree._children[1]); + case 2: + DFSRecursiveInorderBinary(tree._children[0]); + Console.WriteLine(tree.Id); + DFSRecursiveInorderBinary(tree._children[1]); + break; + case 1: + DFSRecursiveInorderBinary(tree._children[0]); + Console.WriteLine(tree.Id); + break; + case 0: + Console.WriteLine(tree.Id); + break; + default: + throw new Exception("Not binary tree!"); } - else - Console.WriteLine(tree.Id); } } diff --git a/contents/tree_traversal/tree_traversal.md b/contents/tree_traversal/tree_traversal.md index e03f09652..f79cd34d7 100644 --- a/contents/tree_traversal/tree_traversal.md +++ b/contents/tree_traversal/tree_traversal.md @@ -157,7 +157,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t {% sample lang="cpp" %} [import:34-52 lang:"cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} -[import:60-79, lang:"csharp"](code/csharp/Tree.cs) +[import:60-84, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:55-73, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} @@ -215,7 +215,7 @@ In code, it looks like this: {% sample lang="cpp" %} [import:55-70, lang:"cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} -[import:81-94, lang:"csharp"](code/csharp/Tree.cs) +[import:86-99, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:75-93, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} @@ -266,7 +266,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can {% sample lang="cpp" %} [import:73-86, lang:"cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} -[import:96-109, lang:"csharp"](code/csharp/Tree.cs) +[import:101-114, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:95-113, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} From 234eb0bb6fd8c2c4696c754498fcaf98de78151d Mon Sep 17 00:00:00 2001 From: Henrik Christensen Date: Fri, 22 Oct 2021 08:30:13 +0200 Subject: [PATCH 2/4] removed submitted notes at the top of the files --- contents/tree_traversal/code/csharp/Program.cs | 1 - contents/tree_traversal/code/csharp/Tree.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/contents/tree_traversal/code/csharp/Program.cs b/contents/tree_traversal/code/csharp/Program.cs index aa2df0485..27e9c9708 100644 --- a/contents/tree_traversal/code/csharp/Program.cs +++ b/contents/tree_traversal/code/csharp/Program.cs @@ -1,4 +1,3 @@ -// submitted by Julian Schacher (jspp) using System; namespace TreeTraversal diff --git a/contents/tree_traversal/code/csharp/Tree.cs b/contents/tree_traversal/code/csharp/Tree.cs index 55f9636ff..45c88797e 100644 --- a/contents/tree_traversal/code/csharp/Tree.cs +++ b/contents/tree_traversal/code/csharp/Tree.cs @@ -1,4 +1,3 @@ -// submitted by Julian Schacher (jspp) using System; using System.Collections.Generic; From a545dbd545f00c81e32bc4b6054ee1444c9357ca Mon Sep 17 00:00:00 2001 From: Henrik Christensen Date: Fri, 22 Oct 2021 08:42:02 +0200 Subject: [PATCH 3/4] updated import lines --- contents/tree_traversal/tree_traversal.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contents/tree_traversal/tree_traversal.md b/contents/tree_traversal/tree_traversal.md index f79cd34d7..cf7530488 100644 --- a/contents/tree_traversal/tree_traversal.md +++ b/contents/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:12-15, lang:"cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} -[import:7-11, lang:"csharp"](code/csharp/Tree.cs) +[import:6-10, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:7-11, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} @@ -54,7 +54,7 @@ Because of this, the most straightforward way to traverse the tree might be recu {% sample lang="cpp" %} [import:17-24, lang:"cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} -[import:34-45, lang:"csharp"](code/csharp/Tree.cs) +[import:33-44, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:37-45, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} @@ -108,7 +108,7 @@ Now, in this case the first element searched through is still the root of the tr {% sample lang="cpp" %} [import:26-31, lang:"cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} -[import:47-58, lang:"csharp"](code/csharp/Tree.cs) +[import:46-57, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:47-53, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} @@ -157,7 +157,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t {% sample lang="cpp" %} [import:34-52 lang:"cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} -[import:60-84, lang:"csharp"](code/csharp/Tree.cs) +[import:59-83, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:55-73, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} @@ -215,7 +215,7 @@ In code, it looks like this: {% sample lang="cpp" %} [import:55-70, lang:"cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} -[import:86-99, lang:"csharp"](code/csharp/Tree.cs) +[import:85-98, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:75-93, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} @@ -266,7 +266,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can {% sample lang="cpp" %} [import:73-86, lang:"cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} -[import:101-114, lang:"csharp"](code/csharp/Tree.cs) +[import:100-113, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:95-113, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} From ad93d5d3c42b355d5c8a6becee5508cb732a513a Mon Sep 17 00:00:00 2001 From: Henrik Christensen Date: Mon, 25 Oct 2021 17:46:01 +0200 Subject: [PATCH 4/4] WriteLine -> Write as mentioned in comment --- contents/tree_traversal/code/csharp/Tree.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/tree_traversal/code/csharp/Tree.cs b/contents/tree_traversal/code/csharp/Tree.cs index e4e62dd8c..28df47c91 100644 --- a/contents/tree_traversal/code/csharp/Tree.cs +++ b/contents/tree_traversal/code/csharp/Tree.cs @@ -66,15 +66,15 @@ void DFSRecursiveInorderBinary(Tree tree) { case 2: DFSRecursiveInorderBinary(tree._children[0]); - Console.WriteLine(tree.Id + " "); + Console.Write(tree.Id + " "); DFSRecursiveInorderBinary(tree._children[1]); break; case 1: DFSRecursiveInorderBinary(tree._children[0]); - Console.WriteLine(tree.Id + " "); + Console.Write(tree.Id + " "); break; case 0: - Console.WriteLine(tree.Id + " "); + Console.Write(tree.Id + " "); break; default: throw new Exception("Not binary tree!");