如何更正BST C#代码中的隐式转换错误? [英] How do I correct an implicit conversion error in BST C# Code?

查看:48
本文介绍了如何更正BST C#代码中的隐式转换错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码

我有这些错误

在findmin上不能将类型x.Program.TreeNode'隐式转换为'int'//p

在findmax上不能将类型x.Program.TreeNode'隐式转换为'int'//p

我的主要知识是否正确或缺失?

以及如何计数节点,离开并获得最高值(仅需要提示)

class Program
    {
        static void Main(string[] args)
        {
            BinarySearchTree t = new BinarySearchTree();

            t.insert(ref t.root, 10);
            t.insert(ref t.root, 5);
            t.insert(ref t.root, 6);
            t.insert(ref t.root, 17);
            t.insert(ref t.root, 2);
            t.insert(ref t.root, 3);

            BinarySearchTree.print(t.root);
        }

        public class TreeNode
        {
            public int n;
            public TreeNode _left;
            public TreeNode _right;


            public TreeNode(int n, TreeNode _left, TreeNode _right)
            {
                this.n = n;
                this._left = _left;
                this._right = _right;
            }


            public void DisplayNode()
            {
                Console.Write(n);
            }
        }


        public class BinarySearchTree
        {
            public TreeNode root;

            public BinarySearchTree()
            {
                root = null;
            }


            public void insert(ref TreeNode root, int x)
            {
                if (root == null)
                {
                    root = new TreeNode(x, null, null);
                }
                else
                    if (x < root.n)
                        insert(ref root._left, x);
                    else
                        insert(ref root._right, x);
            }

            public int FindMin()
            {
                TreeNode current = root;

                while (current._left != null)
                    current = current._left;

                return current;
            }

            public int FindMax()
            {
                TreeNode current = root;

                while (current._right != null)
                    current = current._right;

                return current;
            }

            public TreeNode Find(int key)
            {
                TreeNode current = root;

                while (current.n != key)
                {
                    if (key < current.n)
                        current = current._left;
                    else
                        current = current._right;
                    if (current == null)
                        return null;
                }
                return current;
            }



            public void InOrder(ref TreeNode root)
            {
                if (root != null)
                {
                    InOrder(ref root._left);
                    root.DisplayNode();
                    InOrder(ref root._right);
                }
            }


            public static void print(TreeNode root)
            {
                if (root != null)
                {
                    print(root._left);
                    Console.WriteLine(root.n.ToString());
                    print(root._right);
                }

            }

解决方案

因为您需要(FindMin/FindMax)返回int,所以您是说current.n吗?


已更新:用于计算叶子和节点,怎么样(作为TreeNode的实例方法):

    public int CountNodes()
    {
        int count = 1; // me!
        if (_left != null) count += _left.CountNodes();
        if (_right != null) count += _right.CountNodes();
        return count;
    }

    public int CountLeaves()
    {
        int count = (_left == null && _right == null) ? 1 : 0;
        if (_left != null) count += _left.CountLeaves();
        if (_right != null) count += _right.CountLeaves();
        return count;
    }

如果我错过了重点,请告诉我...

I wrote this code

I have these errors

Cannot implicitly convert type x.Program.TreeNode' to 'int' // on findmin

Cannot implicitly convert type x.Program.TreeNode' to 'int' // on findmax

and is my main correct or missing somethin?

and how i can count the nodes,leaves and get the hight (need only hints)

class Program
    {
        static void Main(string[] args)
        {
            BinarySearchTree t = new BinarySearchTree();

            t.insert(ref t.root, 10);
            t.insert(ref t.root, 5);
            t.insert(ref t.root, 6);
            t.insert(ref t.root, 17);
            t.insert(ref t.root, 2);
            t.insert(ref t.root, 3);

            BinarySearchTree.print(t.root);
        }

        public class TreeNode
        {
            public int n;
            public TreeNode _left;
            public TreeNode _right;


            public TreeNode(int n, TreeNode _left, TreeNode _right)
            {
                this.n = n;
                this._left = _left;
                this._right = _right;
            }


            public void DisplayNode()
            {
                Console.Write(n);
            }
        }


        public class BinarySearchTree
        {
            public TreeNode root;

            public BinarySearchTree()
            {
                root = null;
            }


            public void insert(ref TreeNode root, int x)
            {
                if (root == null)
                {
                    root = new TreeNode(x, null, null);
                }
                else
                    if (x < root.n)
                        insert(ref root._left, x);
                    else
                        insert(ref root._right, x);
            }

            public int FindMin()
            {
                TreeNode current = root;

                while (current._left != null)
                    current = current._left;

                return current;
            }

            public int FindMax()
            {
                TreeNode current = root;

                while (current._right != null)
                    current = current._right;

                return current;
            }

            public TreeNode Find(int key)
            {
                TreeNode current = root;

                while (current.n != key)
                {
                    if (key < current.n)
                        current = current._left;
                    else
                        current = current._right;
                    if (current == null)
                        return null;
                }
                return current;
            }



            public void InOrder(ref TreeNode root)
            {
                if (root != null)
                {
                    InOrder(ref root._left);
                    root.DisplayNode();
                    InOrder(ref root._right);
                }
            }


            public static void print(TreeNode root)
            {
                if (root != null)
                {
                    print(root._left);
                    Console.WriteLine(root.n.ToString());
                    print(root._right);
                }

            }

解决方案

Since you need (FindMin/FindMax) to return an int, do you mean current.n?


Updated: for counting leaves and nodes, how about (as instance methods of TreeNode):

    public int CountNodes()
    {
        int count = 1; // me!
        if (_left != null) count += _left.CountNodes();
        if (_right != null) count += _right.CountNodes();
        return count;
    }

    public int CountLeaves()
    {
        int count = (_left == null && _right == null) ? 1 : 0;
        if (_left != null) count += _left.CountLeaves();
        if (_right != null) count += _right.CountLeaves();
        return count;
    }

If I've missed the point, let me know...

这篇关于如何更正BST C#代码中的隐式转换错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