Last active
May 28, 2018 04:21
-
-
Save thmain/6cc0e01314091c485717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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