Java Program for the Preorder Tree Traversal in Binary Tree Last Updated : 15 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Preorder traversal is the method used to traverse the tree data structure. In the preorder traversal, the nodes are visited in the order: of root, left subtree, and right subtree. It is called as "preorder" because the root node is visited before its children. This traversal technique is widely used in the various tree-based algorithms and applications. Representation of Binary Tree Implementation of the Preorder Tree TraversalPreorder traversal can be implemented using either recursion or iteration(using the stack). The recursive approach is straightforward: Visit the root node.Recursively traverse the left subtree.Recursively traverse the right subtree.Basic Operations and ComplexityTraversal" In the preorder traversal, each node is visited exactly once, which can result in O(n) time complexity where the n is a number of nodes in the tree. The space complexity of the recursive approach is O(h) where the h is the height of the tree due to the recursion stack. Program to Implement Preorder Tree TraversalBelow is the implementation of Preorder Tree Traversal: Java // Java Program to implement Tree // Preorder Traversal // Node Class class Node { int data; Node left, right; public Node(int item) { data = item; left = right = null; } } // Class to create Binary Tree class BinaryTree { Node root; // Preorder Traversal void preorderTraversal(Node node) { if (node == null) return; System.out.print(node.data + " "); preorderTraversal(node.left); preorderTraversal(node.right); } // main function public static void main(String args[]) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); System.out.println("Preorder traversal of binary tree is "); tree.preorderTraversal(tree.root); } } OutputPreorder traversal of binary tree is 1 2 4 5 3 Complexity of the above MethodTime complexity: O(n)Space complexity: O(n) Applications of Preorder Traversal in Binary TreeBinary search trees for the efficient searching, insertion and deletion.Expression trees for the representing the expressions in the computer science.Heap trees for the implementing the priority queues.Decision trees for the classification and regression in the machine learning. Comment More infoAdvertise with us Next Article Inorder Tree Traversal in Binary Tree in C M maheshkadambala Follow Improve Article Tags : Java Java-DSA Practice Tags : Java Similar Reads Preorder Tree Traversal of Binary Tree in C++ A binary tree is a non-linear hierarchical data structure where each node has at most two children which are referred to as the left child and the right child. As it has non-linear structure it can be traversed in multiple ways one such way is pre-order traversal which is a technique in which nodes 4 min read Preorder Tree Traversal of Binary Tree in C A binary tree is a hierarchical data structure composed of nodes where each node has at most two children. It can referred to as the left child and the right child. Due to having a non-linear structure, a binary tree can be traversed in multiple ways. One such way is preorder traversal which is a De 3 min read Java Program to Perform the Inorder Tree Traversal The binary tree is the hierarchical data structure in which each node has at most two children and it can referred to as the left child and the right child. Inorder tree traversal is one of the fundamental ways to visit all the nodes in the binary tree. It can specifically visiting the nodes in the 4 min read Postorder Tree Traversal in Binary Tree in C A binary tree is a hierarchical data structure in computer science. Each node in a binary tree can have at most two children: a left child and a right child. There are several ways to traverse a binary tree and in this article, we will learn about the postorder traversal of a binary tree in C. Examp 3 min read Inorder Tree Traversal in Binary Tree in C A binary Tree is a hierarchical data structure in which each node has at most two children and it can referred to as the left child and right child. Due to being a non-linear data structure, different traversal techniques are possible for it. Inorder tree traversal is one of the techniques used to v 3 min read Java Program to Perform the Postorder Tree Traversal The Binary Tree can consist of nodes where each node contains the value and the references to its left and right children. The structure is organized hierarchically with a single root node at the top and each node having at Most Two Children. In this article, we will learn to perform Postorder Tree 3 min read Like