简体   繁体   English

程序不打印输出任何人都可以帮助

[英]the program not print output anyone can help

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Node
    {
        public int Data;
        public Node Left;
        public Node Right;

        public void DisplayNode()
        {
            Console.Write(Data + " ");
        }
    }

    public class BinarySearchTree
    {
        public Node root;
        public BinarySearchTree()
        {
            root = null;
        }
        public void Insert(int i)
        {
            Node newNode = new Node();
            newNode.Data = i;
            if (root == null)
                root = newNode;
            else
            {
                Node current = root;
                Node parent;
                while (true)
                {
                    parent = current;
                    if (i < current.Data)
                    {
                        current = current.Left;
                        if (current == null)
                        {
                            parent.Left = newNode;
                            break;
                        }
                        else
                        {
                            current = current.Right;
                            if (current == null)
                            {
                                parent.Right = newNode;
                                break;
                            }
                        }
                    }
                }
            }
        }


            public void InOrder(Node theRoot)
            {
                if (!(theRoot == null))
                {
                    InOrder(theRoot.Left);
                    theRoot.DisplayNode();
                    InOrder(theRoot.Right);
                }
            }


            static void Main()
            {
                BinarySearchTree nums = new BinarySearchTree();
                nums.Insert(23);
                nums.Insert(45);
                nums.Insert(16);
                nums.Insert(37);
                nums.Insert(3);
                nums.Insert(99);
                nums.Insert(22);
                Console.WriteLine("Inorder traversal: "); 
                nums.InOrder(nums.root);

            }

        }
    }

You've got an infinite loop in your Insert function; 你的Insert函数中有一个无限循环; you're handling the case where i < current.data but if i >= current.data then it gets stuck in the while(true) . 你正在处理i < current.data的情况,但如果i >= current.data则会陷入while(true) I think you need to move the current = current.right code up a nesting level, ie 我认为你需要将current = current.right代码移动到嵌套级别,即

    while (true)
    {
      parent = current;
      if (i < current.Data)
      {
        current = current.Left;
        if (current == null)
        {
          parent.Left = newNode;
          break;
        }
      }
      else
      {
        current = current.Right;
        if (current == null)
        {
          parent.Right = newNode;
          break;
        }
      }
    }

Note the else is now if (i < current.Data) ... else not if (current == null) ... else . 注意else现在是if (i < current.Data) ... else不是if (current == null) ... else

But really you need to learn to use the debugger to diagnose this sort of thing yourself. 但实际上你需要学习使用调试器来自己诊断这类事情。

也许是的

Console.Write(Data.ToString() + " ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM