Tree Data Structure in C#

Muhammad Zeeshan Oct 12, 2023
  1. Tree Data Structure in C#
  2. Steps to Build a Tree Data Structure in C#
Tree Data Structure in C#

Trees in C# will be the topic of discussion in this article. The data structure is the first thing we need to know.

You may arrange and store your data on the computer using a data structure more efficiently. Stacks, linked lists, and queues are all examples of sequential data structures.

Tree Data Structure in C#

A kind of hierarchical data organized in the form of a tree is referred to as the tree data structure. It comprises a central node, structural nodes, and sub-nodes linked together by edges.

It is also possible to state that the tree data structure’s roots, branches, and leaves are linked.

A tree might be utilized to represent data in a hierarchical structure. Each node that makes up a tree comprises two subparts: data and references.

The node at the very top of the tree is referred to as the root. The two products directly below it are referred to as the left subtree and right subtree.

The code for writing a tree node might look like this:

struct node {
  int Data;
  struct node
  *Leftchild;
  struct node
  *Rightchild;
};

Basic Terminologies in the Tree Data Structure in C#

Below are the basic terminologies you need to know in the tree data structure in C#:

  • Root node: The node at the top of the tree is known as the root. Each tree has a single root, and a single route leads from the root to any other node.
  • Level node: Nodes’ levels reflect the number of nodes that have been generated. Nodes descend in level increments of 1, 2, and so on, with the root node always being on the top.
  • Subtree node: The descendants of a node are represented by the subtree.
  • Parent node: If any node other than the root is connected to a parent node, it is termed a parent.
  • Child node: When a node’s edge descends, it is referred to as its child node.

Advantages of the Tree Data Structure in C#

Here are two advantages of using the tree data structure in C#:

  1. It is unnecessary to state the size of a tree when comparing it to other data structures, such as arrays or linked lists. As a result, the tree is efficient in terms of its storage space.
  2. In contrast, working with trees eliminates the need for labor-intensive operations like inserting, removing, and finding nodes, which are required by linked lists.

Steps to Build a Tree Data Structure in C#

We have here an example of a tree data structure in C#. Read the steps below to follow along.

  1. To begin, we must have the following libraries:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
  2. Create a class called node in the Shanii class that stores int type variables key, leftn as the left node, and rightn as the right node.

    class node {
      public int key;
      public node leftn, rightn;
    };
    
  3. Then, make a root node and a list of nodes called z from a node’s object.

    static node rootnode = null;
    static List<node> z = new List<node>();
    
  1. To generate a node with the data, we need to write a function called newnode that will do so. Initially, both of this new node’s children are null.

    static node newnode(int val) {
      node y = new node();
      y.key = val;
      y.leftn = null;
      y.rightn = null;
      return y;
    }
    
  2. Now, we’ll see whether the node is accessible by using the if check. If yes, then the current node’s left child will be used.

    if (rootnode == null) {
      rootnode = node;
    }
    
  3. If available, the current node child is utilized. Since this node’s left child has already been used, the right child is utilized.

    else if (z[0].leftn == null) {
      z[0].leftn = node;
    }
    
  4. The address of a newly added node in the tree is added to the queue. Therefore, it may be used to store information about its children’s nodes.

    else {
      z[0].rightn = node;
      z.RemoveAt(0);
    }
    z.Add(node);
    
  5. The following class will be used to construct a tree.

    static void Constructtree(int[] ar, int a) {
      for (int i = 0; i < a; i++) InsrtVal(ar[i]);
    }
    
  6. The following function will organize the nodes according to their level.

    static void OrderData(node root) {}
    
  7. Finally, we’ll call out the functions needed to create and organize a tree and its parameters.

    static void Main() {
      int[] array = { 29, 39, 49, 59, 69, 79 };
      int n = array.Length;
      Constructtree(array, n);
      OrderData(rootnode);
      Console.ReadKey();
    }
    

Complete Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tree_example {
  class Shanii {
    class node {
      public int key;
      public node leftn, rightn;
    };
    static node rootnode = null;
    static List<node> z = new List<node>();

    static node newnode(int val) {
      node y = new node();
      y.key = val;
      y.leftn = null;
      y.rightn = null;
      return y;
    }

    static void InsrtVal(int newval) {
      node node = newnode(newval);
      if (rootnode == null) {
        rootnode = node;
      }

      else if (z[0].leftn == null) {
        z[0].leftn = node;
      }

      else {
        z[0].rightn = node;
        z.RemoveAt(0);
      }
      z.Add(node);
    }

    static void Constructtree(int[] ar, int a) {
      for (int i = 0; i < a; i++) InsrtVal(ar[i]);
    }

    static void OrderData(node root) {
      if (root == null)
        return;
      List<node> n = new List<node>();
      n.Add(root);
      while (n.Count > 0) {
        Console.Write(n[0].key + " ");
        if (n[0].leftn != null)
          n.Add(n[0].leftn);
        if (n[0].rightn != null)
          n.Add(n[0].rightn);
        n.RemoveAt(0);
      }
    }

    static void Main() {
      int[] array = { 29, 39, 49, 59, 69, 79 };
      int n = array.Length;
      Constructtree(array, n);
      OrderData(rootnode);
      Console.ReadKey();
    }
  }
}

Output:

29 39 49 59 69 79 
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn