Skip to content

Commit 4c9a774

Browse files
committed
Tree Traversal [java]: Move main method into Tree class
1 parent 11610b5 commit 4c9a774

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

contents/tree_traversal/code/java/MainClass.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

contents/tree_traversal/code/java/Tree.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,34 @@ public int compareTo(Node other) {
124124
return Integer.compare(this.id, other.id);
125125
}
126126
}
127+
128+
public static void main(String[] args) {
129+
System.out.println("Creating Tree");
130+
Tree tree = new Tree(3, 3);
131+
132+
System.out.println("Using recursive DFS :");
133+
tree.dfsRecursive();
134+
135+
System.out.println("Using stack-based DFS :");
136+
tree.dfsStack();
137+
138+
System.out.println("Using queue-based BFS :");
139+
tree.bfsQueue();
140+
141+
System.out.println("Using post-order recursive DFS :");
142+
tree.dfsRecursivePostOrder();
143+
144+
145+
// 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.
146+
System.out.println("Using in-order binary recursive DFS : (fail)");
147+
tree.dfsRecursiveInOrderBinary();
148+
149+
tree = new Tree(3, 2);
150+
System.out.println("Using in-order binary recursive DFS : (succeed)");
151+
tree.dfsRecursiveInOrderBinary();
152+
153+
154+
System.out.println("");
155+
}
156+
127157
}

contents/tree_traversal/tree_traversal.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,6 @@ Here is a video describing tree traversal:
330330
{% sample lang="java" %}
331331
##### Tree.java
332332
[import, lang:"java"](code/java/Tree.java)
333-
##### MainClass.java
334-
[import, lang:"java"](code/java/MainClass.java)
335333
{% sample lang="js" %}
336334
[import, lang:"javascript"](code/javascript/tree.js)
337335
{% sample lang="py" %}

0 commit comments

Comments
 (0)