Preorder Traversal in Binary Tree in Java

What is Preorder Traversal ?

In preorder traversal , the root node is visited first, then the left subtree and finally the right subtree. Preorder traversal also used to get an prefix expression of an expression. In this article we will see how to perform preorder travesal using recursion in java.

preorder traversal using recurrsion

Steps to Find Preorder Traversal in Binary Tree

    Here are some of the steps for tree traversal :
  • Step 1: First we will print the root element which is 10 in above example.
  • Step 2: Traverse the left subtree recursively. The only node on left subtree is 20 . So print it and move to the right subtree of root.
  • Step 3: 30 is the root of right sub-tree , therefore , print it and move to its left. 
  • Step 5: Print 40 which is the only element in left subtree and move to right of it’s parent.
  • Step 6: Print 50  which is the last element of the tree.
  • Step 7 : Therefore, the printing sequence will be 10,20,30,40,50.
preorder binary tree traversal

Algorithm to find preorder traversal of binary tree using recursion

   Algorithm  preorder(Tree):

  1. Visit root node.
  2. Recursively traverse left subtree.
  3. Recursively traverse right subtree.

Java code for Preorder traversal in binary tree using recurrsion in java

Run
// Preorder Traversal in java 
/*Node class with left and right child and currend node and key value */
class Node
{
   Node left ,right;
   int value;
   Node(int value)
   {
       this.value=value;
       left=null;
       right=null;
   }
}
class Preorder
{
   Node root; //root of the binary tree
   Preorder()
   {
       root=null;
   }
   /*preorder traversal of binary tree */  
   public  void preorder(Node ptr)
   {
       if(ptr==null)
       return ;
       /*first print the value of node*/  
       System.out.print(ptr.value+" ");
       /*then traverse the left child */
       preorder(ptr.left);
       /*now traverse the right child*/
       preorder(ptr.right);
   }
}

public class Main{
   public static void main(String[] args)
   {
       Preorder t=new Preorder();
       t.root=new Node(10);
       t.root.left=new Node(20);
       t.root.right=new Node(30);
       t.root.right.left=new Node(40);
       t.root.right.right=new Node(50);
       t.preorder(t.root);
   }
}

Output :

10 20 30 40 50 

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