ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Tree data structure tutorial 3. Binary Tree Traversal

prodevelopertutorial June 15, 2019

In this chapter we shall learn about:
3.1 Introduction of Tree Traversal
3.2 Types of tree traversal
3.3 Breadth first traversal.
3.4 Depth first traversal.
3.5 Pre Order Traversal: DLR
3.6 In Order Traversal: LDR
3.7 Post Order Traversal: LRD

3.1 Introduction of Tree Traversal

A tree traversal can be defined as the process of visiting each node exactly once in some order.

As we know that trees are non linear data structure, we start from root node. Then we have 2 possible directions, i.e to go left or to go right.

3.2 So tree traversal can be done in 2 ways:

1. Breadth first traversal.
2. Depth first traversal.

This type of traversal holds good for both trees and graphs. [We shall discuss graph in next chapter]. In this chapter we shall learn related to trees.

3.3 Breadth first traversal.

In Breadth first traversal, we shall visit all the nodes at the same level before visiting the nodes at the next level.

Tree data structure tutorial 3. Binary Tree Traversal

In Breadth First approach, we should start from Level 0, then cover all the nodes in that level, then go to the next level.

So first we visit the root node at level 0. The root node value is “10”. Then we go to level 1, and then visit the nodes from left to right. Then nodes at level 1 are “11” “12”.

Now we completed level 1, then visit level 2. The nodes at level 2 are “13” “14” “15” “16”.

Now we visit level 3. The nodes at level 3 are “17” “18” “19” “20” “21” “22” “23” “24”.

When we combine all the nodes from all the level we get:

“10” “11” “12” “13” “14” “15” “16” “17” “18” “19” “20” “21” “22” “23” “24”.

This traversal is also called as level order traversal.

3.4.Depth first traversal.

In this approach, once we get a child, we would complete the whole sub tree of that child before going to the next child.

Tree data structure tutorial 3. Binary Tree Traversal

In our example, “10” is the root node. If we select “11” child, then we need to complete all the nodes present in that “11” sub tree. Then we should go to Right Sub Tree. Like wise we traverse all the nodes in the tree.

To do Depth First Traversal, we have multiple options as shown below:

root -> left child -> right child.
root -> right child -> left child.
left child -> root -> right child.

Depending on how we traverse we have 3 options.

1. Pre Order Traversal i.e root -> left -> right
2. In Order Traversal i.e left -> root -> right
3. Post Order Traversal i.e left -> right -> root

In total we have 6 possible permutations. But above 3 are the commonly used strategies.

To remember the traversal easily, if we consider root as “D” as in data, left as “L” and right as “R” we get:
Pre Order Traversal: DLR
In Order Traversal: LDR
Post Order Traversal: LRD

Now let us understand how the above 3 traversals are done.

We shall consider the below image as an example.
Tree data structure tutorial 3. Binary Tree Traversal

3.5 Pre Order Traversal: DLR

In this type of traversal, we visit the root node first, then visit left node, then right node.
First visit the root node “10”, then go to left sub tree “11”, then again “13” has a child “17”, visit it.
Hence we have visited all the nodes to the left.
“10” “11” “13” “17”.
There are no left sub tree to visit. Hence go one level up i.e “13”. “13” node, left sub tree is completed. But we did not complete right sub tree. Hence visit it i.e “18”.

“10” “11” “13” “17” “18”.

Now we have completed node “13”. Go one level up to “11”. As we have visited left sub tree, go to right sub tree i.e “14”.

“10” “11” “13” “17” “18” “14”.

Now we have completed the left sub tree of root node. Now we go to right sub tree of root node.

Now we go to “12” node. Node “12” has a left node “15” visit it. Then Node “15” does not have left sub tree, but has a right sub tree visit it i.e “22”.
“12” “15” “22”
Now that we have complete node “15” go one level up to node “12”. Visit all the nodes at right sub tree. i.e “16” “24”.

Hence the total order of visiting of all the nodes in pre order traversal is

“10” “11” “13” “17” “18” “14” “12” “15” “22” “16” “24”

3.6 In Order Traversal: LDR

In this traversal we visit left sub tree, then root then right sub tree.

In our example, we start with root node “10”, traverse to left i.e “11”, then “13”, then “17”. As “17” node doesn’t have any left sub tree, start with that, then visit it’s root i.e “13”, then to its right, i.e “18”.
Hence we get
“17” “13” “18”.

Then go one level up, to node “11”, as we have visited left sub tree, go to right i.e “14”.
“17” “13” “18” “11” “14”.

Then go to root node “10”.
“17” “13” “18” “11” “14” “10”.

Then go to right sub tree “12”, in that go to left sub tree “15”, as “15” does not have left sub tree, visit right sub tree “22”
Hence we get “15”, “22”

Go one level up “12”, then visit right sub tree “16” “24”

Total of right sub tree will be
“15”, “22”, “12”, “16” “24”.

Total In order traversal will be:
“17” “13” “18” “11” “14” “10” “15”, “22”, “12”, “16” “24”.

3.7 Post Order Traversal: LRD

In post order traversal, we visit the left sub tree first, then right sub tree, then to the root node.

In our example, we first go to 10 -> 11 -> 13 -> 17.
First we visit 17 node, then 18, then 13.

“17”, “18”, “13”.
Then we go one level up to node “11”, visit node “14”, then visit “11”.

Hence the left sub tree will be
“17”, “18”, “13”, “14”, “11”.

Note: Now we don’t visit the root node, we need to go to right sub tree.

Now traverse 12 -> 15 -> 22

Now visit the node “22”, then “15”,
“22”, “15”

Then go one level up to node 12. Traverse 16 -> 24.

Visit “24”, “16” then “12” then “10”.

Right sub tree will be
“22”, “15”, “24”, “16”, “12”, “10”

Total traversal will be:

“17”, “18”, “13”, “14”, “11” ,“22”, “15”, “24”, “16”, “12”, “10”

Further Reading:

AJ’s definitive guide for DS and Algorithms. Click here to study the complete list of algorithm and data structure tutorial. 85+ chapters to study from.

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT