Skip to content

Commit 4a81890

Browse files
authored
c# tree traversal: fixed index out of range issue (#894)
1 parent cfcfccc commit 4a81890

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

contents/tree_traversal/code/csharp/Program.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// submitted by Julian Schacher (jspp)
21
using System;
32

43
namespace TreeTraversal

contents/tree_traversal/code/csharp/Tree.cs

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// submitted by Julian Schacher (jspp)
21
using System;
32
using System.Collections.Generic;
43

@@ -11,23 +10,23 @@ public class Tree
1110

1211
public Tree(int depthCount, int childrenCount)
1312
{
14-
this.Id = depthCount;
13+
Id = 1;
1514

1615
if (depthCount > 0)
1716
{
1817
for (int i = 0; i < childrenCount; i++)
19-
this._children.Add(new Tree(depthCount - 1, childrenCount));
18+
_children.Add(new Tree(Id * 10 + i + 1, depthCount - 1, childrenCount));
2019
}
2120
}
2221

2322
private Tree(int id, int depthCount, int childrenCount)
2423
{
25-
this.Id = id;
24+
Id = id;
2625

2726
if (!(depthCount <= 1))
2827
{
2928
for (int i = 0; i < childrenCount; i++)
30-
this._children.Add(new Tree(this.Id * 10 + i + 1, depthCount - 1, childrenCount));
29+
_children.Add(new Tree(Id * 10 + i + 1, depthCount - 1, childrenCount));
3130
}
3231
}
3332

@@ -61,20 +60,25 @@ public void DFSRecursiveInorderBinary()
6160
{
6261
DFSRecursiveInorderBinary(this);
6362

64-
// This assumes only 2 children
6563
void DFSRecursiveInorderBinary(Tree tree)
6664
{
67-
if (tree._children.Count > 2)
68-
throw new Exception("Not binary tree!");
69-
70-
if (tree._children.Count > 0)
65+
switch (tree._children.Count)
7166
{
72-
DFSRecursiveInorderBinary(tree._children[0]);
73-
Console.Write(tree.Id + " ");
74-
DFSRecursiveInorderBinary(tree._children[1]);
67+
case 2:
68+
DFSRecursiveInorderBinary(tree._children[0]);
69+
Console.Write(tree.Id + " ");
70+
DFSRecursiveInorderBinary(tree._children[1]);
71+
break;
72+
case 1:
73+
DFSRecursiveInorderBinary(tree._children[0]);
74+
Console.Write(tree.Id + " ");
75+
break;
76+
case 0:
77+
Console.Write(tree.Id + " ");
78+
break;
79+
default:
80+
throw new Exception("Not binary tree!");
7581
}
76-
else
77-
Console.Write(tree.Id + " ");
7882
}
7983
}
8084

contents/tree_traversal/tree_traversal.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Trees are naturally recursive data structures, and because of this, we cannot ac
88
{% sample lang="cpp" %}
99
[import:12-15, lang:"cpp"](code/c++/tree_example.cpp)
1010
{% sample lang="cs" %}
11-
[import:7-11, lang:"csharp"](code/csharp/Tree.cs)
11+
[import:6-10, lang:"csharp"](code/csharp/Tree.cs)
1212
{% sample lang="c" %}
1313
[import:7-11, lang:"c"](code/c/tree_traversal.c)
1414
{% sample lang="java" %}
@@ -56,7 +56,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
5656
{% sample lang="cpp" %}
5757
[import:17-24, lang:"cpp"](code/c++/tree_example.cpp)
5858
{% sample lang="cs" %}
59-
[import:34-45, lang:"csharp"](code/csharp/Tree.cs)
59+
[import:33-44, lang:"csharp"](code/csharp/Tree.cs)
6060
{% sample lang="c" %}
6161
[import:37-45, lang:"c"](code/c/tree_traversal.c)
6262
{% sample lang="java" %}
@@ -112,7 +112,7 @@ Now, in this case the first element searched through is still the root of the tr
112112
{% sample lang="cpp" %}
113113
[import:26-31, lang:"cpp"](code/c++/tree_example.cpp)
114114
{% sample lang="cs" %}
115-
[import:47-58, lang:"csharp"](code/csharp/Tree.cs)
115+
[import:46-57, lang:"csharp"](code/csharp/Tree.cs)
116116
{% sample lang="c" %}
117117
[import:47-53, lang:"c"](code/c/tree_traversal.c)
118118
{% 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
163163
{% sample lang="cpp" %}
164164
[import:34-52 lang:"cpp"](code/c++/tree_example.cpp)
165165
{% sample lang="cs" %}
166-
[import:60-79, lang:"csharp"](code/csharp/Tree.cs)
166+
[import:59-83, lang:"csharp"](code/csharp/Tree.cs)
167167
{% sample lang="c" %}
168168
[import:55-73, lang:"c"](code/c/tree_traversal.c)
169169
{% sample lang="java" %}
@@ -223,7 +223,7 @@ In code, it looks like this:
223223
{% sample lang="cpp" %}
224224
[import:55-70, lang:"cpp"](code/c++/tree_example.cpp)
225225
{% sample lang="cs" %}
226-
[import:81-94, lang:"csharp"](code/csharp/Tree.cs)
226+
[import:85-98, lang:"csharp"](code/csharp/Tree.cs)
227227
{% sample lang="c" %}
228228
[import:75-93, lang:"c"](code/c/tree_traversal.c)
229229
{% sample lang="java" %}
@@ -276,7 +276,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
276276
{% sample lang="cpp" %}
277277
[import:73-86, lang:"cpp"](code/c++/tree_example.cpp)
278278
{% sample lang="cs" %}
279-
[import:96-109, lang:"csharp"](code/csharp/Tree.cs)
279+
[import:100-113, lang:"csharp"](code/csharp/Tree.cs)
280280
{% sample lang="c" %}
281281
[import:95-113, lang:"c"](code/c/tree_traversal.c)
282282
{% sample lang="java" %}

0 commit comments

Comments
 (0)