Join Us

Binary Search Tree Traversal – Inorder, Preorder, Post ...

Author: sufeifei

Nov. 28, 2023

Hardware

In this tutorial, you will learn what a binary search tree is, what parts make up a tree, and some of the common terms we use when describing parts of a tree.

We will also see how to traverse a tree using some of the common algorithms – all illustrated with clear examples.

What Is a Binary Search Tree?

A binary search tree is a binary tree made up of nodes. Each node has a key signifying its value.

The value of the nodes on the left subtree are smaller than the value of the root node. And the value of the nodes on the right subtree are larger than the value of the root node.

The root node is the parent node of both subtrees.

The diagram below shows the main parts of a binary tree:

Diagram of a binary search tree

Let's us look at the relationship between the nodes.

  • A is the root node.
  • The left subtree begins at B while the right subtree begins at C.
  • Node A has two child nodes – B and C.
  • Node C is the parent node to F and G. F and G are siblings.
  • Node F and G are know as leaf nodes because they do not have children.
  • Node B is the parent node to D and E.
  • Node D is the parent node to H and I.
  • D and E are siblings as well as H and I.
  • Node E is a leaf node.

So here are some important terms that we just used to describe the tree above:

Root: The topmost node in the tree.

Parent: A node with a child or children.

Child: A node extended from another node (parent node).

Leaf: A node without a child.

What Is a Binary Search Tree Used For?

Binary search trees help us speed up our binary search as we are able to find items faster.

We can use the binary search tree for the addition and deletion of items in a tree.

We can also represent data in a ranked order using a binary tree. And in some cases, it can be used as a chart to represent a collection of information.

Next, we'll look at some techniques used in traversing a binary tree.

What is Tree Traversal?

Traversing a tree means visiting and outputting the value of each node in a particular order. In this tutorial, we will use the Inorder, Preorder, and Post order tree traversal methods.

The major importance of tree traversal is that there are multiple ways of carrying out traversal operations unlike linear data structures like arrays, bitmaps, matrices where traversal is done in a linear order.

Each of these methods of traversing a tree have a particular order they follow:

  • For Inorder, you traverse from the left subtree to the root then to the right subtree.
  • For Preorder, you traverse from the root to the left subtree then to the right subtree.
  • For Post order, you traverse from the left subtree to the right subtree then to the root.

Here is another way of representing the information above:

Inorder => Left, Root, Right.

Preorder => Root, Left, Right.

Post order => Left, Right, Root.

How to Traverse a Tree Using Inorder Traversal

We are going to create a tree similar to the one in the last section, but this time the node keys will be numbers instead of letters.

Remember that the values of the nodes on the left subtree are always smaller than the value of the root node. Also, the values of the nodes on the right subtree are larger than the value of the root node.

Here is the diagram we will be working with:

Recall that the order for inorder traversal is Left, Root, Right.

This is the result we get after using inorder traversal:

D, B, E, A, F, C, G

If that seems a bit complex for you to understand, then follow the order of the colors in the picture below

Inorder traversal

How to Traverse a Tree Using Preorder Traversal

The order here is Root, Left, Right.

Using the same diagram above, we have:

A, B, D, E, C, F, G

Here is the same diagram with different colors used as a guide:

Preorder traversal

How to Traverse a Tree Using Postorder Traversal

The order for post order traversal is Left, Right, Root.

Suggested reading:
Hardware
Selecting the Right Spherical Hex Nut
Hexagon Metal Mesh: Applications and Benefits
What are the advantages of Belleville washers?
How to Install Threaded Bar Anchors for Concrete?
What Are Carbide End Mills Used for?
Choosing the Right Rubber Slurry Pump: A Comprehensive Buyer's Guide

Here is the output:

D, E, B, F, G, C, A

If you can't figure out how we arrived at that result, then use the colors in the picture below as a guide:

Postorder traversal

Conclusion

