Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
inorder traversal of binary tree

Inorder Traversal in Binary Tree

Posted on 3 June 202023 April 2023 By YASH PAL No Comments on Inorder Traversal in Binary Tree

In the Inorder traversal of the binary tree, we first visit the left subtree of the root node, and then we visit the root node and after that, we visit the right subtree of the root node.

Properties of Inorder traversal

  • Traverse the left subtree of the root in Inorder
  • Visit the root node
  • Traverse the right subtree of the root in Inorder

let’s understand it with an example let’s say we have a binary tree as you see in the image given below.

inorder traversal in binary tree

First, we need to divide the whole tree into subtrees as you see in the given image above. and then we start traversing the tree.

as you see first we visit the left subtree of the root node in inorder. so the root node is P and its left subtree is B but in subtree B, there is also a left subtree D so we first start traversing with subtree D.

In the subtree, there is no left subtree so we first visit the left node and then the root node, and then the right node.

so the inorder traversal of subtree D is T S Q and inorder of subtree B is D A E and substitute the value of subtree D so the inorder is

T S Q A E P F C

now find the inorder of subtree E and the inorder of E is E D so the inorder is

T S Q A E D P F C

now go to the root node P so the inorder is

T S Q A E D P F C

now find the inorder of subtree C but in subtree C, there is also left subtree F and right subtree G.

so first we need to find the inorder of the F subtree because it’s a left subtree and in inorder, the left subtree comes first, and then the root node and right node.

so the inorder of subtree F is M so the inorder of subtree C is

M X G

now we find the inorder of subtree G so the inorder of subtree is C R
so the inorder of subtree C is

M X C R

now we substitute the inorder of right subtree C into the so the inorder of the binary tree is

T S Q A E D P M X C R

Program to find the inorder of binary trees using Python.

from collections import deque


class Node:
    def __init__(self, value):
        self.info = value
        self.lchild = None
        self.rchild = None

class BinaryTree:
    def __init__(self):
        self.root = None
    def is_empty(self):
        return self.root is None
    def display(self):
        self._display(self.root, 0)
        print()

    def _display(self,p,level):
        if p is None:
            return        self._display(p.rchild, level+1)
        print()

        for i in range(level):
            print(" ", end='')
        print(p.info)
        self._display(p.lchild, level+1)

    def preorder(self):
        self._preorder(self.root)
        print()

    def _preorder(self,p):
        if p is None:
            return        print(p.info, " ", end='')
        self._preorder(p.lchild)
        self._preorder(p.rchild)

    def inorder(self):
        self._inorder(self.root)
        print()

    def _inorder(self,p):
        if p is None:
            return        self._inorder(p.lchild)
        print(p.info," ", end='')
        self._inorder(p.rchild)

    def postorder(self):
        self._postorder(self.root)
        print()

    def _postorder(self,p):
        if p is None:
            return        self._postorder(p.lchild)
        self._postorder(p.rchild)
        print(p.info," ",end='')

    def level_order(self):
        if self.root is None:
            print("Tree is empty")
            return
        qu = deque()
        qu.append(self.root)

        while len(qu) != 0:
            p = qu.popleft()
            print(p.info + " ", end='')
            if p.lchild is not None:
                qu.append(p.lchild)
            if p.rchild is not None:
                qu.append(p.rchild)

    def height(self):
        return self._height(self.root)

    def _height(self,p):
        if p is None:
            return 0
        hL = self._height(p.lchild)
        hR = self._height(p.rchild)

        if hL > hR:
            return 1 + hL
        else:
            return 1 + hR

    def create_tree(self):
        self.root = Node('p')
        self.root.lchild = Node('Q')
        self.root.rchild = Node('R')
        self.root.lchild.lchild = Node('A')
        self.root.lchild.rchild = Node('B')
        self.root.rchild.lchild = Node('X')


##########################
bt = BinaryTree()

bt.create_tree()

bt.display()
print()

print("Preorder : ")
bt.preorder()
print("")

print("Inorder : ")
bt.inorder()
print()

print("Postorder : ")
bt.postorder()
print()

print("Level order : ")
bt.level_order()
print()

print("Height of tree is ", bt.height())
Computer Science Tutorials, Data Structures Tutorials Tags:computer science, Data Structure

Post navigation

Previous Post: Traversing in a Binary Tree
Next Post: Preorder Traversal of Binary Tree

Related Tutorials

python libraries for text transformation REPHRASING REVOLUTION: HOW PYTHON LIBRARIES FACILITATE TEXT TRANSFORMATION Computer Science Tutorials
Introduction to Statistics for Data Science: Building a Solid Foundation Computer Science Tutorials
programming languages for machine learning and data science Top Programming Languages For Machine Learning Computer Science Tutorials
How to Become a Successful Data Engineer in the Data Science Field – Complete Guide Computer Science Tutorials
Is Python a good language for Machine Learning/AI? Computer Science Tutorials
basics of boolean algebra Its Operators, Laws, and Examples Basics of Boolean Algebra: Its Operators, Laws, and Examples Boolean Algebra

Leave a Reply

You must be logged in to post a comment.

  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

  • Algorithms Tutorials
  • Basic Programming
  • Boolean Algebra
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Linux
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Python Tutorials
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme