当前位置: 代码网 > it编程>编程语言>C# > C#使用自定义的泛型节点类实现二叉树类

C#使用自定义的泛型节点类实现二叉树类

2024年05月18日 C# 我要评论
一、涉及到的知识点1.comparer<t>.default 属性返回由泛型参数指定的类型的默认排序顺序比较器。public static system.collections.gener

一、涉及到的知识点

1.comparer<t>.default 属性

返回由泛型参数指定的类型的默认排序顺序比较器。

public static system.collections.generic.comparer<t> default { get; }

属性值

comparer<t>

继承 comparer<t> 并作为 t 类型的排序顺序比较器的对象。

comparer<t>.default 属性是 c# 中 system.collections.generic命名空间下的一个属性。它返回一个 comparer<t> 对象的默认实例,该对象可以对泛型集合中的对象进行比较。默认情况下,这个比较器根据对象的自然顺序进行比较,即通过调用对象的 compareto 方法进行比较。

// comparer<t>.default 属性
 
namespace _135_3
{
    public class program
    {
        public static void main(string[] args)
        {
            argumentnullexception.throwifnull(args);
 
            list<int> numbers = [3, 1, 4, 2];
 
            // 使用默认比较器对集合进行排序
            numbers.sort(comparer<int>.default);
 
            console.writeline(string.join(", ", numbers));
        }
    }
}
//运行结果:
/*
1, 2, 3, 4
 */

在这个例子中创建了一个包含整数的列表。然后,使用 comparer<int>.default 属性提供的默认比较器对列表进行排序。最后,输出排序后的列表,可以看到数字已经按照升序排列。

2.实现二叉树类binarytree<t>步骤

(1)先设计一个泛型节点类

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)再设计一个泛型的二叉树类

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>.default.compare(value, current.data) < 0)
                {
                    if (current.left == null)
                    {
                        current.left = newnode;
                        break;
                    }
                    current = current.left;
                }
                else
                {
                    if (current.right == null)
                    {
                        current.right = newnode;
                        break;
                    }
                    current = current.right;
                }
            }
        }
    }
}

(3)最后设计main方法

定义一个二叉树类的对象,引用类中的方法。

binarytree<int> tree = new();

二、 使用泛型节点类 node<t>实现二叉树类binarytree<t>

// 使用泛型节点类 node<t>设计实现二叉树类
namespace _135_1
{
    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;
    }
 
    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>.default.compare(value, current.data) < 0)
                    {
                        if (current.left == null)
                        {
                            current.left = newnode;
                            break;
                        }
                        current = current.left;
                    }
                    else
                    {
                        if (current.right == null)
                        {
                            current.right = newnode;
                            break;
                        }
                        current = current.right;
                    }
                }
            }
        }
    }
 
    class program
    {
        static void main(string[] args)
        {
            argumentnullexception.throwifnull(args);
 
            binarytree<int> tree = new();
            tree.addnode(5);
            tree.addnode(3);
            tree.addnode(8);
            tree.addnode(1);
            tree.addnode(4);
            tree.addnode(7);
 
            console.writeline("中序遍历:");
            printinorder(tree.root!);
 
            console.writeline("前序遍历:");
            printpreorder(tree.root!);
 
            console.writeline("后序遍历:");
            printpostorder(tree.root!);
 
            console.readkey();
        }
 
        static void printinorder(node<int> node)
        {
            if (node != null)
            {
                printinorder(node.left!);
                console.writeline(node.data);
                printinorder(node.right!);
            }
        }
 
        static void printpreorder(node<int> node)
        {
            if (node != null)
            {
                console.writeline(node.data);
                printpreorder(node.left!);
                printpreorder(node.right!);
            }
        }
 
        static void printpostorder(node<int> node)
        {
            if (node != null)
            {
                printpostorder(node.left!);
                printpostorder(node.right!);
                console.writeline(node.data);
            }
        }
    }
}

运行结果:

中序遍历:
1
3
4
5
7
8
前序遍历:
5
3
1
4
8
7
后序遍历:
1
4
3
7
8
5

在这个实例中使用 comparer<t>.default 来比较两个值的大小。这个方法适用于任何实现了 system.icomparable<t> 接口的类型,因此可以使用任何实现了该接口的值类型或引用类型。

这个程序的主要功能是添加一个新的节点到二叉树中。它首先检查根节点是否为空,如果为空,则将新的节点设置为根节点。否则,它将从根节点开始,递归地遍历二叉树,找到合适的位置插入新的节点。

这个程序的实现是正确的,它可以用于存储和操作实现了 system.icomparable<t> 接口的类型。可以根据需要修改和扩展这个程序,例如,可以添加其他方法来遍历和操作二叉树。

以上就是c#使用自定义的泛型节点类实现二叉树类的详细内容,更多关于c#二叉树类的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com