C# 未在二进制搜索树C中添加节点#

C# 未在二进制搜索树C中添加节点#,c#,.net,binary-search-tree,nullreferenceexception,C#,.net,Binary Search Tree,Nullreferenceexception,我尝试在我的BST中显示节点,但它们没有显示,因此我尝试检查它们是否在第一时间被添加到BST中 using System; namespace Binary_Search_Tree { class Program { static void Main(string[] args) { BinarySearchTree bst = new BinarySearchTree(7); bst.Add(ne

我尝试在我的BST中显示节点,但它们没有显示,因此我尝试检查它们是否在第一时间被添加到BST中

using System;

namespace Binary_Search_Tree
{
    class Program
    {
        static void Main(string[] args)
        {
            BinarySearchTree bst = new BinarySearchTree(7);
            bst.Add(new int[] { 4, 15, 2, 6, 14, 16, 10 });

            //bst.preOrderTraversal(bst.root);
            Console.Write(bst.root.left.right);
        }
    }
}

但这是我的错误: 未处理的异常。System.NullReferenceException:对象引用未设置为对象的实例。 在Binary_Search_Tree.Program.Main处(字符串[]args)

未将节点添加到二进制搜索树中

这是二进制搜索树类:

using System;
namespace Binary_Search_Tree
{
    public class BinarySearchTree
    {
        public Node root { get; set; }
        Node currentNode;
        public BinarySearchTree(int data)
        {
            root = new Node(data);
            currentNode = root;
        }

        public void Add(int data)
        {
            Node newNode = new Node(data);
            currentNode = root;
            while (currentNode != null)
            {
                if (newNode.data <= currentNode.data)
                {
                    currentNode = currentNode.left;
                }
                else
                {
                    currentNode = currentNode.right;
                }
            }
            currentNode = newNode;

        }

        public void Add(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Add(array[i]);
            }
        }

        public void preOrderTraversal(Node node)
        {
            if (node != null)
            {
                Console.Write(node.data + " ");
                preOrderTraversal(node.left);
                preOrderTraversal(node.right);
            }
        }
    }
}


你们能告诉我是什么问题吗?

添加节点出错了。我不打算解决所有问题,但代码应该如下所示。下面的代码没有经过测试,只是用来显示问题所在的位置

       public void Add(int data)
        {
            Node newNode = new Node(data);
            currentNode = root;
            while (currentNode != null)
            {
                if (newNode.data <= currentNode.data)
                {
                    currentNode.left = newNode;
                }
                else
                {
                    currentNode.right = currentNode;
                }
            }
            currentNode = newNode;

        }
