C# program to implement In-order traversal in Binary Tree

Here, we are going to learn how to implement In-order traversal in Binary Tree in C#?
Submitted by Nidhi, on November 05, 2020

Here, we will implement a binary tree then traverse a binary tree into in-order.

Program:

The source code to implement In-order traversal in Binary Tree is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to implement In-order traversal in Binary Tree

using System;
using System.Collections.Generic;
using System. Text; 

class Node
{
    public int  item        ;
    public Node left_ptr    ;
    public Node right_ptr   ;
    
}
class BinaryTree
{
    public Node root;
    public Node GetRoot()
    {
        return root;
    }
    public void Inorder_Traverse(Node rootNode)
    {
        if (rootNode != null)
        {
            Inorder_Traverse(rootNode.left_ptr);
            Console.Write("{0} ", rootNode.item);
            Inorder_Traverse(rootNode.right_ptr);
        }
    }
    public void InsertItem(int item)
    {
        Node curNode;
        Node parentNode;
        Node node = new Node();
        
        node.item = item;

        if (root != null)
        {
            curNode = root;

            while (true)
            {
                parentNode = curNode;
                if (item < curNode.item)
                {
                    curNode = curNode.left_ptr;
                    if (curNode == null)
                    {
                        parentNode.left_ptr = node;
                        break;
                    }
                }
                else
                {
                    curNode = curNode.right_ptr;
                    if (curNode == null)
                    {
                        parentNode.right_ptr = node;
                        break;
                    }
                }
            }
        }
        else
        {
            root = node;
        }
    }
    
}
class Demo
{
    static void Main(string[] args)
    {
        BinaryTree tree = new BinaryTree();
       
        tree.InsertItem(10);
        tree.InsertItem(15);
        tree.InsertItem(35);
        tree.InsertItem(26);
        tree.InsertItem(47);
        tree.InsertItem(34);
        tree.InsertItem(90);
        
        Console.WriteLine("Inorder Traversal : ");
        tree.Inorder_Traverse(tree.GetRoot());
        Console.WriteLine(" ");
       
    }
}

Output:

Inorder Traversal :
10 15 26 34 35 47 90
Press any key to continue . . .

Explanation:

In the above program, we created three classes Node, BinaryTree, and Demo. The code class contains the item and left and a right pointer to the node.

The BinaryTree class is used to implement binary tree, it contains GetRoot(), InsertItem(), and Inorder_Traverse() methods. 

The GetRoot() method returns the root node, and InsertItem() method is used to insert the item into the tree. The Inorder_Traverse() method is used to traverse the tree into in-order.

Now look to the Demo class, the Demo class contains the Main() method, here we created an object of BinaryTree class and then insert the items into the tree and then traverse them into in-order traversal.

C# Data Structure Programs »


ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.