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#:
- 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.
- 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.
-
To begin, we must have the following libraries:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
-
Create a class called
node
in theShanii
class that stores int type variableskey
,leftn
as the left node, andrightn
as the right node.class node { public int key; public node leftn, rightn; };
-
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>();
-
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 arenull
.static node newnode(int val) { node y = new node(); y.key = val; y.leftn = null; y.rightn = null; return y; }
-
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; }
-
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; }
-
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);
-
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]); }
-
The following function will organize the nodes according to their level.
static void OrderData(node root) {}
-
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
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