Java Program to Perform the Preorder Tree Traversal

Java Program to Perform the Preorder Tree Traversal

What are Trees?

In this Article, we will write a Program to Perform the preorder tree traversal in Java.
In Java, a tree is a data structure that is similar to a linked list, but with a hierarchical structure. Each node in a tree has a value and one or more child nodes, and the topmost node is called the root. Trees are commonly used to implement algorithms such as search, sorting, and traversal.

Tree Class:

There is no specific “Tree” class in the core Java libraries. However, the Java Collections Framework does provide several classes that are based on tree data structures. These include the TreeSet and TreeMap classes.
The TreeSet class is a collection that stores elements in a sorted order. The TreeMap class is a map that stores key-value pairs in a sorted order.
Both TreeSet and TreeMap classes provide useful methods for adding, removing, and searching for elements, as well as for traversing the tree in different ways.
Additionally, there are also several other classes in the Java libraries that can be used to implement trees, such as ArrayDeque, LinkedList, and PriorityQueue. Additionally, there are several third party libraries such as Google’s Guava, Apache’s Commons, and others that provide Tree data structures.

What is Tree Traversal?

Tree traversal is a process of visiting all the nodes of a tree. It is used to traverse the tree one node at a time and is used to access or change the data in the nodes. There are three main types of tree traversal:

Java Program to Perform the preorder tree traversal

Java Program to Perform the Preorder tree traversal:

Run
// Importing all the required Packages
import java.util.*;

// Define the tree node class
class Node {
    int value;
    Node left;
    Node right;
    
    // defining the constructor
    Node(int val) {
        value = val;
        left = null;
        right = null;
    }
}

// Define the main class
class Main{
    
    // Function Definition to perform Preorder traversal
    public static void PreorderTraversal(Node node) {
        if (node == null) {
            return;
        }
        
        // Recursive calls 
        System.out.print(node.value + " ");
        PreorderTraversal(node.left);
        PreorderTraversal(node.right);
    }
    
    // Define the main function to test the Pre-order traversal
    public static void main(String[] args){
        // Creating a binary tree
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        
        // Perform Pre-order traversal and print the output
        System.out.print("Pre-order Traversal: ");
        PreorderTraversal(root);
    }
}
Output:

Pre-order Traversal: 1 2 4 5 3 

Explanation:

In the above program, we define a Node class to represent the tree nodes, and a PreorderTraversal class to define the preorderTraversal function and the main function. The PreorderTraversal function performs the preorder traversal recursively by traversing the left subtree first, then the right subtree, and finally visiting the current node. The main function creates a sample binary tree and calls the preorderTraversal function to print the preorder traversal output.

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