diff --git a/contents/tree_traversal/code/csharp/Program.cs b/contents/tree_traversal/code/csharp/Program.cs index 4308794dd..0fc35c547 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 f581008d7..28df47c91 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; @@ -11,23 +10,23 @@ public class Tree public Tree(int depthCount, int childrenCount) { - this.Id = depthCount; + Id = 1; if (depthCount > 0) { for (int i = 0; i < childrenCount; i++) - this._children.Add(new Tree(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 +60,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.Write(tree.Id + " "); - DFSRecursiveInorderBinary(tree._children[1]); + case 2: + DFSRecursiveInorderBinary(tree._children[0]); + Console.Write(tree.Id + " "); + DFSRecursiveInorderBinary(tree._children[1]); + break; + case 1: + DFSRecursiveInorderBinary(tree._children[0]); + Console.Write(tree.Id + " "); + break; + case 0: + Console.Write(tree.Id + " "); + break; + default: + throw new Exception("Not binary tree!"); } - else - Console.Write(tree.Id + " "); } } diff --git a/contents/tree_traversal/tree_traversal.md b/contents/tree_traversal/tree_traversal.md index 94b9086a8..4b6c5b762 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" %} @@ -56,7 +56,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" %} @@ -112,7 +112,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" %} @@ -163,7 +163,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:59-83, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:55-73, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} @@ -223,7 +223,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:85-98, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:75-93, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %} @@ -276,7 +276,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:100-113, lang:"csharp"](code/csharp/Tree.cs) {% sample lang="c" %} [import:95-113, lang:"c"](code/c/tree_traversal.c) {% sample lang="java" %}