-
-
Notifications
You must be signed in to change notification settings - Fork 359
Implementation of Tree Traversal in java #149
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
Merged
zsparal
merged 10 commits into
algorithm-archivists:master
from
xam4lor:treetraversal-java
Jun 29, 2018
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
02bd3c0
Added java examples for Tree Traversal
xam4lor 6f0aed8
Added java in mk file
xam4lor 3f919c6
Combined example and explanation code in one file
xam4lor 971a62f
Updated markdown file
xam4lor bc174f2
Added method overloading
xam4lor ff1c8de
Removed useless spaces
xam4lor c3aba0f
Updated Markdown file
xam4lor f6772a6
Updated code for coding conventions
xam4lor 8467c5f
Moved temp variable in bfsQueue()
xam4lor 374ee27
Update tree_traversal.md
xam4lor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//submitted by xam4lor | ||
public class MainClass { | ||
public static void main(String[] args) { | ||
System.out.println("Creating Tree"); | ||
Tree tree = new Tree(2, 3); | ||
|
||
System.out.println("Using recursive DFS :"); | ||
tree.DFSRecursive(tree.root); | ||
|
||
System.out.println("Using stack-based DFS :"); | ||
tree.DFSStack(); | ||
|
||
System.out.println("Using queue-based BFS :"); | ||
tree.BFSQueue(); | ||
|
||
System.out.println(""); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// submitted by xam4lor | ||
import java.util.ArrayList; | ||
import java.util.PriorityQueue; | ||
import java.util.Queue; | ||
import java.util.Stack; | ||
|
||
public class Tree { | ||
// root node of the Tree | ||
public Node root; | ||
|
||
public Tree(int numberRow, int numberChild) { | ||
root = createTree(numberRow, numberChild); | ||
} | ||
|
||
|
||
|
||
public void DFSRecursive(Node node) { | ||
System.out.println(node.id); | ||
|
||
for (Node n : node.children) { | ||
DFSRecursive(n); | ||
} | ||
} | ||
|
||
public void DFSRecursivePostOrder(Node node) { | ||
for (Node n : node.children) { | ||
DFSRecursivePostOrder(n); | ||
} | ||
|
||
// Here we are doing something ... | ||
System.out.println(node.id); | ||
} | ||
|
||
// This assumes only 2 children | ||
public void DFSRecursiveInOrderBTree(Node node) { | ||
if(node.children.size() > 2) { | ||
System.out.println("Not a binary tree!"); | ||
System.exit(1); | ||
} | ||
|
||
if(node.children.size() > 0) { | ||
DFSRecursiveInOrderBTree(node.children.get(0)); | ||
System.out.println(node.id); | ||
DFSRecursiveInOrderBTree(node.children.get(1)); | ||
} | ||
else { | ||
System.out.println(node.id); | ||
} | ||
} | ||
|
||
public void DFSStack() { | ||
Stack<Node> stack = new Stack<Node>(); | ||
stack.push(this.root); | ||
|
||
Node tmp; | ||
|
||
while(stack.size() != 0) { | ||
System.out.println(stack.peek().id); | ||
tmp = stack.pop(); | ||
|
||
for (Node c : tmp.children) { | ||
stack.push(c); | ||
} | ||
} | ||
} | ||
|
||
public void BFSQueue() { | ||
Queue<Node> queue = new PriorityQueue<Node>(); | ||
queue.add(this.root); | ||
|
||
Node temp; | ||
|
||
while(queue.size() != 0) { | ||
System.out.println(queue.peek().id); | ||
temp = queue.poll(); // return null if the queue is empty | ||
|
||
if(temp != null) { | ||
for (Node c : temp.children) { | ||
queue.add(c); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
public Node createTree(int numberRow, int numberChild) { | ||
Node ret = new Node(numberRow); | ||
|
||
if(numberRow == 0) { | ||
return ret; | ||
} | ||
|
||
for (int i = 1; i < numberChild; i++) { | ||
Node child = createTree(numberRow - 1, numberChild); | ||
ret.children.add(child); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
|
||
|
||
private class Node implements Comparable<Node> { | ||
public ArrayList<Node> children; | ||
public int id; | ||
|
||
public Node(int id) { | ||
this.children = new ArrayList<Node>(); | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public int compareTo(Node other) { | ||
// Need to implement Comparable<Node> and override this | ||
// method because of the method BFSQueue() which uses Queues | ||
// and must know how to check if two nodes are the same or not | ||
return Integer.compare(this.id, other.id); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?
Uh oh!
There was an error while loading. Please reload this page.
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)