patternjavaMinor
Binary search traversal with in/pre/post order and BFS/DFS
Viewed 0 times
ordersearchwithbfsdfsprepostbinaryandtraversal
Problem
I have the following code for the BST for the inorder, postorder, preorder, breadth first and depth first traversals. Can you review and let me know the optimisation points and issues, if any?
```
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class BST {
static class Node {
int data;
Node right, left;
Node(int data) {
this.data = data;
right = left = null;
}
@Override
public String toString() {
return "Node [data=" + data + ", right=" + right + ", left=" + left + "]";
}
}
public static void preOrder(Node root) {
if (root != null) {
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
}
public static void inOrder(Node root) {
if (root != null) {
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}
public static void postOrder(Node root) {
if (root != null) {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
}
public static void breadthFirstTraversal(Node root) {
Queue queue = new ArrayDeque();
if (root != null) {
queue.add(root);
}
while (!queue.isEmpty()) {
Node node = queue.remove();
System.out.print(node.data + " ");
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}
public static void depthFirstTraversal(Node root) {
Stack stack = new Stack();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
Node node = stack.pop();
System.out.p
```
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class BST {
static class Node {
int data;
Node right, left;
Node(int data) {
this.data = data;
right = left = null;
}
@Override
public String toString() {
return "Node [data=" + data + ", right=" + right + ", left=" + left + "]";
}
}
public static void preOrder(Node root) {
if (root != null) {
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
}
public static void inOrder(Node root) {
if (root != null) {
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}
public static void postOrder(Node root) {
if (root != null) {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
}
public static void breadthFirstTraversal(Node root) {
Queue queue = new ArrayDeque();
if (root != null) {
queue.add(root);
}
while (!queue.isEmpty()) {
Node node = queue.remove();
System.out.print(node.data + " ");
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}
public static void depthFirstTraversal(Node root) {
Stack stack = new Stack();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
Node node = stack.pop();
System.out.p
Solution
There is only one point I can state: in your constructor of
Node you don't need to explicitly set left and right to null. The Java runtime system will do that for you. Also, note that DFS and preorder traversal are doing the same thing so you might want to consider removing one. Since your binary tree is not guaranteed to be balanced, I would retain the DFS as it allocates your stack on the heap, and so it is invulnerable to stack overflow as your recursive preorder traversal routine.Context
StackExchange Code Review Q#117774, answer score: 4
Revisions (0)
No revisions yet.