Binary Tree Traversal in Data StructureThe tree can be defined as a non-linear data structure that stores data in the form of nodes, and nodes are connected to each other with the help of edges. Among all the nodes, there is one main node called the root node, and all other nodes are the children of these nodes. In any data structure, traversal is an important operation. In the traversal operation, we walk through the data structure visiting each element of the data structure at least once. The traversal operation plays a very important role while doing various other operations on the data structure like some of the operations are searching, in which we need to visit each element of the data structure at least once so that we can compare each incoming element from the data structure to the key that we want to find in the data structure. So like any other data structure, the tree data also needs to be traversed to access each element, also known as a node of the tree data structure. There are different ways of traversing a tree depending upon the order in which the tree's nodes are visited and the types of data structure used for traversing the tree. There are various data structures involved in traversing a tree, as traversing a tree involves iterating over all nodes in some manner. As from a given node, there could be more than one way to traverse or visit the next node of the tree, so it becomes important to store one of the nodes traverses further and store the rest of the nodes having a possible path for backtracking the tree if needed. Backtracking is not a linear approach, so we need different data structures for traversing through the whole tree. The stack and queue are the major data structure that is used for traversing a tree. Traversal is a technique for visiting all of a tree's nodes and printing their values. Traversing a tree involves iterating over all nodes in some manner. We always start from the root (head) node since all nodes are connected by edges (links). As the tree is not a linear data structure, there can be more than one possible next node from a given node, so some nodes must be deferred, i.e., stored in some way for later visiting. Types of Traversal of Binary TreeThere are three types of traversal of a binary tree.
Inorder Tree TraversalThe left subtree is visited first, followed by the root, and finally the right subtree in this traversal strategy. Always keep in mind that any node might be a subtree in and of itself. The output of a binary tree traversal in order produces sorted key values in ascending order. C Code Let's write a basic C program for Inorder traversal of the binary search tree. Output The above C code hives the following output. Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree (via Inorder Traversal). 1 Enter the value to be inserted 12 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 98 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 23 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 78 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 45 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 87 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 2 Inorder Traversal of the Binary Tree:: 12 23 45 78 87 98 Do you want to continue (Type y or n) n Preorder Tree TraversalIn this traversal method, the root node is visited first, then the left subtree, and finally the right subtree. Code Let's write a C code for the Preorder traversal of the binary search tree. Output: Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 45 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 53 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 1 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 2 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 97 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 22 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 2 Preorder Traversal of the Binary Tree:: 45 1 2 22 53 97 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 76 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 30 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 67 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 4 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 2 Preorder Traversal of the Binary Tree:: 45 1 2 22 4 30 53 97 76 67 Do you want to continue (Type y or n) n Postorder Tree TraversalThe root node is visited last in this traversal method, hence the name. First, we traverse the left subtree, then the right subtree, and finally the root node. Code Let's write a program for Postorder traversal of the binary search tree. Output: Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 12 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 31 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 24 Wrong Entry Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 24 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 88 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 67 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 56 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 90 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 44 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 71 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 38 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 29 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 2 Postorder Traversal of the Binary Tree:: 29 24 38 44 56 71 67 90 88 31 12 Do you want to continue (Type y or n) n We have seen the different C programs to implement inorder, preorder, and postorder traversal of the nodes of the Binary tree. Now let us write a code to perform all three types of traversal in a single program. Code Output: Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 2 Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 5 Directed to right link. Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 7 Directed to right link. Directed to right link. Leaf node created. Do you want to continue (Type y or n) Y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 9 Directed to right link. Directed to right link. Directed to right link. Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 31 Directed to right link. Directed to right link. Directed to right link. Directed to right link. Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 78 Directed to right link. Directed to right link. Directed to right link. Directed to right link. Directed to right link. Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 2 Preorder Traversal of the Binary Tree:: 2 5 7 9 31 78 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 3 Inorder Traversal of the Binary Tree:: 2 5 7 9 31 78 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 4 Postorder Traversal of the Binary Tree:: 78 31 9 7 5 2 Do you want to continue (Type y or n) n ConclusionSo, in this article, we understood the binary tree traversal and different types of binary tree traversal like inorder binary tree traversal, preorder binary tree traversal, and postorder binary tree traversal. And we also did the programming implementation of all these types of tree traversal. And in the last part of the article, we also understood how to implement a basic c program that can be used to implement all three types of tree traversals in a single file. Next TopicQueue Operations in Data Structure |