Skip to content

Commit bccfb32

Browse files
committed
fixed index out of range issue
1 parent 65599ee commit bccfb32

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

contents/tree_traversal/code/csharp/Tree.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public class Tree
1111

1212
public Tree(int depthCount, int childrenCount)
1313
{
14-
this.Id = 1;
14+
Id = 1;
1515

1616
if (!(depthCount <= 1))
1717
{
1818
for (int i = 0; i < childrenCount; i++)
19-
this._children.Add(new Tree(this.Id * 10 + i + 1, depthCount - 1, childrenCount));
19+
_children.Add(new Tree(Id * 10 + i + 1, depthCount - 1, childrenCount));
2020
}
2121
}
2222

2323
private Tree(int id, int depthCount, int childrenCount)
2424
{
25-
this.Id = id;
25+
Id = id;
2626

2727
if (!(depthCount <= 1))
2828
{
2929
for (int i = 0; i < childrenCount; i++)
30-
this._children.Add(new Tree(this.Id * 10 + i + 1, depthCount - 1, childrenCount));
30+
_children.Add(new Tree(Id * 10 + i + 1, depthCount - 1, childrenCount));
3131
}
3232
}
3333

@@ -61,20 +61,25 @@ public void DFSRecursiveInorderBinary()
6161
{
6262
DFSRecursiveInorderBinary(this);
6363

64-
// This assumes only 2 children
6564
void DFSRecursiveInorderBinary(Tree tree)
6665
{
67-
if (tree._children.Count > 2)
68-
throw new Exception("Not binary tree!");
69-
70-
if (tree._children.Count > 0)
66+
switch (tree._children.Count)
7167
{
72-
DFSRecursiveInorderBinary(tree._children[0]);
73-
Console.WriteLine(tree.Id);
74-
DFSRecursiveInorderBinary(tree._children[1]);
68+
case 2:
69+
DFSRecursiveInorderBinary(tree._children[0]);
70+
Console.WriteLine(tree.Id);
71+
DFSRecursiveInorderBinary(tree._children[1]);
72+
break;
73+
case 1:
74+
DFSRecursiveInorderBinary(tree._children[0]);
75+
Console.WriteLine(tree.Id);
76+
break;
77+
case 0:
78+
Console.WriteLine(tree.Id);
79+
break;
80+
default:
81+
throw new Exception("Not binary tree!");
7582
}
76-
else
77-
Console.WriteLine(tree.Id);
7883
}
7984
}
8085

contents/tree_traversal/tree_traversal.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
157157
{% sample lang="cpp" %}
158158
[import:34-52 lang:"cpp"](code/c++/tree_example.cpp)
159159
{% sample lang="cs" %}
160-
[import:60-79, lang:"csharp"](code/csharp/Tree.cs)
160+
[import:60-84, lang:"csharp"](code/csharp/Tree.cs)
161161
{% sample lang="c" %}
162162
[import:55-73, lang:"c"](code/c/tree_traversal.c)
163163
{% sample lang="java" %}
@@ -215,7 +215,7 @@ In code, it looks like this:
215215
{% sample lang="cpp" %}
216216
[import:55-70, lang:"cpp"](code/c++/tree_example.cpp)
217217
{% sample lang="cs" %}
218-
[import:81-94, lang:"csharp"](code/csharp/Tree.cs)
218+
[import:86-99, lang:"csharp"](code/csharp/Tree.cs)
219219
{% sample lang="c" %}
220220
[import:75-93, lang:"c"](code/c/tree_traversal.c)
221221
{% sample lang="java" %}
@@ -266,7 +266,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
266266
{% sample lang="cpp" %}
267267
[import:73-86, lang:"cpp"](code/c++/tree_example.cpp)
268268
{% sample lang="cs" %}
269-
[import:96-109, lang:"csharp"](code/csharp/Tree.cs)
269+
[import:101-114, lang:"csharp"](code/csharp/Tree.cs)
270270
{% sample lang="c" %}
271271
[import:95-113, lang:"c"](code/c/tree_traversal.c)
272272
{% sample lang="java" %}

0 commit comments

Comments
 (0)