Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 28, 2018 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/6cc0e01314091c485717 to your computer and use it in GitHub Desktop.
Save thmain/6cc0e01314091c485717 to your computer and use it in GitHub Desktop.
import java.util.*;
public class BSTDFS {
public void DFS(Node root) {
Stack<Node> s = new Stack<Node>();
s.add(root);
while (s.isEmpty() == false) {
Node x = s.pop();
if(x.right!=null) s.add(x.right);
if(x.left!=null) s.add(x.left);
System.out.print(" " + x.data);
}
}
public static void main(String args[]){
Node root = new Node(1);
root.left = new Node(2);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right = new Node(3);
root.right.left = new Node(6);
root.right.right = new Node(7);
BSTDFS b = new BSTDFS();
System.out.println("Depth-First-Search : ");
b.DFS(root);
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
left = null;
right = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment