SoFunction
Updated on 2025-03-07

C# implements binary tree class using custom generic node classes

1. Knowledge points involved

<T>.Default property

Returns the default sort order comparator for the type specified by the generic parameter.

public static <T> Default { get; }

Attribute value

Comparer<T>

An object that inherits Comparer<T> and serves as a sort order comparator of type T.

The Comparer<T>.Default property is a property under the namespace in C#. It returns a default instance of Comparer<T> object that can compare objects in a generic collection. By default, this comparator is compared according to the natural order of objects, i.e. by calling the CompareTo method of the object.

// Comparer<T>.Default property 
namespace _135_3
{
    public class Program
    {
        public static void Main(string[] args)
        {
            (args);
 
            List&lt;int&gt; numbers = [3, 1, 4, 2];
 
            // Sort the collection using the default comparator            (Comparer&lt;int&gt;.Default);
 
            ((", ", numbers));
        }
    }
}
//Run result:/*
1, 2, 3, 4
 */

In this example, a list containing integers is created. Then, sort the list using the default comparator provided by the Comparer<int>.Default property. Finally, output the sorted list and you can see that the numbers have been sorted in ascending order.

2. Implement the BinaryTree<T> step

(1) Design a generic node class first

public class Node<T>(T value)
{
    public T Data { get; set; } = value;
    public Node<T>? Left { get; set; } = null;
    public Node<T>? Right { get; set; } = null;
}

(2) Design another generic binary tree class

public class BinaryTree<T>
{
    public Node<T>? Root { get; private set; }
 
    public void AddNode(T value)
    {
        Node<T> newNode = new(value);
        if (Root == null)
        {
            Root = newNode;
        }
        else
        {
            Node<T> current = Root;
            while (true)
            {
                if (Comparer<T>.(value, ) < 0)
                {
                    if ( == null)
                    {
                         = newNode;
                        break;
                    }
                    current = ;
                }
                else
                {
                    if ( == null)
                    {
                         = newNode;
                        break;
                    }
                    current = ;
                }
            }
        }
    }
}

(3) The final design of Main method

Defines an object of a binary tree class and references methods in the class.

BinaryTree<int> tree = new();

2. Use generic node class Node<T> to implement the binary tree class BinaryTree<T>

// Use generic node class Node<T> to implement binary tree classnamespace _135_1
{
    public class Node&lt;T&gt;(T value)
    {
        public T Data { get; set; } = value;
        public Node&lt;T&gt;? Left { get; set; } = null;
        public Node&lt;T&gt;? Right { get; set; } = null;
    }
 
    public class BinaryTree&lt;T&gt;
    {
        public Node&lt;T&gt;? Root { get; private set; }
 
        public void AddNode(T value)
        {
            Node&lt;T&gt; newNode = new(value);
            if (Root == null)
            {
                Root = newNode;
            }
            else
            {
                Node&lt;T&gt; current = Root;
                while (true)
                {
                    if (Comparer&lt;T&gt;.(value, ) &lt; 0)
                    {
                        if ( == null)
                        {
                             = newNode;
                            break;
                        }
                        current = ;
                    }
                    else
                    {
                        if ( == null)
                        {
                             = newNode;
                            break;
                        }
                        current = ;
                    }
                }
            }
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            (args);
 
            BinaryTree&lt;int&gt; tree = new();
            (5);
            (3);
            (8);
            (1);
            (4);
            (7);
 
            ("In-order traversal:");
            PrintInOrder(!);
 
            ("Previous traversal:");
            PrintPreOrder(!);
 
            ("Post-order traversal:");
            PrintPostOrder(!);
 
            ();
        }
 
        static void PrintInOrder(Node&lt;int&gt; node)
        {
            if (node != null)
            {
                PrintInOrder(!);
                ();
                PrintInOrder(!);
            }
        }
 
        static void PrintPreOrder(Node&lt;int&gt; node)
        {
            if (node != null)
            {
                ();
                PrintPreOrder(!);
                PrintPreOrder(!);
            }
        }
 
        static void PrintPostOrder(Node&lt;int&gt; node)
        {
            if (node != null)
            {
                PrintPostOrder(!);
                PrintPostOrder(!);
                ();
            }
        }
    }
}

Running results:

In-order traversal:
1
3
4
5
7
8
Preamble traversal:
5
3
1
4
8
7
Post-order traversal:
1
4
3
7
8
5

In this instance, use Comparer<T>.Default to compare the sizes of two values. This method is suitable for any type that implements the <T> interface, so any value type or reference type that implements the interface can be used.

The main function of this program is to add a new node to the binary tree. It first checks whether the root node is empty, and if it is empty, sets the new node as the root node. Otherwise, it will start from the root node, recursively traverse the binary tree, finding the right place to insert the new node.

This program is implemented correctly, and it can be used to store and operate the types of the <T> interface. This program can be modified and extended as needed, for example, other methods can be added to traverse and manipulate the binary tree.

The above is the detailed content of C# using custom generic node classes to implement binary tree classes. For more information about C# binary tree classes, please pay attention to my other related articles!