Skip to content

Commit 6d02784

Browse files
june128zsparal
authored andcommitted
Update Tree Traversal C#. (#209)
* Restructure code by removing the Node class. Remove CreateAllChildren method and make the constructor do it instead. * Inline temp variables of DFSSTack and BFSQueue.
1 parent e19f00e commit 6d02784

File tree

3 files changed

+50
-54
lines changed

3 files changed

+50
-54
lines changed

chapters/tree_traversal/code/cs/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static void Main(string[] args)
1717
tree.BFSQueue();
1818
Console.WriteLine("DFSRecursivePostorder");
1919
tree.DFSRecursivePostorder();
20+
2021
// Uncommenting the following 2 lines will result in an exception thrown because at least one Node of the Tree has more than 2 children and therefor a DFSRecursiveInorderBinary doesn't work.
2122
// Console.WriteLine("DFSRecursiveInorder (fail)");
2223
// tree.DFSRecursiveInorderBinary();

chapters/tree_traversal/code/cs/Tree.cs

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,111 +6,106 @@ namespace TreeTraversal
66
{
77
public class Tree
88
{
9-
private class Node
9+
public int Id { get; private set; }
10+
private List<Tree> _children = new List<Tree>();
11+
12+
public Tree(int depthCount, int childrenCount)
1013
{
11-
public List<Node> Children { get; set; } = new List<Node>();
12-
public int Id { get; set; }
14+
this.Id = 1;
1315

14-
public Node(int id) => this.Id = id;
16+
if (!(depthCount <= 1))
17+
{
18+
for (int i = 0; i < childrenCount; i++)
19+
this._children.Add(new Tree(this.Id * 10 + i + 1, depthCount - 1, childrenCount));
20+
}
1521
}
1622

17-
private Node root;
18-
19-
public Tree(int depthCount, int childrenCount)
23+
private Tree(int id, int depthCount, int childrenCount)
2024
{
21-
root = new Node(1);
22-
CreateAllChildren(root, depthCount, childrenCount);
25+
this.Id = id;
26+
27+
if (!(depthCount <= 1))
28+
{
29+
for (int i = 0; i < childrenCount; i++)
30+
this._children.Add(new Tree(this.Id * 10 + i + 1, depthCount - 1, childrenCount));
31+
}
2332
}
2433

2534
public void DFSRecursive()
2635
{
27-
DFSRecursive(root);
36+
DFSRecursive(this);
2837

29-
void DFSRecursive(Node node)
38+
void DFSRecursive(Tree tree)
3039
{
31-
Console.WriteLine(node.Id);
40+
Console.WriteLine(tree.Id);
3241

33-
foreach (var c in node.Children)
42+
foreach (var c in tree._children)
3443
DFSRecursive(c);
3544
}
3645
}
3746

3847
public void DFSRecursivePostorder()
3948
{
40-
DFSRecursivePostorder(root);
49+
DFSRecursivePostorder(this);
4150

42-
void DFSRecursivePostorder(Node node)
51+
void DFSRecursivePostorder(Tree tree)
4352
{
44-
foreach (var c in node.Children)
53+
foreach (var c in tree._children)
4554
DFSRecursivePostorder(c);
4655

47-
Console.WriteLine(node.Id);
56+
Console.WriteLine(tree.Id);
4857
}
4958
}
5059

5160
public void DFSRecursiveInorderBinary()
5261
{
53-
DFSRecursiveInorderBinary(root);
62+
DFSRecursiveInorderBinary(this);
5463

5564
// This assumes only 2 children
56-
void DFSRecursiveInorderBinary(Node node)
65+
void DFSRecursiveInorderBinary(Tree tree)
5766
{
58-
if (node.Children.Count > 2)
59-
throw new Exception("Not binary tree!");
67+
if (tree._children.Count > 2)
68+
throw new Exception("Not binary tree!");
6069

61-
if (node.Children.Count > 0)
70+
if (tree._children.Count > 0)
6271
{
63-
DFSRecursiveInorderBinary(node.Children[0]);
64-
Console.WriteLine(node.Id);
65-
DFSRecursiveInorderBinary(node.Children[1]);
72+
DFSRecursiveInorderBinary(tree._children[0]);
73+
Console.WriteLine(tree.Id);
74+
DFSRecursiveInorderBinary(tree._children[1]);
6675
}
6776
else
68-
Console.WriteLine(node.Id);
77+
Console.WriteLine(tree.Id);
6978
}
7079
}
7180

7281
public void DFSStack()
7382
{
74-
var stack = new Stack<Node>();
75-
stack.Push(root);
76-
Node temp;
83+
var stack = new Stack<Tree>();
84+
stack.Push(this);
7785

7886
while (stack.Count != 0)
7987
{
8088
Console.WriteLine(stack.Peek().Id);
81-
temp = stack.Pop();
89+
var temp = stack.Pop();
8290

83-
foreach (var c in temp.Children)
91+
foreach (var c in temp._children)
8492
stack.Push(c);
8593
}
8694
}
8795

8896
public void BFSQueue()
8997
{
90-
var queue = new Queue<Node>();
91-
queue.Enqueue(root);
92-
Node temp;
98+
var queue = new Queue<Tree>();
99+
queue.Enqueue(this);
93100

94101
while (queue.Count != 0)
95102
{
96103
Console.WriteLine(queue.Peek().Id);
97-
temp = queue.Dequeue();
104+
var temp = queue.Dequeue();
98105

99-
foreach (var c in temp.Children)
106+
foreach (var c in temp._children)
100107
queue.Enqueue(c);
101108
}
102109
}
103-
104-
private void CreateAllChildren(Node node, int rowCount, int childrenCount)
105-
{
106-
if (rowCount <= 1)
107-
return;
108-
109-
for (int i = 0; i < childrenCount; i++)
110-
{
111-
node.Children.Add(new Node(node.Id * 10 + i + 1));
112-
CreateAllChildren(node.Children[i], rowCount - 1, childrenCount);
113-
}
114-
}
115110
}
116111
}

