-
-
Notifications
You must be signed in to change notification settings - Fork 360
Enhance the code of TreeTraversal and EuclideanAlgorithm for C# #120
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
Changes from 4 commits
522a058
741f983
3134b20
ed2415f
7041652
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// submitted by Julian Schacher (jspp) | ||
using System; | ||
|
||
namespace TreeTraversal | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("TreeTraversal"); | ||
var tree = new Tree(3, 3); | ||
Console.WriteLine("DFSRecursive:"); | ||
tree.DFSRecursive(); | ||
Console.WriteLine("DFSStack:"); | ||
tree.DFSStack(); | ||
Console.WriteLine("BFSQueue:"); | ||
tree.BFSQueue(); | ||
Console.WriteLine("DFSRecursivePostorder"); | ||
tree.DFSRecursivePostorder(); | ||
|
||
// Console.WriteLine("DFSRecursiveInorder (fail)"); | ||
// tree.DFSRecursiveInorderBinary(); | ||
tree = new Tree(3, 2); | ||
Console.WriteLine("DFSRecursiveInorder (succeed)"); | ||
tree.DFSRecursiveInorderBinary(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,27 +10,63 @@ private class Node | |
{ | ||
public List<Node> Children { get; set; } = new List<Node>(); | ||
public int Id { get; set; } | ||
|
||
public Node(int id) => this.Id = id; | ||
} | ||
|
||
private Node root; | ||
|
||
public Tree(int depthCount, int childrenCount) | ||
{ | ||
CreateTree(depthCount, childrenCount); | ||
root = new Node(1); | ||
CreateAllChildren(root, depthCount, childrenCount); | ||
} | ||
|
||
public void CreateTree(int depthCount, int childrenCount) | ||
public void DFSRecursive() | ||
{ | ||
root = new Node | ||
DFSRecursive(root); | ||
|
||
void DFSRecursive(Node node) | ||
{ | ||
Id = 1 | ||
}; | ||
CreateAllChildren(root, depthCount, childrenCount); | ||
Console.WriteLine(node.Id); | ||
|
||
foreach (var c in node.Children) | ||
DFSRecursive(c); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a C# expert, but I'm pretty sure that omitting brackets for loops and conditions is dangerous no matter which language you're using. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Gustorn What do you think about that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be dangerous, sure, but C#'s bracket style kinda encourages it. Having a single-line statement stretch into 3 eats up a lot of vertical space. I don't really mind either way but as long as it's a short, single line I think omitting the parens is fine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I shall allow it, then. |
||
} | ||
} | ||
|
||
public void StartDFSRecursive() | ||
public void DFSRecursivePostorder() | ||
{ | ||
DFSRecursive(root); | ||
DFSRecursivePostorder(root); | ||
|
||
void DFSRecursivePostorder(Node node) | ||
{ | ||
foreach (var c in node.Children) | ||
DFSRecursivePostorder(c); | ||
|
||
Console.WriteLine(node.Id); | ||
} | ||
} | ||
|
||
public void DFSRecursiveInorderBinary() | ||
{ | ||
DFSRecursiveInorderBinary(root); | ||
|
||
// This assumes only 2 children | ||
void DFSRecursiveInorderBinary(Node node) | ||
{ | ||
if (node.Children.Count > 2) | ||
throw new Exception("Not binary tree!"); | ||
|
||
if (node.Children.Count > 0) | ||
{ | ||
DFSRecursiveInorderBinary(node.Children[0]); | ||
Console.WriteLine(node.Id); | ||
DFSRecursiveInorderBinary(node.Children[1]); | ||
} | ||
else | ||
Console.WriteLine(node.Id); | ||
} | ||
} | ||
|
||
public void DFSStack() | ||
|
@@ -45,9 +81,7 @@ public void DFSStack() | |
temp = stack.Pop(); | ||
|
||
foreach (var c in temp.Children) | ||
{ | ||
stack.Push(c); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -63,9 +97,7 @@ public void BFSQueue() | |
temp = queue.Dequeue(); | ||
|
||
foreach (var c in temp.Children) | ||
{ | ||
queue.Enqueue(c); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -76,22 +108,9 @@ private void CreateAllChildren(Node node, int rowCount, int childrenCount) | |
|
||
for (int i = 0; i < childrenCount; i++) | ||
{ | ||
node.Children.Add(new Node | ||
{ | ||
Id = node.Id * 10 + i + 1 | ||
}); | ||
node.Children.Add(new Node(node.Id * 10 + i + 1)); | ||
CreateAllChildren(node.Children[i], rowCount - 1, childrenCount); | ||
} | ||
} | ||
|
||
private void DFSRecursive(Node node) | ||
{ | ||
Console.WriteLine(node.Id); | ||
|
||
foreach (var c in node.Children) | ||
{ | ||
DFSRecursive(c); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these left over from a debugging session?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People can uncomment that, to see, that the algorithm will throw an exception then (because the node has more than 2 children). After that a new Tree is created with only 2 children per node and the method can be used without problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case: What about a comment above these 2 lines that explains what you just explained here? Just something like
// Uncomment these to see what happens if ...
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. I'll do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.