Ok cant seem to find the null pointer exception error on my code one moment its working fine the next when i add the 20 i get an error, and i dont know if this is what messing my find method.
Between my tree is supposed to be a binary non search tree, and its generic. The add methods works by randomly going left or right of current node. I hope my code is understendable. Any help is appreciated i had already made another post on how to start this tree, now i guess is about how to continue.
import java.util.*;
/**
*
* @author alejandro
*/
public class GenBinTree <T>
{
//btn is the binary tree node class
private static class btn<T>
{
public T data;
public btn<T> left;
public btn<T> right;
public btn(T data)
{
this.data=data;
left=null;
right=null;
}
}
//references the root of tree
btn<T> root;
// creating a new tree with one node, the root
public GenBinTree()
{
root=null;
}
//craeting a new tree with given root data
public GenBinTree(T rootData)
{
root = new btn<> (rootData);
}
//checks if tree is empty
public boolean isEmpty()
{
return root == null;
}
public boolean hasChildren(btn<T> someRoot)
{
if(someRoot.left!=null&&someRoot.right!=null)
{
return true;
}
else
{
return false;
}
}
//adds node to the tree with d being the data being passed
public void add(T d)
{
if(this.isEmpty())
{
root = new btn<>(d);
}
else
{
add(d,root);
}
}
public void add(T d,btn<T> treeRoot)
{
//this will creat a random number of 1 or 0, mainly to randomly go left or right
Random random = new Random();
int x= random.nextInt(2);
//randomly went to the left of current root
if(x==0)
{
if(treeRoot.left ==null)
treeRoot.left = new btn<>(d);
//randomly going left or right
else if(treeRoot.left !=null)
{
x=random.nextInt(2);
if(x==0)
add(d,treeRoot.left);
else
add(d,treeRoot.right);
}
}
//randomly went to the right of current root
else
{
if (treeRoot.right == null)
treeRoot.right = new btn<>(d);
else if(treeRoot.left !=null)
{
x=random.nextInt(2);
if(x==0)
add(d,treeRoot.left);
else
add(d,treeRoot.right);
}
}
}
/*public boolean find(T d)
{
return(find(d,root));
}
public boolean find(T d,btn<T> n)
{
boolean found = false;
if(n.data==d)
{
return true;
}
else
{
find(d,n.left);
find(d,n.right);
}
return found;
}
/*public void remove(T d)
{
if(root.data.equals(d))
{
root = null;
}
else
{
remove(d, root);
}
}
public void remove(T d,btn<T> n)
{
if(n.left==d&&hasChildren(n.left)==false)
{
n.left=null;
}
else if (n.right==d&&hasChildren(n.right)==false)
{
n.right=null;
}
else if(hasChildren(n.left)==true)
{
remove(d,n.left);
}
else
{
remove(d,n.right);
}
}*/
public void Print(btn<T> n)
{
if(n!=null)
{
Print(n.left);
System.out.println(n.data);
Print(n.right);
}
}
public void Print()
{
Print(root);
}
public static void main(String[] args)
{
int x;
GenBinTree<Integer> l1 = new GenBinTree<>(10);
l1.Print();
System.out.println("************");
l1.add(50);
l1.add(20);
l1.add(60);
l1.Print();
System.out.println("************");
l1.add(50);
l1.Print();
System.out.println("************");
// System.out.println(l1.find(20));
}
}