Data Structures in C# - Exploring BinaryTree - Part 1 - Create, Find and Display - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Thursday, July 9, 2026

Data Structures in C# - Exploring BinaryTree - Part 1 - Create, Find and Display

Data Structures in C# - Exploring BinaryTree - Part 1 - Create, Find and Display

Screenshot from the tutorial
Screenshot from the tutorial

Data Structures in C# - Exploring Binary Trees

In the world of computer science, data structures play a crucial role in storing and managing data efficiently. One of the fundamental data structures is the Binary Tree. In this blog post, we will explore the concept of Binary Trees, how to create them, find elements within them, and display their contents, all using C#. This tutorial is based on the video "Data Structures in C# - Exploring BinaryTree - Part 1 - Create, Find and Display."

What is a Binary Tree?

A Binary Tree is a hierarchical data structure where each node has at most two children, referred to as the left child and the right child. This structure is useful for various applications, including searching, sorting, and storing hierarchical data.

Key Characteristics of a Binary Tree

  • Node: The basic unit of a binary tree, containing data and references to its left and right child nodes.
  • Root: The top node of the tree, which serves as the entry point to the structure.
  • Leaf: A node that does not have any children.
  • Height: The length of the longest path from the root to a leaf node.

Creating a Binary Tree in C#

Before we dive into the implementation, let’s define a simple Node class in C# that represents an individual node in the binary tree.

public class Node
{
    public int Data { get; set; }
    public Node Left { get; set; }
    public Node Right { get; set; }

    public Node(int data)
    {
        Data = data;
        Left = null;
        Right = null;
    }
}

Next, we will create a BinaryTree class that will facilitate operations such as inserting nodes, finding elements, and displaying the tree.

public class BinaryTree
{
    public Node Root { get; private set; }

    public BinaryTree()
    {
        Root = null;
    }

    // Method to insert a new node
    public void Insert(int data)
    {
        Root = InsertRec(Root, data);
    }

    private Node InsertRec(Node root, int data)
    {
        if (root == null)
        {
            root = new Node(data);
            return root;
        }

        if (data < root.Data)
            root.Left = InsertRec(root.Left, data);
        else if (data > root.Data)
            root.Right = InsertRec(root.Right, data);

        return root;
    }
}

Explanation of the Insert Method

  • Insert: This public method allows users to insert a new value into the binary tree. It calls a recursive helper method, InsertRec, which checks if the current node is null. If it is, a new node is created; otherwise, the method decides whether to place the new value in the left or right subtree based on its comparison with the current node's value.

Finding Elements in a Binary Tree

To search for an element in the binary tree, we can implement a simple recursive method.

public bool Find(int data)
{
    return FindRec(Root, data);
}

private bool FindRec(Node root, int data)
{
    if (root == null)
        return false;

    if (root.Data == data)
        return true;

    return data < root.Data ? FindRec(root.Left, data) : FindRec(root.Right, data);
}

Explanation of the Find Method

  • Find: This method initiates the search for a specific value in the tree. It calls the FindRec method, which traverses the tree recursively, returning true if the value is found and false otherwise.

Displaying the Binary Tree

To visualize the contents of the binary tree, we can implement an in-order traversal method. In in-order traversal, the nodes are processed in the following order: left child, parent, and then right child.

public void DisplayInOrder()
{
    DisplayInOrderRec(Root);
}

private void DisplayInOrderRec(Node root)
{
    if (root != null)
    {
        DisplayInOrderRec(root.Left);
        Console.Write(root.Data + " ");
        DisplayInOrderRec(root.Right);
    }
}

Explanation of the Display Method

  • DisplayInOrder: This method serves as a public interface for displaying the tree. It calls the private DisplayInOrderRec method, which recursively visits each node in in-order sequence.

Putting It All Together

Now that we have defined our Node and BinaryTree classes with methods to insert, find, and display nodes, let’s see how to use them in a simple console application.

class Program
{
    static void Main(string[] args)
    {
        BinaryTree tree = new BinaryTree();
        
        // Inserting nodes
        tree.Insert(10);
        tree.Insert(5);
        tree.Insert(15);
        tree.Insert(3);
        tree.Insert(7);
        
        // Displaying the tree
        Console.WriteLine("In-order traversal of the binary tree:");
        tree.DisplayInOrder();
        Console.WriteLine();

        // Finding an element
        int searchValue = 7;
        Console.WriteLine($"Finding {searchValue}: {tree.Find(searchValue)}");
    }
}

Explanation of the Console Application

  • We create a new instance of BinaryTree.
  • We insert several values into the tree.
  • We display the contents of the tree using in-order traversal.
  • Finally, we search for a specific value and output whether it exists in the tree.

Conclusion

In this post, we explored Binary Trees in C#, covering how to create a binary tree, find elements, and display its contents through in-order traversal. Understanding these fundamental concepts in data structures will give you a solid foundation for more complex data management tasks.

Stay tuned for Part 2, where we will delve into more advanced operations such as deletion and balancing of binary trees! If you have any questions or need further clarification, feel free to leave a comment below.

Another screenshot from the tutorial
Another view from the tutorial

Connect with SkillBakery Studios

Explore more tutorials, tools, and resources:

Posted by SkillBakery Studios

No comments:

Post a Comment

Post Top Ad