【问题标题】:InOrder traversal is only going to the first left node then an error?InOrder 遍历只到第一个左节点然后出错?
【发布时间】:2019-05-02 02:46:06
【问题描述】:

我正在尝试对 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);
        }

    }

【问题讨论】:

    标签: c# binary-search-tree inorder


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      相关资源
      最近更新 更多