-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Implementation of Tree Traversal in java #149
Implementation of Tree Traversal in java #149
Conversation
The same argument as in the C# case. I don't think we need separate implementations just to include in the chapters. The final code, especially with the explicit Additionally, I'd prefer to keep merging stuff into the archive that actually compiles. |
Yup. What @Gustorn already said. Show the entire code at the bottom of the chapter and import parts of it for the smaller snippets. No need to have two versions of the same code. |
Allright ! |
The problem is that if I do that, I have to options :
That's why I made two different codes. |
I'd just go for unused methods. They don't hurt and people can take the code and play around with them if they want. |
Currently the C# code is split into |
|
||
|
||
|
||
public void DFSRecursive(Node node) { |
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.
You should really just remove these versions. They are not any more clear and they take up a bunch of space for no reason. People who'll want to use the Java implementation will know what this.root
means in the other implementations.
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.
But we need this method to have a parameter of type Node.
I have two solutions : one is to create a function with a same name but no parameters like DFSRecursive() who just call DFSRecursive(this.root).
Or I call DFSRecursive(null) and check if the node is null, the node equals this.root.
In java we cannot create functions within other functions.
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.
You could create a private function under the public one which does the recursive part and then you can include both in the code example
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.
I was about to suggest method overloading as well. Is there a reason not to?
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.
It was one of my solutions, I think It's the better thing to do. (overloading)
One more thing, the methods do not follow Java's naming convention: they should be camelCase, e.g.: |
I'm a bit unsure about the excessive whitespace use to separate the different functions. I see why you did it but maybe change it to 2 newlines. |
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.
My last nitpicks, sorry for doing them piecemeal, I did the earlier ones during the day when I had a little free time.
|
||
private void createAllChildren(Node node, int rowCount, int childrenCount) { | ||
if(rowCount <= 1) | ||
return; |
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.
This indentation is not quite right
Queue<Node> queue = new PriorityQueue<Node>(); | ||
queue.add(this.root); | ||
|
||
Node temp; |
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.
You can move this inside the loop
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.
Any opinion on this one?
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.
What do you mean by 'inside a loop' ?
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.
You only use tmp
inside the following while loop, so you could write this instead:
while(stack.size() != 0) {
System.out.println(stack.peek().id);
Node tmp = stack.pop();
for (Node c : tmp.children) {
stack.push(c);
}
}
dfsRecursiveInOrderBinary(node.children.get(0)); | ||
System.out.println(node.id); | ||
dfsRecursiveInOrderBinary(node.children.get(1)); | ||
} |
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.
The standard Java coding style would place the else on the same line as the closing brace:
if (...) {
} else {
}
|
||
// This assumes only 2 children | ||
private void dfsRecursiveInOrderBinary(Node node) { | ||
if(node.children.size() > 2) { |
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.
I think the de-facto standard is to have spaces around brackets in Java, so
if (condition) {
}
// Instead of
if(condition) {
}
Similarly for while, for, etc. loops
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.
You are right ! That's a bad practice that I have :/
With the newest whitespace/formatting changes the line ranges in the chapter are out of date |
And I think that's done ! |
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.
Thank you for the contribution, sorry again for giving the feedback piece-by-piece, I know it's more annoying to fix it this way :)
You do what you can ! No problem ! :) |
No description provided.