Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象在没有任何赋值的情况下自动更新_C#_Class_Object_Data Structures_Binary Tree - Fatal编程技术网

C# 对象在没有任何赋值的情况下自动更新

C# 对象在没有任何赋值的情况下自动更新,c#,class,object,data-structures,binary-tree,C#,Class,Object,Data Structures,Binary Tree,我用C#编写了一个简单的程序,在二叉树中添加一个节点。 我有一个对象字段“Root”来保存主父节点。这样,每次添加节点时,我都通过检索父节点中的数据来遍历树 这是我的密码 public class BTNode { public int Value { get; set; } public BTNode Left { get; set; } public BTNode Right { get; set; }

我用C#编写了一个简单的程序,在二叉树中添加一个节点。 我有一个对象字段“Root”来保存主父节点。这样,每次添加节点时,我都通过检索父节点中的数据来遍历树

这是我的密码

public class BTNode
    {
        public int Value { get; set; }        
        public BTNode Left { get; set; }
        public BTNode Right { get; set; }
        public BTNode Root { get; set; }
   }


public class BinaryTree
   {
       public  BinaryTree()
        {
            size = 0;
            Root = null; //To hold the main Parent Node
         }
        int size;
        object Root;

        public void AddNode(int value)
        {
            BTNode NewNode = new BTNode();
            if (Root != null)
            {
                NewNode = (BTNode)Root; //If tree exists, Get the Root Node
            }

            while (NewNode.Root != null)
            {
                if (value < NewNode.Value)
                {
                    NewNode.Root = NewNode.Left;
                }
                else if (value > NewNode.Value)
                {
                    NewNode.Root = NewNode.Right;
                }
            }

            size++;
            NewNode.Value = value;   //OBJECT 'ROOT' GETTING UPDATED AT THIS POINT
            NewNode.Root = NewNode;  //self pointer 
            NewNode.Left = null;
            NewNode.Right = null;

            if (size == 1)
            {
                Root = (object) NewNode;  //if this is the Parent Node(First Node) 
            }                             //then update the Root to be the parent Node
        }
    }
