Как исправить ошибку неявного преобразования в коде BST C#?

StackOverflow https://stackoverflow.com/questions/406402

  •  03-07-2019
  •  | 
  •  

Вопрос

Я написал этот код

у меня есть такие ошибки

Невозможно неявно преобразовать тип x.Program.TreeNode' в 'int' // в findmin

Невозможно неявно преобразовать тип x.Program.TreeNode' в 'int' // в findmax

и мой главный правильный или чего-то не хватает?

и как я могу посчитать узлы, листья и получить высоту (нужны только подсказки)

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;
    }

Если я упустил суть, дайте мне знать...

Другие советы

Твой FindMin() и FindMax() методы пытаются вернуть TreeNode объекты, но подпись говорит, что они возвращаются intс.Поскольку эти методы не используются в программе, возможно, вы могли бы их удалить?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top