节点未添加到二叉搜索树 C#
Nodes not getting added in Binary Search tree C#
我尝试在我的 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(字符串[] 参数)
节点未添加到二叉搜索树中。
这是二叉搜索树 Class :
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);
}
}
}
}
编辑:
节点 Class :
namespace Binary_Search_Tree
{
public class Node
{
public int data { get; set; }
public Node left { get; set; }
public Node right { get; set; }
public Node(int data)
{
this.data = data;
left = null;
right = null;
}
}
}
请问各位大神,请问是什么问题?
添加节点错误。我不打算解决所有问题,但代码应该如下所示。下面的代码未经测试,只是为了显示问题所在。
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(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;
}
}
}
首先,您的添加节点方法存在一些问题。另外,考虑让你的控制台输出它自己的方法。看一下我的 pre-order 遍历示例
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();
}
}
}
我尝试在我的 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(字符串[] 参数)
节点未添加到二叉搜索树中。
这是二叉搜索树 Class :
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);
}
}
}
}
编辑: 节点 Class :
namespace Binary_Search_Tree
{
public class Node
{
public int data { get; set; }
public Node left { get; set; }
public Node right { get; set; }
public Node(int data)
{
this.data = data;
left = null;
right = null;
}
}
}
请问各位大神,请问是什么问题?
添加节点错误。我不打算解决所有问题,但代码应该如下所示。下面的代码未经测试,只是为了显示问题所在。
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(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;
}
}
}
首先,您的添加节点方法存在一些问题。另外,考虑让你的控制台输出它自己的方法。看一下我的 pre-order 遍历示例
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();
}
}
}