Skip to content

Commit 01c6fb8

Browse files
xam4lorzsparal
authored andcommitted
Implementation of Tree Traversal in java (#149)
* Added java examples for Tree Traversal * Added java in mk file * Combined example and explanation code in one file * Updated markdown file * Added method overloading * Removed useless spaces * Updated Markdown file * Updated code for coding conventions * Moved temp variable in bfsQueue() * Update tree_traversal.md
1 parent 98a2fac commit 01c6fb8

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//submitted by xam4lor
2+
public class MainClass {
3+
public static void main(String[] args) {
4+
System.out.println("Creating Tree");
5+
Tree tree = new Tree(3, 3);
6+
7+
System.out.println("Using recursive DFS :");
8+
tree.dfsRecursive();
9+
10+
System.out.println("Using stack-based DFS :");
11+
tree.dfsStack();
12+
13+
System.out.println("Using queue-based BFS :");
14+
tree.bfsQueue();
15+
16+
System.out.println("Using post-order recursive DFS :");
17+
tree.dfsRecursivePostOrder();
18+
19+
20+
// 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.
21+
System.out.println("Using in-order binary recursive DFS : (fail)");
22+
tree.dfsRecursiveInOrderBinary();
23+
24+
tree = new Tree(3, 2);
25+
System.out.println("Using in-order binary recursive DFS : (suceed)");
26+
tree.dfsRecursiveInOrderBinary();
27+
28+
29+
System.out.println("");
30+
}
31+
}
+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// submitted by xam4lor
2+
import java.util.ArrayList;
3+
import java.util.PriorityQueue;
4+
import java.util.Queue;
5+
import java.util.Stack;
6+
7+
public class Tree {
8+
public Node root;
9+
10+
public Tree(int rowCount, int childrenCount) {
11+
// this.root is the root node of the Tree
12+
this.root = new Node(1);
13+
this.createAllChildren(this.root, rowCount, childrenCount);
14+
}
15+
16+
17+
public void dfsRecursive() {
18+
this.dfsRecursive(this.root);
19+
}
20+
21+
private void dfsRecursive(Node node) {
22+
System.out.println(node.id);
23+
24+
for (Node n : node.children) {
25+
dfsRecursive(n);
26+
}
27+
}
28+
29+
30+
public void dfsRecursivePostOrder() {
31+
this.dfsRecursivePostOrder(this.root);
32+
}
33+
34+
private void dfsRecursivePostOrder(Node node) {
35+
for (Node n : node.children) {
36+
dfsRecursivePostOrder(n);
37+
}
38+
39+
// Here we are doing something ...
40+
System.out.println(node.id);
41+
}
42+
43+
44+
public void dfsRecursiveInOrderBinary() {
45+
dfsRecursiveInOrderBinary(this.root);
46+
}
47+
48+
// This assumes only 2 children
49+
private void dfsRecursiveInOrderBinary(Node node) {
50+
if (node.children.size() > 2) {
51+
System.err.println("Not a binary tree at dfsRecursiveInOrderBinary()!");
52+
return;
53+
}
54+
55+
if (node.children.size() > 1) {
56+
dfsRecursiveInOrderBinary(node.children.get(0));
57+
System.out.println(node.id);
58+
dfsRecursiveInOrderBinary(node.children.get(1));
59+
} else {
60+
System.out.println(node.id);
61+
}
62+
}
63+
64+
65+
public void dfsStack() {
66+
Stack<Node> stack = new Stack<Node>();
67+
stack.push(this.root);
68+
69+
Node tmp;
70+
71+
while (stack.size() != 0) {
72+
System.out.println(stack.peek().id);
73+
tmp = stack.pop();
74+
75+
for (Node c : tmp.children) {
76+
stack.push(c);
77+
}
78+
}
79+
}
80+
81+
public void bfsQueue() {
82+
Queue<Node> queue = new PriorityQueue<Node>();
83+
queue.add(this.root);
84+
85+
while (queue.size() != 0) {
86+
System.out.println(queue.peek().id);
87+
Node temp = queue.poll(); // return null if the queue is empty
88+
89+
if (temp != null) {
90+
for (Node c : temp.children) {
91+
queue.add(c);
92+
}
93+
}
94+
}
95+
}
96+
97+
98+
private void createAllChildren(Node node, int rowCount, int childrenCount) {
99+
if (rowCount <= 1) {
100+
return;
101+
}
102+
103+
for (int i = 0; i < childrenCount; i++) {
104+
node.children.add(new Node(node.id * 10 + i + 1));
105+
createAllChildren(node.children.get(i), rowCount - 1, childrenCount);
106+
}
107+
}
108+
109+
110+
private class Node implements Comparable<Node> {
111+
public ArrayList<Node> children;
112+
public int id;
113+
114+
public Node(int id) {
115+
this.children = new ArrayList<Node>();
116+
this.id = id;
117+
}
118+
119+
@Override
120+
public int compareTo(Node other) {
121+
// Need to implement Comparable<Node> and override this
122+
// method because of the method BFSQueue() which uses Queues
123+
// and must know how to check if two nodes are the same or not
124+
return Integer.compare(this.id, other.id);
125+
}
126+
}
127+
}

chapters/tree_traversal/tree_traversal.md

+18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Trees are naturally recursive data structures, and because of this, we cannot ac
1111
[import:9-15, lang:"csharp"](code/cs/Tree.cs)
1212
{% sample lang="c" %}
1313
[import:7-11, lang:"c_cpp"](code/c/tree_traversal.c)
14+
{% sample lang="java" %}
15+
[import:110-126, lang:"java"](code/java/Tree.java)
1416
{% sample lang="js" %}
1517
This has not been implemented in your chosen language, so here is the Julia code
1618
[import:3-7, lang:"julia"](code/julia/Tree.jl)
@@ -36,6 +38,8 @@ Because of this, the most straightforward way to traverse the tree might be recu
3638
[import:25-36, lang:"csharp"](code/cs/Tree.cs)
3739
{% sample lang="c" %}
3840
[import:37-45, lang:"c_cpp"](code/c/tree_traversal.c)
41+
{% sample lang="java" %}
42+
[import:21-27, lang:"java"](code/java/Tree.java)
3943
{% sample lang="js" %}
4044
[import:12-15, lang:"javascript"](code/javascript/tree.js)
4145
{% sample lang="py" %}
@@ -70,6 +74,8 @@ This has not been implemented in your chosen language, so here is the Julia code
7074
[import:38-49, lang:"csharp"](code/cs/Tree.cs)
7175
{% sample lang="c" %}
7276
[import:47-53, lang:"c_cpp"](code/c/tree_traversal.c)
77+
{% sample lang="java" %}
78+
[import:34-41, lang:"java"](code/java/Tree.java)
7379
{% sample lang="js" %}
7480
[import:17-20, lang:"javascript"](code/javascript/tree.js)
7581
{% sample lang="py" %}
@@ -101,6 +107,8 @@ This has not been implemented in your chosen language, so here is the Julia code
101107
[import:51-70, lang:"csharp"](code/cs/Tree.cs)
102108
{% sample lang="c" %}
103109
[import:55-73, lang:"c_cpp"](code/c/tree_traversal.c)
110+
{% sample lang="java" %}
111+
[import:48-62, lang:"java"](code/java/Tree.java)
104112
{% sample lang="js" %}
105113
[import:22-34, lang:"javascript"](code/javascript/tree.js)
106114
{% sample lang="py" %}
@@ -141,6 +149,8 @@ In code, it looks like this:
141149
[import:72-86, lang:"csharp"](code/cs/Tree.cs)
142150
{% sample lang="c" %}
143151
[import:75-93, lang:"c_cpp"](code/c/tree_traversal.c)
152+
{% sample lang="java" %}
153+
[import:65-79, lang:"java"](code/java/Tree.java)
144154
{% sample lang="js" %}
145155
[import:36-43, lang:"javascript"](code/javascript/tree.js)
146156
{% sample lang="py" %}
@@ -172,6 +182,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
172182
[import:88-102, lang:"csharp"](code/cs/Tree.cs)
173183
{% sample lang="c" %}
174184
[import:95-113, lang:"c_cpp"](code/c/tree_traversal.c)
185+
{% sample lang="java" %}
186+
[import:81-95, lang:"java"](code/java/Tree.java)
175187
{% sample lang="js" %}
176188
[import:45-52, lang:"javascript"](code/javascript/tree.js)
177189
{% sample lang="py" %}
@@ -205,6 +217,12 @@ utility.h
205217
[import, lang:"c_cpp"](code/c/utility.h)
206218
tree_traversal.c
207219
[import, lang:"c_cpp"](code/c/tree_traversal.c)
220+
{% sample lang="java" %}
221+
### Java
222+
Tree.java
223+
[import, lang:"java"](code/java/Tree.java)
224+
MainClass.java
225+
[import, lang:"java"](code/java/MainClass.java)
208226
{% sample lang="js" %}
209227
### JavaScript
210228
[import, lang:"javascript"](code/javascript/tree.js)

0 commit comments

Comments
 (0)