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.
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 treeLet's us look at the relationship between the nodes.
A
is the root node. B
while the right subtree begins at C
.A
has two child nodes – B
and C
.C
is the parent node to F
and G
. F
and G
are siblings.F
and G
are know as leaf nodes because they do not have children.B
is the parent node to D
and E
.D
is the parent node to H
and I
.D
and E
are siblings as well as H
and I
.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.
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.
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:
Here is another way of representing the information above:
Inorder => Left, Root, Right.
Preorder => Root, Left, Right.
Post order => Left, Right, Root.
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 traversalThe 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 traversalThe order for post order traversal is Left, Right, Root.
Suggested reading: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 traversalIn 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!
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:
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.
Now, let's see the algorithm 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.
The final output that we will get after postorder traversal is -
{15, 28, 25, 35, 30, 45, 55, 70, 60, 50, 40}
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.
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 Matrix153
0
0
All Comments (0)
Related Articles
If you are interested in sending in a Guest Blogger Submission,welcome to write for us!
Comments