公共类BTNode
{
公共int值{get;set;}
公共BTNode左{get;set;}
公共BTNode权限{get;set;}
公共BTNode根{get;set;}
}
公共类二叉树
{
公共二叉树()
{
尺寸=0;
Root=null;//保存主父节点
}
整数大小;
对象根;
公共void AddNode(int值)
{
BTNode NewNode=新的BTNode();
if(Root!=null)
{
NewNode=(BTNode)Root;//如果树存在,则获取根节点
}
while(NewNode.Root!=null)
{
if(值NewNode.value)
{
NewNode.Root=NewNode.Right;
}
}
大小++;
NewNode.Value=Value;//此时正在更新对象“ROOT”
NewNode.Root=NewNode;//自指针
NewNode.Left=null;
NewNode.Right=null;
如果(大小==1)
{
Root=(object)NewNode;//如果这是父节点(第一个节点)
}//然后将根节点更新为父节点
}
}
我想在“根”中只保留二叉树的父节点。。我只想在size=1时执行最后一行,即如果它是树的第一个节点,但无论我做什么,根都会为每个节点更新。我正努力想知道为什么会发生这种情况,请帮助我。我有没有遗漏任何线索,逻辑

  • 您的
    Root
    属性可以键入到
    BTNode
    。这样你就不用投了
  • NewNode=(BTNode)根正在获取根节点引用。对
    NewNode
    所做的任何更改都将影响根节点。您是否知道值类型引用类型
  • 我不明白为什么要向上(检查
    根节点)而不是向下(检查左或右节点)
  • 请检查这个解决方案。它使用简单的递归方法放置新节点:

    public class BinaryTree
        {
            public BinaryTree()
            {
                size = 0;
                Root = null; //To hold the main Parent Node
            }
            int size;
            BTNode Root;
    
            public void AddNode(int value)
            {
                size++;
                BTNode NewNode = new BTNode()
                {
                    Value = value
                };
    
                if (this.Root == null)
                {
                    this.Root = NewNode;
                    return;
                }
    
                this.PlaceNewNode(this.Root, NewNode);
            }
    
            public void PlaceNewNode(BTNode RootNode, BTNode NewNode)
            {
                if (NewNode.Value < RootNode.Value)
                {
                    if (RootNode.Left != null)
                    {
                        PlaceNewNode(RootNode.Left, NewNode);
                    }
                    else
                    {
                        RootNode.Left = NewNode;
                        return;
                    }
                }
                else
                {
                    if (RootNode.Right != null)
                    {
                        PlaceNewNode(RootNode.Right, NewNode);
                    }
                    else
                    {
                        RootNode.Right = NewNode;
                        return;
                    }
                }
            }
        }
    
    公共类二进制树
    {
    公共二叉树()
    {
    尺寸=0;
    Root=null;//保存主父节点
    }
    整数大小;
    节点根;
    公共void AddNode(int值)
    {
    大小++;
    BTNode NewNode=新的BTNode()
    {
    价值=价值
    };
    if(this.Root==null)
    {
    this.Root=NewNode;
    返回;
    }
    this.PlaceNewNode(this.Root,NewNode);
    }
    public void PlaceNewNode(BTNode RootNode,BTNode NewNode)
    {
    if(NewNode.Value
    希望这有帮助


    关于

    我认为你用词根这个词令人困惑。树只能有一个根对象。节点对象可以有父节点(除非它是根节点)。树结构非常适合递归代码

    这里有一些代码与Andre发布的代码类似

    public class BTNode
    {
        public BTNode() { }
        public BTNode(int value) { Value = value; }
    
        public int Value { get; set; }
        public BTNode Left { get; set; }
        public BTNode Right { get; set; }
        public BTNode Parent { get; set; }
    }
    
    public class BinaryTree
    {
        public BinaryTree() { }
    
        public BTNode Root { get; private set; }
        public int Size { get; private set; }
    
        public void AddNode(int value)
        {
            BTNode insertNode = new BTNode(value);
            if (Root == null)
                Root = insertNode;
            else
                AddNodeToTree(Root, insertNode);
            Size++;
        }
    
        private void AddNodeToTree(BTNode parentNode, BTNode insertNode)
        {
            if (insertNode.Value >= parentNode.Value)
            {
                if (parentNode.Right != null)
                    AddNodeToTree(parentNode.Right, insertNode);
                else
                {
                    parentNode.Right = insertNode;
                    insertNode.Parent = parentNode;
                }
            }
            else
            {
                if (parentNode.Left != null)
                    AddNodeToTree(parentNode.Left, insertNode);
                else
                {
                    parentNode.Left = insertNode;
                    insertNode.Parent = parentNode;
                }
            }
        }
    
        public BTNode FindNode(int value)
        {
            return FindNode(Root, value);
        }
    
        public BTNode FindNode(BTNode parent, int value)
        {
            BTNode node = null;
            if (parent != null)
            {
                if (parent.Value == value)
                    node = parent;
                else
                {
                    if (parent.Value < value)
                        node = FindNode(parent.Right, value);
                    else
                        node = FindNode(parent.Left, value);
                }
            }
            return node;
        }
    }
    
    公共类BTNode
    {
    公共BTNode(){}
    公共BTNode(int值){value=value;}
    公共int值{get;set;}
    公共BTNode左{get;set;}
    公共BTNode权限{get;set;}
    公共BTNode父节点{get;set;}
    }
    公共类二叉树
    {
    公共二进制树(){}
    公共BTNode根{get;private set;}
    公共整数大小{get;私有集;}
    公共void AddNode(int值)
    {
    BTNode insertNode=新BTNode(值);
    if(Root==null)
    根=插入节点;
    其他的
    AddNodeToTree(根,插入节点);
    大小++;
    }
    私有void AddNodeToTree(BTNode parentNode、BTNode insertNode)
    {
    if(insertNode.Value>=parentNode.Value)
    {
    if(parentNode.Right!=null)
    AddNodeToTree(parentNode.Right,insertNode);
    其他的
    {
    parentNode.Right=insertNode;
    insertNode.Parent=parentNode;
    }
    }
    其他的
    {
    if(parentNode.Left!=null)
    AddNodeToTree(parentNode.Left,insertNode);
    其他的
    {
    parentNode.Left=插入节点;
    insertNode.Parent=parentNode;
    }
    }
    }
    公共BTNode FindNode(int值)
    {
    返回FindNode(根,值);
    }
    公共BTNode FindNode(BTNode父节点,int值)
    {
    BTNode=null;
    如果(父项!=null)
    {
    if(parent.Value==值)
    节点=父节点;
    其他的
    {
    if(parent.Value