I was just having a simple implementation query.
So I make a BST by using the following code:
class Node{
int data;
Node left=null;
Node right=null;
Node link=null;
public Node(int d)
{
data=d;
}
public void append(int d)
{
Node n=this;
Node nval=new Node(d);
if(n==null)
{
n.data=d;
}
else
{ boolean done=false;
while(!done)
{
if(n.data<=d)
{
if(n.left==null)
{
n.left=nval;
done=true;
System.out.println("Data Entered "+nval.data);
}
else
{
n=n.left;
}
}
else
if(n.data>d)
{
if(n.right==null)
{
n.right=nval;
done=true;
System.out.println("Data Entered "+nval.data);
}
else
{
n=n.right;
}
}
}
}
}
}
Now, I started applying Breadth first and Depth First search on it. I was having genuine issues with doing this.
For DFS, I have to add in the left and right values of the current node that is placed on a stack right? How would I program this? I was having problems with doing this using a Linked List? Can someone tell me how the data structure or pointers should be?
The same thing happens with BFS. In case I wasn't clear before, my main problem is removing an array element and then replacing it with its children.
if (n == null) { n.data = d; }Are you trying to getNullPointerExceptions or is this a typo?Node n = this;so n can never be null.