Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c# tree traversal: fixed index out of range issue #894

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion contents/tree_traversal/code/csharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// submitted by Julian Schacher (jspp)
using System;

namespace TreeTraversal
Expand Down
34 changes: 19 additions & 15 deletions contents/tree_traversal/code/csharp/Tree.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// submitted by Julian Schacher (jspp)
using System;
using System.Collections.Generic;

Expand All @@ -11,23 +10,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));
}
}

Expand Down Expand Up @@ -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.WriteLine(tree.Id);
DFSRecursiveInorderBinary(tree._children[1]);
case 2:
DFSRecursiveInorderBinary(tree._children[0]);
Console.WriteLine(tree.Id);
henrikac marked this conversation as resolved.
Show resolved Hide resolved
DFSRecursiveInorderBinary(tree._children[1]);
break;
case 1:
DFSRecursiveInorderBinary(tree._children[0]);
Console.WriteLine(tree.Id);
henrikac marked this conversation as resolved.
Show resolved Hide resolved
break;
case 0:
Console.WriteLine(tree.Id);
henrikac marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
throw new Exception("Not binary tree!");
}
else
Console.WriteLine(tree.Id);
}
}

Expand Down
12 changes: 6 additions & 6 deletions contents/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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:59-83, lang:"csharp"](code/csharp/Tree.cs)
{% sample lang="c" %}
[import:55-73, lang:"c"](code/c/tree_traversal.c)
{% sample lang="java" %}
Expand Down Expand Up @@ -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:85-98, lang:"csharp"](code/csharp/Tree.cs)
{% sample lang="c" %}
[import:75-93, lang:"c"](code/c/tree_traversal.c)
{% sample lang="java" %}
Expand Down Expand Up @@ -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:100-113, lang:"csharp"](code/csharp/Tree.cs)
{% sample lang="c" %}
[import:95-113, lang:"c"](code/c/tree_traversal.c)
{% sample lang="java" %}
Expand Down