C# 二叉搜索树;堆栈";解释

C# 二叉搜索树;堆栈";解释,c#,return,binary-search-tree,C#,Return,Binary Search Tree,我在C#中实现了一个非常简单的BST。守则: class Node { public int? value; public Node parent; public Node left; public Node right; public Node(int? value, Node parent = null) { this.value = value; this.parent = parent; } }

我在C#中实现了一个非常简单的BST。守则:

class Node
{
    public int? value;
    public Node parent;
    public Node left;
    public Node right;

    public Node(int? value, Node parent = null)
    {
        this.value = value;
        this.parent = parent;
    }
}

class BST
{
    public Node root;
    public List<Node> tree = new List<Node>();

    public BST(int? value = null)
    {
        if (value != null)
        {
            this.root = new Node(value);
            this.tree.Add(this.root);
        }
    }

    public Node insert(int value)
    {
        Node node = this.root;

        if (node == null)
        {
            this.root = new Node(value);
            this.tree.Add(this.root);

            return this.root;
        }

        while (true)
        {
            if (value > node.value)
            {
                if (node.right != null)
                {
                    node = node.right;
                }
                else
                {
                    node.right = new Node(value, node);
                    node = node.right;
                    break;
                }
            }
            else if (value < node.value)
            {
                if (node.left != null)
                {
                    node = node.left;
                }
                else
                {
                    node.left = new Node(value, node);
                    node = node.left;
                    break;
                }
            }
            else
            {
                break;
            }
        }
        return node;
    }
}

class Program
{
    static void Main()
    {
        BST superTree = new BST(15);
        superTree.insert(14);
        superTree.insert(25);
        superTree.insert(2);
    }
}
类节点
{
公共价值;
公共节点父节点;
公共节点左;
公共节点权;
公共节点(int?值,节点父节点=null)
{
这个值=值;
this.parent=parent;
}
}
BST级
{
公共节点根;
公共列表树=新列表();
公共BST(int?value=null)
{
if(值!=null)
{
this.root=新节点(值);
this.tree.Add(this.root);
}
}
公共节点插入(int值)
{
Node=this.root;
if(node==null)
{
this.root=新节点(值);
this.tree.Add(this.root);
返回这个.root;
}
while(true)
{
如果(值>节点值)
{
if(node.right!=null)
{
node=node.right;
}
其他的
{
node.right=新节点(值,节点);
node=node.right;
打破
}
}
else if(值<节点值)
{
if(node.left!=null)
{
node=node.left;
}
其他的
{
node.left=新节点(值,节点);
node=node.left;
打破
}
}
其他的
{
打破
}
}
返回节点;
}
}
班级计划
{
静态void Main()
{
BST超级树=新的BST(15);
超级树.插入(14);
超级树.插入(25);
超级树。插入(2);
}
}
我的问题是关于BST类的“Insert”方法

当我在main方法中调用它时,它的“return”究竟是如何工作的? 它怎么知道把“节点”放在“左边”?我并没有在任何地方引用“root.left”,但不知怎的,它被正确地插入了

我在某一点上意识到那里发生了某种递归,但这已经有6个小时了,我仍然无法理解这个方法是如何正常工作的

我非常感谢您对“插入”方法的任何解释。谢谢

Node node = this.root;

由于这一行,您的代码总是以根开头。只有在
节点
不再为空后,
节点
才能重新分配给根以外的对象。其余的代码在
节点上工作。left
,但由于您的代码如上所述以
根开始,
节点。left
实际上在开始时引用了
根。

我不理解这个问题。“return”与其他任何函数一样,返回节点,在这里根本不使用它。代码中有两个对左节点的引用。您是否在调试器中逐行运行它以查看其中发生了什么?有一个循环遍历树并查看插入的位置。最初,node left为null,因此您可以看到以下代码行:if(node.left!=null),它将新节点置于左侧。函数insert不是递归的。while循环从根遍历树,直到找到合适的终端节点,并在该节点的末尾创建一个包含该值的新节点。请注意,
node
不是
root
对象的副本。由于
节点
类型是引用类型,因此两个变量都指向同一个对象。因此,将新节点分配给
node.Left
与将其分配给
root.Left
相同。是否确定“node”是引用类型,因为它是我创建的类,没有继承任何其他类(接口等)?所有类都是引用类型。结构都是值类型,类是引用类型。OMFG引用实际上完全解释了这种暗魔法的工作原理。谢谢:)。