In this tutorial, we learned the basics of what a binary search tree is, what the various parts of a binary tree are, and the common terms associated with a tree. We also saw some of the algorithms we can use to traverse a tree.

Thank you for reading!

next → ← prev

Postorder Traversal

In this article, we will discuss the postorder 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. So, here we will discuss another way to traverse the tree data structure, i.e., postorder traversal. The postorder traversal is one of the traversing techniques used for visiting the node in the tree. It follows the principle LRN (Left-right-node). Postorder traversal is used to get the postfix expression of a tree.

The following steps are used to perform the postorder traversal:

  • Traverse the left subtree by calling the postorder function recursively.
  • Traverse the right subtree by calling the postorder function recursively.
  • Access the data part of the current node.

The post order traversal technique follows the Left Right Root policy. Here, Left Right Root means the left subtree of the root node is traversed first, then the right subtree, and finally, the root node is traversed. Here, the Postorder name itself suggests that the tree's root node would be traversed at last.

Algorithm

Now, let's see the algorithm of postorder traversal.

Example of postorder traversal

Now, let's see an example of postorder traversal. It will be easier to understand the process of postorder traversal using an example.

The nodes with yellow color are not visited yet. Now, we will traverse the nodes of above tree using postorder traversal.

  • Here, 40 is the root node. We first visit the left subtree of 40, i.e., 30. Node 30 will also traverse in post order. 25 is the left subtree of 30, so it is also traversed in post order. Then 15 is the left subtree of 25. But 15 has no subtree, so print 15 and move towards the right subtree of 25.
  • 28 is the right subtree of 25, and it has no children. So, print 28.
  • Now, print 25, and the postorder traversal for 25 is finished.
  • Next, move towards the right subtree of 30. 35 is the right subtree of 30, and it has no children. So, print 35.
  • After that, print 30, and the postorder traversal for 30 is finished. So, the left subtree of given binary tree is traversed.
  • Now, move towards the right subtree of 40 that is 50, and it will also traverse in post order. 45 is the left subtree of 50, and it has no children. So, print 45 and move towards the right subtree of 50.
  • 60 is the right subtree of 50, which will also be traversed in post order. 55 is the left subtree of 60 that has no children. So, print 55.
  • Now, print 70, which is the right subtree of 60.
  • Now, print 60, and the post order traversal for 60 is completed.
  • Now, print 50, and the post order traversal for 50 is completed.
  • At last, print 40, which is the root node of the given binary tree, and the post order traversal for node 40 is completed.

The final output that we will get after postorder traversal is -

{15, 28, 25, 35, 30, 45, 55, 70, 60, 50, 40}

Complexity of Postorder traversal

The time complexity of postorder traversal is O(n), where 'n' is the size of binary tree.

Whereas, the space complexity of postorder traversal is O(1), if we do not consider the stack size for function calls. Otherwise, the space complexity of postorder traversal is O(h), where 'h' is the height of tree.

Implementation of Postorder traversal

Now, let's see the implementation of postorder traversal in different programming languages.

Program: Write a program to implement postorder traversal in C language.

Output

Program: Write a program to implement postorder traversal in C++.

Output

Program: Write a program to implement postorder traversal in C#.

Output

After the execution of the above code, the output will be -

Program: Write a program to implement postorder 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 Topic

Sparse Matrix

← prev next →

Binary Search Tree Traversal – Inorder, Preorder, Post ...

Postorder Traversal (Data Structures)

Related links:
The Science Behind Tri-cone Drilling Bits: How Do They Conquer the Earth's Depths?
Difference Between a Gate Valve And a Ball Valve
What Are the Common Uses of Thread Bars?
Comparing Belleville Disc Springs to Wave Washers for Spring Applications
Maximizing Efficiency and Precision: The High Pressure Control Valve
fence - Why does USDA require wooden posts when ...
6 Types of Gates Based on their Functions

46

0