Depth First Search/Traversal in Binary Tree

Objective: Given a Binary Search Tree, Do the Depth First Search/Traversal .

Appraoch:

  • Approach is quite simple, use Stack.
  • First add the add root to the Stack.
  • Pop out an element from Stack and add its right and left children to stack.
  • Pop out an element and print it and add its children.
  • Repeat the above two steps until the Stack id empty.

Example:

DFS

Complete Code:

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;
}
}

view raw
BSTDFS.java
hosted with ❤ by GitHub


Output:

Depth-First-Search : 
 1 2 4 5 3 6 7