Skip to content

Commit f395357

Browse files
committed
standardize csharp output
1 parent 387e437 commit f395357

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

contents/tree_traversal/code/csharp/Program.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@ class Program
77
{
88
static void Main(string[] args)
99
{
10-
Console.WriteLine("TreeTraversal");
11-
var tree = new Tree(3, 3);
12-
Console.WriteLine("DFSRecursive:");
10+
var tree = new Tree(2, 3);
11+
Console.WriteLine("[#] Recursive DFS:");
1312
tree.DFSRecursive();
14-
Console.WriteLine("DFSStack:");
13+
Console.WriteLine();
14+
15+
Console.WriteLine("[#] Recursive Postorder DFS:");
16+
tree.DFSRecursivePostorder();
17+
Console.WriteLine();
18+
19+
Console.WriteLine("[#] Stack-based DFS:");
1520
tree.DFSStack();
16-
Console.WriteLine("BFSQueue:");
21+
Console.WriteLine();
22+
23+
Console.WriteLine("[#] Queue-based BFS:");
1724
tree.BFSQueue();
18-
Console.WriteLine("DFSRecursivePostorder");
19-
tree.DFSRecursivePostorder();
25+
Console.WriteLine();
2026

21-
// 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.
22-
// Console.WriteLine("DFSRecursiveInorder (fail)");
23-
// tree.DFSRecursiveInorderBinary();
2427
tree = new Tree(3, 2);
25-
Console.WriteLine("DFSRecursiveInorder (succeed)");
28+
Console.WriteLine("[#] Recursive Inorder DFS for Binary Tree:");
2629
tree.DFSRecursiveInorderBinary();
30+
Console.WriteLine();
2731
}
2832
}
2933
}

contents/tree_traversal/code/csharp/Tree.cs

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

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

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

@@ -37,7 +37,7 @@ public void DFSRecursive()
3737

3838
void DFSRecursive(Tree tree)
3939
{
40-
Console.WriteLine(tree.Id);
40+
Console.Write(tree.Id + " ");
4141

4242
foreach (var c in tree._children)
4343
DFSRecursive(c);
@@ -53,7 +53,7 @@ void DFSRecursivePostorder(Tree tree)
5353
foreach (var c in tree._children)
5454
DFSRecursivePostorder(c);
5555

56-
Console.WriteLine(tree.Id);
56+
Console.Write(tree.Id + " ");
5757
}
5858
}
5959

@@ -70,11 +70,11 @@ void DFSRecursiveInorderBinary(Tree tree)
7070
if (tree._children.Count > 0)
7171
{
7272
DFSRecursiveInorderBinary(tree._children[0]);
73-
Console.WriteLine(tree.Id);
73+
Console.Write(tree.Id + " ");
7474
DFSRecursiveInorderBinary(tree._children[1]);
7575
}
7676
else
77-
Console.WriteLine(tree.Id);
77+
Console.Write(tree.Id + " ");
7878
}
7979
}
8080

@@ -85,7 +85,7 @@ public void DFSStack()
8585

8686
while (stack.Count != 0)
8787
{
88-
Console.WriteLine(stack.Peek().Id);
88+
Console.Write(stack.Peek().Id + " ");
8989
var temp = stack.Pop();
9090

9191
foreach (var c in temp._children)
@@ -100,7 +100,7 @@ public void BFSQueue()
100100

101101
while (queue.Count != 0)
102102
{
103-
Console.WriteLine(queue.Peek().Id);
103+
Console.Write(queue.Peek().Id + " ");
104104
var temp = queue.Dequeue();
105105

106106
foreach (var c in temp._children)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)