Preorder Traversal17 Mar 2025 | 5 min read In this article, we will discuss the preorder traversal in data structure. Linear data structures such as stack, array, queue, etc., only have one way to traverse the data. But in a hierarchical data structure such as tree, there are multiple ways to traverse the data. In preorder traversal, first, root node is visited, then left sub-tree and after that right sub-tree is visited. The process of preorder traversal can be represented as - Root node is always traversed first in preorder traversal, while it is the last item of postorder traversal. Preorder traversal is used to get the prefix expression of a tree. The steps to perform the preorder traversal are listed as follows -
The preorder traversal technique follows the Root Left Right policy. The name preorder itself suggests that the root node would be traversed first. AlgorithmNow, let's see the algorithm of preorder traversal. Example of preorder traversalNow, let's see an example of preorder traversal. It will be easier to understand the process of preorder traversal using an example. ![]() The nodes with yellow color are not visited yet. Now, we will traverse the nodes of the above tree using preorder traversal.
After the completion of preorder traversal, the final output is - 40, 30, 25, 15, 28, 35, 50, 45, 60, 55, 70 Complexity of Preorder traversalThe time complexity of preorder traversal is O(n), where 'n' is the size of binary tree. Whereas, the space complexity of preorder traversal is O(1), if we do not consider the stack size for function calls. Otherwise, the space complexity of preorder traversal is O(h), where 'h' is the height of the tree. Implementation of Preorder traversalNow, let's see the implementation of preorder traversal in different programming languages. Program: Write a program to implement preorder traversal in C language. Output After the execution of the above code, the output will be - ![]() Program: Write a program to implement preorder traversal in C++. Output After the execution of the above code, the output will be - ![]() Program: Write a program to implement preorder traversal in C#. Output After the execution of the above code, the output will be - ![]() Program: Write a program to implement preorder traversal in Java. Output After the execution of the above code, the output will be - ![]() So, that's all about the article. Hope the article will be helpful and informative to you. Next TopicTree Traversal |
Introduction Ugly Figures: A Conception in Data Structures and Algorithms (DSA) is an intriguing account of several generalities extensively used in algorithmic design and dynamic programming. An unsolved number is described as an effective integer if its vertex rudiments are only two, three, or 5. An unattractive...
9 min read
The common non-linear data structure known as a tree. A tree illustrates a hierarchical structure in contrast to other data structures such an array, stack, queue, and linked list, which are linear in nature. A tree's ordering information is irrelevant. Two pointers and nodes make up...
5 min read
Problem Statement: Given two BSTs with x1 and x2 distinct nodes and asked to find values of two nodes whose sum is exactly equivalent to value x BST 1: 3 / \ 1 4 / \ 0...
23 min read
A stack is a linear data structure that operates on the Last In First Out (LIFO) principle. This indicates that the last thing added to the stack is deleted first. The alternate word for a stack is LIFO, which refers to the order in which items...
23 min read
Introduction to greedy algorithm: A greedy algorithm is a simple and intuitive strategy for solving optimization problems. It is an algorithmic paradigm that follows the problem-solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. The idea is...
11 min read
Problem Statement: We are given an integer array of nums and an integer k. Append k unique positive integers that do not appear in nums to nums such that the resulting total sum is minimum. Return the sum of the k integers appended to nums. Java Approach Using HashSet import...
5 min read
In 1962, GM Adelson-Velsky and EM Landis created the AVL Tree. To honors the people who created it, the tree is known as AVL. The definition of an AVL tree is a height-balanced binary search tree in which each node has a balance factor that is determined...
14 min read
The level of a key in a specific binary tree generally refers to the distance that is present between the root of the binary tree node and the node that is containing the desired key. It is very important and noteworthy how many steps are required...
7 min read
Problem statement We are given a 0-indexed integer array nums. There exists an array arr of length nums. length, where arr[i] is the sum of |i - j| overall j such that nums[j] == nums[i] and j != i. If there is no such j, set arr[i]...
12 min read
Queues are one of the most fundamental data structures in computer science. The principle of first-in, first-out ordering that queues follow has widespread uses in programming. However, understanding the nuances of queues - their strengths, applications, and limitations - is key to utilizing them effectively. In this...
12 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India