Reputation: 67
I tried displaying the nodes in my BST but they were not displaying, so i tried to check if they even were added in the BST in the first place by
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);
}
}
}
but this is the error i got : Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. at Binary_Search_Tree.Program.Main(String[] args)
The nodes are not getting added in the Binary Search Tree.
this is the Binary Search Tree 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);
}
}
}
}
Edit: Node 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;
}
}
}
Can you guys please tell what the problem is?
Upvotes: 0
Views: 131
Reputation: 171
To start, there's some issues with your add node method. Also, consider having your console output its own method. Take a look at my pre-order traversal example here
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();
}
}
}
Upvotes: 1
Reputation: 67
changed the Add function a bit and now its working, i still don't understand what the problem was in the first place
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;
}
}
}
Upvotes: 0
Reputation: 34419
The Add node is wrong. I'm not going to fix all the issues but the code should look something like below. The code below is not tested and just meant to show where the issue is located.
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;
}
Upvotes: 0