Depth First Traversal

Depth First Traversal in Java

Depth first traversal means we start at the root node and explores as far as possible along each branch before backtracking. There are three depth first traversals.

depth first search example

Depth First Traversal

  1. Pre-order : In preorder traversal, we visit each node before visiting any of its children. We follow the pattern : Root -> Left child -> Right child           
  2. In-order : In inorder traversal, we visit each node after visiting its left child, but before visiting its right child. We follow the pattern : Left child -> Root  ->Right child 
  3. Post-order : In postorder traversal, we visit each node after visiting both of its children. We follow the pattern : Left child -> Right child -> Root node
Depth first Traversal

Algorithm:

Preorder:

  1. Visit the root.
  2. Traverse the left subtree.
  3. Traverse the right subtree.

Inorder:

  1. Traverse the left subtree.
  2. Visit the root.
  3. Traverse the right subtree.

Postorder:

  1. Traverse the left subtree.
  2. Traverse the right subtree.
  3. Visit the root.

Code For Binary Search Tree​

Run
import java.util.*;

public class Main
{
  //Node class

  public static class Node
  {

    int data;
    Node left;
    Node right;

    public Node (int data)
    {
      this.data = data;
      this.left = null;
      this.right = null;
    }

  }

//Binary tree class to create a binary tree

  public static class BinaryTree
  {
    Node root;

    public BinaryTree ()
    {
      this.root = null;
    }
  }

//Preorder traversal

  public static void preOrder (Node node)
  {
//Base case

    if (node == null)
      {
	return;
      }

//Print root data
    System.out.print (node.data + " ");

//Go to left subtree
    preOrder (node.left);

//Go to right subtree
    preOrder (node.right);
  }

//Inorder traversal

  public static void inOrder (Node node)
  {
//Base case

    if (node == null)
      {
	return;
      }

//Go to left subtree

    inOrder (node.left);

//Print root data

    System.out.print (node.data + " ");

//Go to right subtree

    inOrder (node.right);
  }

//Postorder traversal

  public static void postOrder (Node node)
  {
//Base case

    if (node == null)
      {
	return;
      }

//Go to left subtree

    postOrder (node.left);

//Go to right subtree

    postOrder (node.right);

//Print root data

    System.out.print (node.data + " ");
  }

  public static void main (String[]args) throws Exception
  {

//Construct binary tree

    BinaryTree bt = new BinaryTree ();

      bt.root = new Node (10);

      bt.root.left = new Node (20);

      bt.root.right = new Node (30);

      bt.root.left.left = new Node (40);

      bt.root.left.right = new Node (50);

      System.out.println ("Preorder Traversal :");
      preOrder (bt.root);

      System.out.println ("\nInorder Traversal :");

      inOrder (bt.root);

      System.out.println ("\nPostorder Traversal :");

      postOrder (bt.root);

  }
}

Output :

Preorder Traversal :
10 20 40 50 30 
Inorder Traversal :
40 20 50 10 30 
Postorder Traversal :
40 50 20 30 10 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java