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 6 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
31 changes: 31 additions & 0 deletions chapters/tree_traversal/code/java/MainClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//submitted by xam4lor
public class MainClass {
public static void main(String[] args) {
System.out.println("Creating Tree");
Tree tree = new Tree(3, 3);

System.out.println("Using recursive DFS :");
tree.dfsRecursive();

System.out.println("Using stack-based DFS :");
tree.dfsStack();

System.out.println("Using queue-based BFS :");
tree.bfsQueue();

System.out.println("Using post-order recursive DFS :");
tree.dfsRecursivePostOrder();


// 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.
System.out.println("Using in-order binary recursive DFS : (fail)");
tree.dfsRecursiveInOrderBinary();

tree = new Tree(3, 2);
System.out.println("Using in-order binary recursive DFS : (suceed)");
tree.dfsRecursiveInOrderBinary();


System.out.println("");
}
}
129 changes: 129 additions & 0 deletions chapters/tree_traversal/code/java/Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// submitted by xam4lor
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;

public class Tree {
public Node root;

public Tree(int rowCount, int childrenCount) {
// this.root is the root node of the Tree
this.root = new Node(1);
this.createAllChildren(this.root, rowCount, childrenCount);
}


public void dfsRecursive() {
this.dfsRecursive(this.root);
}

private void dfsRecursive(Node node) {
System.out.println(node.id);

for (Node n : node.children) {
dfsRecursive(n);
}
}


public void dfsRecursivePostOrder() {
this.dfsRecursivePostOrder(this.root);
}

private void dfsRecursivePostOrder(Node node) {
for (Node n : node.children) {
dfsRecursivePostOrder(n);
}

// Here we are doing something ...
System.out.println(node.id);
}


public void dfsRecursiveInOrderBinary() {
dfsRecursiveInOrderBinary(this.root);
}

// This assumes only 2 children
private void dfsRecursiveInOrderBinary(Node node) {
if(node.children.size() > 2) {
Copy link
Contributor

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

Copy link
Contributor Author

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 :/

System.err.println("Not a binary tree at dfsRecursiveInOrderBinary()!");
return;
}

if(node.children.size() > 1) {
dfsRecursiveInOrderBinary(node.children.get(0));
System.out.println(node.id);
dfsRecursiveInOrderBinary(node.children.get(1));
}
Copy link
Contributor

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 {

}

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;
Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor Author

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' ?

Copy link
Contributor

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);
    }
}


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);
}
}
}
}


private void createAllChildren(Node node, int rowCount, int childrenCount) {
if(rowCount <= 1)
return;
Copy link
Contributor

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


for (int i = 0; i < childrenCount; i++) {
node.children.add(new Node(node.id * 10 + i + 1));
createAllChildren(node.children.get(i), rowCount - 1, childrenCount);
}
}


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