i need some help with binary trees. i belive i have finish with insert and displaying the tree methods. but i can not be sure but i am getting a error in main.
error in main:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static field current_node
line which has the error in main:
m.inOrderTraversal(current_node);
i also tried changing current_node to static variable. but no luck.
public class D06_Binary_Tree
{
/*** CREATE NODE ***/
private class node
{
private String name;
private int age;
private node left_child;
private node right_child;
/*** Constructor ***/
public node()
{
name = "";
age = 07;
left_child = null;
right_child = null;
}
public node(String n, int a)
{
name = n;
age = a;
left_child = null;
right_child = null;
}/*** End of constructor ***/
}/*** End of Node class ***/
/*** Linked List Constructor ***/
private node root_node;
private node current_node;
private node parent_node;
private node new_node;
public D06_Binary_Tree()
{
// TODO Auto-generated constructor stub
}
/*** Insert Method ***/
public void insert(String n, int a)
{
current_node = root_node;
parent_node = root_node;
if(root_node == null) //if root is empty
{
root_node = new node(n, a);
root_node.left_child = null;
root_node.right_child = null;
}
else
{
while(true)
{
parent_node = current_node; //follow current_node in loop
if(current_node.age > a) //LEFT HAND SIDE
{
current_node = current_node.left_child; //move current node
if(current_node == null) //if empty
{
new_node = new node(n, a); //create new node
parent_node.left_child = new_node; //parent is same as current cude
break;
}
}
else /*** RIGHT SIDE OF TREE ***/
{
current_node = current_node.right_child;
if(current_node == null)
{
new_node = new node(n, a);
parent_node.right_child = new_node;
break;
}
}
}
}
}/*** End of Insert Method ***/
/*** IN ORDER TRAVERSAL METHOD ***/
//in order traversal - print all value lowest to heightest
public void inOrderTraversal(node current_node)
{
current_node = root_node;
if(current_node == null)
{
System.out.println("Binary Tree is empty!");
}
else
{
inOrderTraversal(current_node.left_child);
System.out.println(current_node);
inOrderTraversal(current_node.right_child);
}
}/*** END OF IN ORDER TRAVERSAL METHOD ***/
/*** Main Method ***/
public static void main(String[] args)
{
D06_Binary_Tree m = new D06_Binary_Tree();
m.insert("a", 50);
m.insert("b", 25);
m.insert("c", 15);
m.insert("d", 30);
m.insert("5", 75);
m.insert("4", 85);
m.inOrderTraversal(current_node);
}
}