chapters/tree_traversal/tree_traversal.md

Lines changed: 6 additions & 6 deletions
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:15-18, lang:"c_cpp"](code/c++/tree_example.cpp)
1010
{% sample lang="cs" %}
11-
[import:9-15, lang:"csharp"](code/cs/Tree.cs)
11+
[import:7-11, lang:"csharp"](code/cs/Tree.cs)
1212
{% sample lang="c" %}
1313
[import:7-11, lang:"c_cpp"](code/c/tree_traversal.c)
1414
{% sample lang="java" %}
@@ -35,7 +35,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
3535
{% sample lang="cpp" %}
3636
[import:20-27, lang:"c_cpp"](code/c++/tree_example.cpp)
3737
{% sample lang="cs" %}
38-
[import:25-36, lang:"csharp"](code/cs/Tree.cs)
38+
[import:34-45, lang:"csharp"](code/cs/Tree.cs)
3939
{% sample lang="c" %}
4040
[import:37-45, lang:"c_cpp"](code/c/tree_traversal.c)
4141
{% sample lang="java" %}
@@ -71,7 +71,7 @@ Now, in this case the first element searched through is still the root of the tr
7171
This has not been implemented in your chosen language, so here is the Julia code
7272
[import:18-26, lang:"julia"](code/julia/Tree.jl)
7373
{% sample lang="cs" %}
74-
[import:38-49, lang:"csharp"](code/cs/Tree.cs)
74+
[import:47-58, lang:"csharp"](code/cs/Tree.cs)
7575
{% sample lang="c" %}
7676
[import:47-53, lang:"c_cpp"](code/c/tree_traversal.c)
7777
{% sample lang="java" %}
@@ -103,7 +103,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
103103
This has not been implemented in your chosen language, so here is the Julia code
104104
[import:28-43, lang:"julia"](code/julia/Tree.jl)
105105
{% sample lang="cs" %}
106-
[import:51-70, lang:"csharp"](code/cs/Tree.cs)
106+
[import:60-79, lang:"csharp"](code/cs/Tree.cs)
107107
{% sample lang="c" %}
108108
[import:55-73, lang:"c_cpp"](code/c/tree_traversal.c)
109109
{% sample lang="java" %}
@@ -144,7 +144,7 @@ In code, it looks like this:
144144
{% sample lang="cpp" %}
145145
[import:29-45, lang:"c_cpp"](code/c++/tree_example.cpp)
146146
{% sample lang="cs" %}
147-
[import:72-86, lang:"csharp"](code/cs/Tree.cs)
147+
[import:81-94, lang:"csharp"](code/cs/Tree.cs)
148148
{% sample lang="c" %}
149149
[import:75-93, lang:"c_cpp"](code/c/tree_traversal.c)
150150
{% sample lang="java" %}
@@ -177,7 +177,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
177177
{% sample lang="cpp" %}
178178
[import:47-61, lang:"c_cpp"](code/c++/tree_example.cpp)
179179
{% sample lang="cs" %}
180-
[import:88-102, lang:"csharp"](code/cs/Tree.cs)
180+
[import:96-109, lang:"csharp"](code/cs/Tree.cs)
181181
{% sample lang="c" %}
182182
[import:95-113, lang:"c_cpp"](code/c/tree_traversal.c)
183183
{% sample lang="java" %}

0 commit comments

Comments
 (0)