InOrder遍历只到第一个左节点然后出错?

InOrder traversal is only going to the first left node then an error?

我正在尝试对 BST 进行简单的中序遍历,并且 BST 构建正确,但是当我尝试打印 BST 中序时,它会转到第一个左节点,然后不会转到任何其他节点并造成溢出。

我尝试过使用 getLeft 和 getRight 进行不同的中序遍历,但它仍然在做同样的事情

class Program
    {
        static void Main(string[] args)
        {
            Tree tree = new Tree();

            List<int> rands = new List<int>();

            Random random = new Random();

            int between = random.Next(20, 30);

            for (int i = 0; i < between; i++)
            {
                rands.Add(random.Next(100));
            }

            for (int x = 0; x < rands.Count; x++)
            {
                tree.constructTree(rands[x]);
            }

            tree.Inorder(tree.returnRoot());

        }
    class Node
    {
        private int number;
        public Node left;
        public Node right;

        public Node()
        {
        }

        public Node getLeft()
        {
            return this.left;
        }

        public Node getRight()
        {
            return this.right;
        }

        public int GetSetNumber
        {
            get
            {
                return this.number;
            }
            set
            {
                this.number = value;
            }
        }

        public Node GetSetLeft
        {
            get
            {
                return this.left;
            }
            set
            {
                this.left = value;
            }
        }

        public Node GetSetRight
        {
            get
            {
                return this.right;
            }
            set
            {
                this.right = value;
            }
        }
    }



    class Tree
    {
        public Node root;

        public Node returnRoot()
        {
            return this.root;
        }

        public void constructTree(int num)
        {
            Node t = new Node();
            t.GetSetNumber = num;

            if (root == null)
            {
                root = t;
            }
            else
            {
                Node cur = root;
                Node top;

                while (true)
                {
                    top = cur;
                    if (num < top.GetSetNumber)
                    {
                        cur = cur.GetSetLeft;
                        if (cur == null)
                        {
                            top.GetSetLeft = t;
                            return;
                        }
                    }
                    else
                    {
                        cur = cur.GetSetRight;
                        if (cur == null)
                        {
                            top.GetSetRight = t;
                            return;
                        }
                    }
                }

            }
        }

        public void Inorder(Node Root)
        {
            if (root == null)
            {
                return;
            }
            Inorder(root.left);
            Console.WriteLine(root.GetSetNumber + " ");
            Inorder(root.right);
        }

    }

您在 Inorder 中引用了 root,而不是 Rootroot(小写r)是包含整棵树的根的class变量,不是你传入的Node参数。你的代码无限循环,因为你永远在同一个节点上调用 Inorder

如果您在 Inorder 中将对 root 的引用大写(或者在方法中使用不同的变量名称以避免混淆,例如 current),您应该能够做出一些进步。

    public void Inorder(Node Root)
    {
        if (Root == null)
        {
            return;
        }
        Inorder(Root.left);
        Console.WriteLine(Root.GetSetNumber + " ");
        Inorder(Root.right);
    }