Skip to content

Commit

Permalink
c# tree traversal: fixed index out of range issue (algorithm-archivis…
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikac authored Oct 26, 2021
1 parent cfcfccc commit 4a81890
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
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 = 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));
}
}

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.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 + " ");
}
}

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 @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down

0 comments on commit 4a81890

Please sign in to comment.