-
-
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
Changes from 6 commits
02bd3c0
6f0aed8
3f919c6
971a62f
bc174f2
ff1c8de
c3aba0f
f6772a6
8467c5f
374ee27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(""); | ||
} | ||
} |
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) { | ||
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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can move this inside the loop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any opinion on this one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by 'inside a loop' ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You only use 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
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 think the de-facto standard is to have spaces around brackets in Java, so
Similarly for while, for, etc. loops
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 are right ! That's a bad practice that I have :/