Skip to content

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
merged 10 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions chapters/tree_traversal/code/java/MainClass.java
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("");
}
}
122 changes: 122 additions & 0 deletions chapters/tree_traversal/code/java/Tree.java
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) {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor Author

@xam4lor xam4lor Jun 29, 2018

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)

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);
}
}
}
18 changes: 18 additions & 0 deletions chapters/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Trees are naturally recursive data structures, and because of this, we cannot ac
[import:11-15, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs)
{% sample lang="c" %}
[import:7-11, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
[import:105-121, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:3-7, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -38,6 +40,8 @@ Because of this, the most straightforward way to traverse the tree might be recu
[import:48-57, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs)
{% sample lang="c" %}
[import:37-45, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
[import:17-23, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
[import:15-23, lang:"javascript"](code/javascript/Tree_example.js)
{% sample lang="py2" %}
Expand Down Expand Up @@ -75,6 +79,8 @@ This has not been implemented in your chosen language, so here is the Julia code
[import:75-84, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs)
{% sample lang="c" %}
[import:47-53, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
[import:25-32, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:18-26, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -110,6 +116,8 @@ This has not been implemented in your chosen language, so here is the Julia code
[import:86-104, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs)
{% sample lang="c" %}
[import:55-73, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
[import:34-49, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:28-43, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -154,6 +162,8 @@ In code, it looks like this:
[import:36-52, lang:"csharp"](code/cs/Tree/Tree.cs)
{% sample lang="c" %}
[import:75-93, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
[import:51-65, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
[import:25-40, lang:"javascript"](code/javascript/Tree_example.js)
{% sample lang="py2" %}
Expand Down Expand Up @@ -187,6 +197,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
[import:54-70, lang:"csharp"](code/cs/Tree/Tree.cs)
{% sample lang="c" %}
[import:95-113, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
[import:67-83, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
[import:42-57, lang:"javascript"](code/javascript/Tree_example.js)
{% sample lang="py2" %}
Expand Down Expand Up @@ -222,6 +234,12 @@ utility.h
[import, lang:"c_cpp"](code/c/utility.h)
tree_traversal.c
[import, lang:"c_cpp"](code/c/tree_traversal.c)
{% sample lang="java" %}
### Java
Tree.java
[import, lang:"java"](code/java/Tree.java)
MainClass.java
[import, lang:"java"](code/java/MainClass.java)
{% sample lang="js" %}
### JavaScript
[import, lang:"javascript"](code/javascript/Tree_example.js)
Expand Down