public void Add(整数数据)
{
Node newNode=新节点(数据);
currentNode=root;
while(currentNode!=null)
{

如果(newNode.data稍微更改了Add函数,现在它可以工作了,我仍然不明白最初的问题是什么

public void Add(int data)
        {
            Node newNode = new Node(data);
            currentNode = root;
            while (currentNode != null)
            {
                if (newNode.data <= currentNode.data)
                {
                    if (currentNode.left == null)
                    {
                        currentNode.left = newNode;
                        return;
                    }
                    else
                    {
                        currentNode = currentNode.left;
                    }
                }
                else
                {
                    if (currentNode.right == null)
                    {
                        currentNode.right = newNode;
                        return;
                    }
                    else
                    {
                        currentNode = currentNode.right;
                    }
                }
            }

public void Add(整数数据)
{
Node newNode=新节点(数据);
currentNode=root;
while(currentNode!=null)
{

如果(NeNoDe.DATA

开始),你的Add Noad方法会有一些问题。同时,考虑你的控制台输出它自己的方法。

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

namespace BST
{
   public class BinarySearchTree
    {
        public Node root;
        public class Node //create a node class within our BinarySearchTree class
        {
            public int NodeData;
            public Node Left;
            public Node Right;

            public void WriteNode(int i) //writes our current node value to the console
            {
                NodeData = i;
                Console.Write(NodeData + "," + " ");
            }
        }
        public BinarySearchTree() //BinarySearchTree construct
        {
            root = null; //assigns the root node within the construct of the BinarySearchTree class
        }

        public void Insert(int i)
        {
            Node newNode = new Node
            {
                NodeData = i
            };

                if (root == null) //begin tree traversal starting at root. Since its pre-order traversal, the order is root->left->right
                {
                root = newNode;
                newNode.WriteNode(i);
                }
            else //root has a value, so begin traversing to find the next available node
            {
                Node current = root;
                Node parent;
                
                while(true)
                {
                    parent = current;
                    if (i < current.NodeData) //traverse down the left side of the tree
                    {
                        current = current.Left;
                        if (current == null)
                        {
                            parent.Left = newNode;
                            newNode.WriteNode(i);
                            break;
                        }
                        else //left node has a value, lets traverse right
                        {
                            current = current.Right;
                            if (current == null)
                            {
                                parent.Right = newNode;
                                newNode.WriteNode(i);
                                break;
                            }
                        }
                    }
                    else if (i > current.NodeData) //current node value cannot be assigned to a left child node if its value is > than its parent or root node. Traverse right
                    {
                        current = current.Right;
                        if (current == null)
                        {
                            parent.Right = newNode;
                            newNode.WriteNode(i);
                            break;
                        }
                    }
                }
            }
            
        }

        static void Main(string[] args)
        {
            BinarySearchTree BSTnums = new BinarySearchTree();
            int[] Numbers = { 45, 10, 32, 26, 8, 54, 32, 19, 12, 15, 18, 11, 35, 46 }; //Numbers array to build tree
            int numLength = Numbers.Length;

            for (int x = 0; x < numLength; x++) //iterates the length of the unsorted array, generates Binary Search Tree based on [] Numbers array.
            {
                BSTnums.Insert(Numbers[x]);
            }
            Console.Read();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间BST
{
公共类二进制搜索树
{
公共节点根;
公共类节点//在BinarySearchTree类中创建一个节点类
{
野田公共酒店;
公共节点左;
公共节点权;
public void WriteNode(int i)//将当前节点值写入控制台
{
NodeData=i;
Console.Write(NodeData+”,“+”);
}
}
public BinarySearchTree()//BinarySearchTree构造
{
root=null;//在BinarySearchTree类的构造中分配根节点
}
公共空白插入(int i)
{
Node newNode=新节点
{
NodeData=i
};
if(root==null)//从根开始树遍历。由于它的预顺序遍历,顺序是root->left->right
{
根=新节点;
newNode.WriteNode(i);
}
else//root有一个值,因此开始遍历以查找下一个可用节点
{
节点电流=根;
节点父节点;
while(true)
{
父项=当前;
if(icurrent.NodeData)//如果左子节点的值大于其父节点或根节点,则无法将当前节点值分配给左子节点。向右遍历
{
电流=电流。右;
如果(当前==null)
{
parent.Right=newNode;
newNode.WriteNode(i);
打破
}
}
}
}
}
静态void Main(字符串[]参数)
{
BinarySearchTree BSTnums=新的BinarySearchTree();
int[]Numbers={45,10,32,26,8,54,32,19,12,15,18,11,35,46};//用于构建树的数字数组
int numLength=Numbers.Length;
for(int x=0;x
你的主要方法是什么样的?它已经贴在顶部了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BST
{
   public class BinarySearchTree
    {
        public Node root;
        public class Node //create a node class within our BinarySearchTree class
        {
            public int NodeData;
            public Node Left;
            public Node Right;

            public void WriteNode(int i) //writes our current node value to the console
            {
                NodeData = i;
                Console.Write(NodeData + "," + " ");
            }
        }
        public BinarySearchTree() //BinarySearchTree construct
        {
            root = null; //assigns the root node within the construct of the BinarySearchTree class
        }

        public void Insert(int i)
        {
            Node newNode = new Node
            {
                NodeData = i
            };

                if (root == null) //begin tree traversal starting at root. Since its pre-order traversal, the order is root->left->right
                {
                root = newNode;
                newNode.WriteNode(i);
                }
            else //root has a value, so begin traversing to find the next available node
            {
                Node current = root;
                Node parent;
                
                while(true)
                {
                    parent = current;
                    if (i < current.NodeData) //traverse down the left side of the tree
                    {
                        current = current.Left;
                        if (current == null)
                        {
                            parent.Left = newNode;
                            newNode.WriteNode(i);
                            break;
                        }
                        else //left node has a value, lets traverse right
                        {
                            current = current.Right;
                            if (current == null)
                            {
                                parent.Right = newNode;
                                newNode.WriteNode(i);
                                break;
                            }
                        }
                    }
                    else if (i > current.NodeData) //current node value cannot be assigned to a left child node if its value is > than its parent or root node. Traverse right
                    {
                        current = current.Right;
                        if (current == null)
                        {
                            parent.Right = newNode;
                            newNode.WriteNode(i);
                            break;
                        }
                    }
                }
            }
            
        }

        static void Main(string[] args)
        {
            BinarySearchTree BSTnums = new BinarySearchTree();
            int[] Numbers = { 45, 10, 32, 26, 8, 54, 32, 19, 12, 15, 18, 11, 35, 46 }; //Numbers array to build tree
            int numLength = Numbers.Length;

            for (int x = 0; x < numLength; x++) //iterates the length of the unsorted array, generates Binary Search Tree based on [] Numbers array.
            {
                BSTnums.Insert(Numbers[x]);
            }
            Console.Read();
        }
    }
}